Native C++ Unit Testing - Method to be run once RRS feed

  • Question

  • I am not understanding TEST_MODULE_INITIALIZE...

    The docs only give me a couple options. TEST_CLASS_INITIALIZE and TEST_MODULE_INITIALIZE. Neither are doing what
    I need. I went in debug a test and it seems 

    The TEST_CLASS_INITIALIZE method gets called before each test method. The class is created and destroyed for every  test method being tested... That really stinks, the point of the test class was to carry some state over from one test to the next.

    TEST_MODULE_INITIALIZE seems to get called once for the entire project and all the tests therein. That's not what I need either. 

    I need a method that gets called once per .cpp and all the tests it contains. For example, I have a group of tests that will parse different files and put all the data into a database to be checked. I need one test method per file type and one method that creates and destroys the database for all the tests in that .cpp Another .cpp in the project would want the database created and destroyed for its batch, neither .cpp should know or care about the other. I don't see how that's possible.

    Any ideas on how to achieve that?
    Monday, June 30, 2014 3:00 PM


  • Hi,

    TEST_MODULE_INITIALIZE method will be called when a project is loaded.

    Based on your description, I feel you want one method to get called once when loading a class in a unittest.cpp file. Except TEST_CLASS_INITIALIZE method, no other method in unit test has such feature.  And this method is called before running all test methods. I think that TEST_CLASS_INITIALIZE method can meet your requirement.

    If you want one method to get called once when running each test method, you can use TEST_METHOD_INITIALIZE METHOD.

    For more information, please see: Using Microsoft.VisualStudio.TestTools.CppUnitTestFramework

    Best regards,

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 1, 2014 6:39 AM