Reinitialize Table Values in SQL SSDT Unit TestingUnit testing of stored proceduresDocumenting a giant web of...

Workplace intimidation due to child's chronic health condition

Why there is no EEPROM in STM32F4 MCUs

How can a kingdom keep the secret of a missing monarchy from the public?

Should corporate security training be tailored based on a users' job role?

multiple price sets?

Why does Python copy numpy arrays where the length of the dimensions are the same?

Badly designed reimbursement form. What does that say about the company?

Why is Bernie Sanders maximum accepted donation on actblue $5600?

Identical projects by students at two different colleges: still plagiarism?

How to play song that contains one guitar when we have two guitarists (or more)?

Bitcoin automatically diverted to bech32 address

How can I portray body horror and still be sensitive to people with disabilities?

Who, if anyone, was the first astronaut to return to earth in a different vessel?

Is 'bad luck' with former employees a red flag?

"Cheaper by the dozen" phrase origin?

Apparently I’m calling random numbers but nothing in call log?

Is layered encryption more secure than long passwords?

Why does XLAT mean 'translate'?

How does one use the Nerode-Myhill theorem to prove that a language is regular?

A dragon's soul trapped in a ring of mind shielding wants a new body; what magic could enable her to do so?

Does the phrase がんばする makes sense?

How to announce in an ATIS message that two parallel runways are in use?

Does limiting the number of sources help simplify the game for a new DM with new and experienced players?

The idea behind the state of Imperative languages



Reinitialize Table Values in SQL SSDT Unit Testing


Unit testing of stored proceduresDocumenting a giant web of inter-related stored procedures in an MS SQL database: What tool or format?SSDT on VS2013 for SQL Server 2012Connect SQL Server 2016 SSDT with TFSSql Server Stored Procs vs Linq with C# and unit testingWhy would I get PREEMPTIVE_OS_AUTHORIZATIONOPS using tSQLt when Working at Home?SQL Server Data Tools 15.5.0 - Script Component Validation ErrorSSIS Not Executing any Script TaskView with OPENQUERY won't build in SSDT projectMake SQL Server SSDT Database Project Reference Different in Publish Profile













5















I am creating SQL Server Unit Tests. We are testing various stored procedures.



In Unit testing principles, it is good practice to setup a small database table, populate values, and tear down (truncate/delete) the database tables, and resetup for each test. This way every unit tests will have a clean environment to validate sprocs which insert, select, update, delete, etc,



Does anyone where or how to reinitialize the tables values in Sql Unit Testing? Resources are pretty new for unit testing in SQL SSDT VS 2017, so I think lot of people are trying to figure out and understand.



Feel free to show or add pictures below.



http://www.sqlservercentral.com/articles/Unit+Testing/155651/
http://www.erikhudzik.com/2017/08/23/writing-sql-server-unit-tests-using-visual-studio-nunit-and-sqltest/



Pictures in Visual Studio SSDT:



enter image description here



Also, trying to review this class in SQLDatabaseSetup.cs:



 [TestClass()]
public class SqlDatabaseSetup
{

[AssemblyInitialize()]
public static void InitializeAssembly(TestContext ctx)
{
// Setup the test database based on setting in the
// configuration file
SqlDatabaseTestClass.TestService.DeployDatabaseProject();
SqlDatabaseTestClass.TestService.GenerateData();
}

}
}


using Microsoft.Data.Tools.Schema.Sql.UnitTesting;









share|improve this question
















bumped to the homepage by Community 7 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1





    The typical place for tear down scripts is in the Post-test, where you could include TRUNCATE or DELETE for the table(s) affected by the test. The general principals are that unit tests should not be dependent on one another and the outcome of a given unit test, pass or fail, should not affect the outcome of other tests so that they can be run independently of one another.

    – Dan Guzman
    Oct 24 '18 at 10:09











  • Agree with @DanGuzman, Still in case of relational data with Cascade Delete in Procedure we need to check the effect in relational table with cascade effect. This will require Tests created to each table separately and need to run each test in related sequence.

    – MarmiK
    Nov 16 '18 at 16:12
















5















I am creating SQL Server Unit Tests. We are testing various stored procedures.



In Unit testing principles, it is good practice to setup a small database table, populate values, and tear down (truncate/delete) the database tables, and resetup for each test. This way every unit tests will have a clean environment to validate sprocs which insert, select, update, delete, etc,



Does anyone where or how to reinitialize the tables values in Sql Unit Testing? Resources are pretty new for unit testing in SQL SSDT VS 2017, so I think lot of people are trying to figure out and understand.



Feel free to show or add pictures below.



http://www.sqlservercentral.com/articles/Unit+Testing/155651/
http://www.erikhudzik.com/2017/08/23/writing-sql-server-unit-tests-using-visual-studio-nunit-and-sqltest/



Pictures in Visual Studio SSDT:



enter image description here



Also, trying to review this class in SQLDatabaseSetup.cs:



 [TestClass()]
public class SqlDatabaseSetup
{

[AssemblyInitialize()]
public static void InitializeAssembly(TestContext ctx)
{
// Setup the test database based on setting in the
// configuration file
SqlDatabaseTestClass.TestService.DeployDatabaseProject();
SqlDatabaseTestClass.TestService.GenerateData();
}

}
}


using Microsoft.Data.Tools.Schema.Sql.UnitTesting;









share|improve this question
















bumped to the homepage by Community 7 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1





    The typical place for tear down scripts is in the Post-test, where you could include TRUNCATE or DELETE for the table(s) affected by the test. The general principals are that unit tests should not be dependent on one another and the outcome of a given unit test, pass or fail, should not affect the outcome of other tests so that they can be run independently of one another.

    – Dan Guzman
    Oct 24 '18 at 10:09











  • Agree with @DanGuzman, Still in case of relational data with Cascade Delete in Procedure we need to check the effect in relational table with cascade effect. This will require Tests created to each table separately and need to run each test in related sequence.

    – MarmiK
    Nov 16 '18 at 16:12














5












5








5








I am creating SQL Server Unit Tests. We are testing various stored procedures.



In Unit testing principles, it is good practice to setup a small database table, populate values, and tear down (truncate/delete) the database tables, and resetup for each test. This way every unit tests will have a clean environment to validate sprocs which insert, select, update, delete, etc,



Does anyone where or how to reinitialize the tables values in Sql Unit Testing? Resources are pretty new for unit testing in SQL SSDT VS 2017, so I think lot of people are trying to figure out and understand.



Feel free to show or add pictures below.



http://www.sqlservercentral.com/articles/Unit+Testing/155651/
http://www.erikhudzik.com/2017/08/23/writing-sql-server-unit-tests-using-visual-studio-nunit-and-sqltest/



Pictures in Visual Studio SSDT:



enter image description here



Also, trying to review this class in SQLDatabaseSetup.cs:



 [TestClass()]
public class SqlDatabaseSetup
{

[AssemblyInitialize()]
public static void InitializeAssembly(TestContext ctx)
{
// Setup the test database based on setting in the
// configuration file
SqlDatabaseTestClass.TestService.DeployDatabaseProject();
SqlDatabaseTestClass.TestService.GenerateData();
}

}
}


using Microsoft.Data.Tools.Schema.Sql.UnitTesting;









share|improve this question
















I am creating SQL Server Unit Tests. We are testing various stored procedures.



In Unit testing principles, it is good practice to setup a small database table, populate values, and tear down (truncate/delete) the database tables, and resetup for each test. This way every unit tests will have a clean environment to validate sprocs which insert, select, update, delete, etc,



Does anyone where or how to reinitialize the tables values in Sql Unit Testing? Resources are pretty new for unit testing in SQL SSDT VS 2017, so I think lot of people are trying to figure out and understand.



Feel free to show or add pictures below.



http://www.sqlservercentral.com/articles/Unit+Testing/155651/
http://www.erikhudzik.com/2017/08/23/writing-sql-server-unit-tests-using-visual-studio-nunit-and-sqltest/



Pictures in Visual Studio SSDT:



enter image description here



Also, trying to review this class in SQLDatabaseSetup.cs:



 [TestClass()]
public class SqlDatabaseSetup
{

[AssemblyInitialize()]
public static void InitializeAssembly(TestContext ctx)
{
// Setup the test database based on setting in the
// configuration file
SqlDatabaseTestClass.TestService.DeployDatabaseProject();
SqlDatabaseTestClass.TestService.GenerateData();
}

}
}


using Microsoft.Data.Tools.Schema.Sql.UnitTesting;






sql-server sql-server-2016 ssdt visual-studio unit-test






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 24 '18 at 5:14

























asked Oct 24 '18 at 5:09







user162241












bumped to the homepage by Community 7 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 7 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1





    The typical place for tear down scripts is in the Post-test, where you could include TRUNCATE or DELETE for the table(s) affected by the test. The general principals are that unit tests should not be dependent on one another and the outcome of a given unit test, pass or fail, should not affect the outcome of other tests so that they can be run independently of one another.

    – Dan Guzman
    Oct 24 '18 at 10:09











  • Agree with @DanGuzman, Still in case of relational data with Cascade Delete in Procedure we need to check the effect in relational table with cascade effect. This will require Tests created to each table separately and need to run each test in related sequence.

    – MarmiK
    Nov 16 '18 at 16:12














  • 1





    The typical place for tear down scripts is in the Post-test, where you could include TRUNCATE or DELETE for the table(s) affected by the test. The general principals are that unit tests should not be dependent on one another and the outcome of a given unit test, pass or fail, should not affect the outcome of other tests so that they can be run independently of one another.

    – Dan Guzman
    Oct 24 '18 at 10:09











  • Agree with @DanGuzman, Still in case of relational data with Cascade Delete in Procedure we need to check the effect in relational table with cascade effect. This will require Tests created to each table separately and need to run each test in related sequence.

    – MarmiK
    Nov 16 '18 at 16:12








1




1





The typical place for tear down scripts is in the Post-test, where you could include TRUNCATE or DELETE for the table(s) affected by the test. The general principals are that unit tests should not be dependent on one another and the outcome of a given unit test, pass or fail, should not affect the outcome of other tests so that they can be run independently of one another.

– Dan Guzman
Oct 24 '18 at 10:09





The typical place for tear down scripts is in the Post-test, where you could include TRUNCATE or DELETE for the table(s) affected by the test. The general principals are that unit tests should not be dependent on one another and the outcome of a given unit test, pass or fail, should not affect the outcome of other tests so that they can be run independently of one another.

– Dan Guzman
Oct 24 '18 at 10:09













Agree with @DanGuzman, Still in case of relational data with Cascade Delete in Procedure we need to check the effect in relational table with cascade effect. This will require Tests created to each table separately and need to run each test in related sequence.

– MarmiK
Nov 16 '18 at 16:12





Agree with @DanGuzman, Still in case of relational data with Cascade Delete in Procedure we need to check the effect in relational table with cascade effect. This will require Tests created to each table separately and need to run each test in related sequence.

– MarmiK
Nov 16 '18 at 16:12










1 Answer
1






active

oldest

votes


















0














I have created the usp_Generate_Merge_For_Table procedure a few years ago exactly for this purpose.
It can generate a MERGE command with a Values Constructor in order to "re-initialize" the contents of a specific table.



You can find it here:



https://gist.github.com/EitanBlumin/7faba0b39c4f90d4cfa879a45f3e01eb#file-generate_merge_for_table-sql



Direct link to the raw script:



https://gist.githubusercontent.com/EitanBlumin/7faba0b39c4f90d4cfa879a45f3e01eb/raw/608177e77da0a42d686098ab10703bbba8c54d64/Generate_Merge_For_Table.sql



Simply create the procedure in your database, run it while sending it your table name as a parameter, like so:



EXEC usp_Generate_Merge_For_Table 'cities', 'dbo'


And then you can copy and paste its output in your Unit Testing project.



You can find more info and detailed instructions on how to use it here:



https://eitanblumin.com/2018/10/28/generate-merge-statement-as-a-snapshot-of-your-tables-content/






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f220867%2freinitialize-table-values-in-sql-ssdt-unit-testing%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown
























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I have created the usp_Generate_Merge_For_Table procedure a few years ago exactly for this purpose.
    It can generate a MERGE command with a Values Constructor in order to "re-initialize" the contents of a specific table.



    You can find it here:



    https://gist.github.com/EitanBlumin/7faba0b39c4f90d4cfa879a45f3e01eb#file-generate_merge_for_table-sql



    Direct link to the raw script:



    https://gist.githubusercontent.com/EitanBlumin/7faba0b39c4f90d4cfa879a45f3e01eb/raw/608177e77da0a42d686098ab10703bbba8c54d64/Generate_Merge_For_Table.sql



    Simply create the procedure in your database, run it while sending it your table name as a parameter, like so:



    EXEC usp_Generate_Merge_For_Table 'cities', 'dbo'


    And then you can copy and paste its output in your Unit Testing project.



    You can find more info and detailed instructions on how to use it here:



    https://eitanblumin.com/2018/10/28/generate-merge-statement-as-a-snapshot-of-your-tables-content/






    share|improve this answer




























      0














      I have created the usp_Generate_Merge_For_Table procedure a few years ago exactly for this purpose.
      It can generate a MERGE command with a Values Constructor in order to "re-initialize" the contents of a specific table.



      You can find it here:



      https://gist.github.com/EitanBlumin/7faba0b39c4f90d4cfa879a45f3e01eb#file-generate_merge_for_table-sql



      Direct link to the raw script:



      https://gist.githubusercontent.com/EitanBlumin/7faba0b39c4f90d4cfa879a45f3e01eb/raw/608177e77da0a42d686098ab10703bbba8c54d64/Generate_Merge_For_Table.sql



      Simply create the procedure in your database, run it while sending it your table name as a parameter, like so:



      EXEC usp_Generate_Merge_For_Table 'cities', 'dbo'


      And then you can copy and paste its output in your Unit Testing project.



      You can find more info and detailed instructions on how to use it here:



      https://eitanblumin.com/2018/10/28/generate-merge-statement-as-a-snapshot-of-your-tables-content/






      share|improve this answer


























        0












        0








        0







        I have created the usp_Generate_Merge_For_Table procedure a few years ago exactly for this purpose.
        It can generate a MERGE command with a Values Constructor in order to "re-initialize" the contents of a specific table.



        You can find it here:



        https://gist.github.com/EitanBlumin/7faba0b39c4f90d4cfa879a45f3e01eb#file-generate_merge_for_table-sql



        Direct link to the raw script:



        https://gist.githubusercontent.com/EitanBlumin/7faba0b39c4f90d4cfa879a45f3e01eb/raw/608177e77da0a42d686098ab10703bbba8c54d64/Generate_Merge_For_Table.sql



        Simply create the procedure in your database, run it while sending it your table name as a parameter, like so:



        EXEC usp_Generate_Merge_For_Table 'cities', 'dbo'


        And then you can copy and paste its output in your Unit Testing project.



        You can find more info and detailed instructions on how to use it here:



        https://eitanblumin.com/2018/10/28/generate-merge-statement-as-a-snapshot-of-your-tables-content/






        share|improve this answer













        I have created the usp_Generate_Merge_For_Table procedure a few years ago exactly for this purpose.
        It can generate a MERGE command with a Values Constructor in order to "re-initialize" the contents of a specific table.



        You can find it here:



        https://gist.github.com/EitanBlumin/7faba0b39c4f90d4cfa879a45f3e01eb#file-generate_merge_for_table-sql



        Direct link to the raw script:



        https://gist.githubusercontent.com/EitanBlumin/7faba0b39c4f90d4cfa879a45f3e01eb/raw/608177e77da0a42d686098ab10703bbba8c54d64/Generate_Merge_For_Table.sql



        Simply create the procedure in your database, run it while sending it your table name as a parameter, like so:



        EXEC usp_Generate_Merge_For_Table 'cities', 'dbo'


        And then you can copy and paste its output in your Unit Testing project.



        You can find more info and detailed instructions on how to use it here:



        https://eitanblumin.com/2018/10/28/generate-merge-statement-as-a-snapshot-of-your-tables-content/







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 27 '18 at 14:54









        Eitan BluminEitan Blumin

        666




        666






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f220867%2freinitialize-table-values-in-sql-ssdt-unit-testing%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            ORA-01691 (unable to extend lob segment) even though my tablespace has AUTOEXTEND onORA-01692: unable to...

            Always On Availability groups resolving state after failover - Remote harden of transaction...

            Circunscripción electoral de Guipúzcoa Referencias Menú de navegaciónLas claves del sistema electoral en...