locked
how to populate data in each unit test method? RRS feed

  • Question

  • I need to do set of unit tests on set of data. data will be populated into database programmatically every time when unit test executed.  I am using Microsoft Unit test Framework.

    My requirement is, I need to populate the data before starting the first unit test and delete the data after the end of last unit test

    For this I put the populate data in the Test Class constructor, delete data in the destructor of the class.

    But constructor is been called for each test method, but should be once. Is there any approach to solve this scenario?

        [TestClass]
        public class VoyageExplorerSvcTest
        {
            public  Voyage newVoyage;
    
            public VoyageExplorerSvcTest()
            {
    
                 //Insert test data
            }
    
            [TestMethod]
            public void TestMethod1()
            {
                  //do unit test on inserted data.
            }
    
    
            [TestMethod]
            public void TestMethod2()
            {
                  //do unit test on inserted data.
            }
    
             ~VoyageExplorerSvcTest()
            {
                //delete the data
            }
        }


    Regards, Anand


    • Edited by manand1234 Friday, November 23, 2012 4:39 AM
    Thursday, November 22, 2012 4:20 PM

Answers

  • Hi,

    use Additional test attributes:

    • [ClassInitialize()]   Use ClassInitialize to run code before you run the first test in the class.
    • [ClassCleanUp()]   Use ClassCleanup to run code after all tests in a class have run.
    • [TestInitialize()]   Use TestInitialize to run code before you run each test.
    • [TestCleanUp()]   Use TestCleanup to run code after each test has run.

    From this article

    Strictly speaking when putting data in the database and letting your tests depend on it the tests aren't unit tests any more but integration tests. For a great resource on unit testing and unit testing patterns read the x-unit test patterns book and see the x-unit test patterns site.


    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me



    Friday, November 23, 2012 1:19 PM
  • In addition to what @KeesDijk said, keep in mind that MSTest creates a new instance of the test class for each test method execution by design. This helps to isolate tests and prevent test faulires caused by shared test environment. By using ClassInitialize/ClassCleanup methods, you are explicitly creating shared test environment. This is a valid optimization technique, however before going down this path, try making your tests more granular and reducing the amount of seed data they require first. You can find more information here.
    Monday, November 26, 2012 4:41 PM

All replies

  • Hi,

    use Additional test attributes:

    • [ClassInitialize()]   Use ClassInitialize to run code before you run the first test in the class.
    • [ClassCleanUp()]   Use ClassCleanup to run code after all tests in a class have run.
    • [TestInitialize()]   Use TestInitialize to run code before you run each test.
    • [TestCleanUp()]   Use TestCleanup to run code after each test has run.

    From this article

    Strictly speaking when putting data in the database and letting your tests depend on it the tests aren't unit tests any more but integration tests. For a great resource on unit testing and unit testing patterns read the x-unit test patterns book and see the x-unit test patterns site.


    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me



    Friday, November 23, 2012 1:19 PM
  • In addition to what @KeesDijk said, keep in mind that MSTest creates a new instance of the test class for each test method execution by design. This helps to isolate tests and prevent test faulires caused by shared test environment. By using ClassInitialize/ClassCleanup methods, you are explicitly creating shared test environment. This is a valid optimization technique, however before going down this path, try making your tests more granular and reducing the amount of seed data they require first. You can find more information here.
    Monday, November 26, 2012 4:41 PM