username01 username01 - 15 days ago 6
C++ Question

Unit testing native C++ code

I have some questions about using Microsoft::VisualStudio::CppUnitTestFramework and Test Explorer for native C++ projects in Visual Studio 2012.


  1. How can i run tests in specific order? In this msdn article said:


    For more information, see Organizing C++ Tests.


    But there is no information about it on msdn, or i just can't find it. (All i found was some articles about ordered tests in managed projects)

  2. In this article said, that in case when my under_test code is static library - i can access private methods from tests. But i can't direct access to it, and found nothing better then define private/protected as public when testing, but i think that it isn't right way.

  3. I only recently start using unit tests and not very familiar with it.
    What is the best framework to start with (for Windows + native C++ usage)? May be boost or googletest, or any other? My main IDE is Visual Studio, so i want good integration between test framework and IDE, like manage/run tests from native Test Explorer, etc.


Answer

For the first question: You can't. You should write your unit tests in a way, that they do not interfere with each others results.

Second: The only way to do that is with reflections. Again I do not recommed it. If you feel, that there are private method that should be tested, that is almost always the sign of that you should divide your class into smaller classes.

Third: The Visual Studio test framework is fine, if you do not have any specific need to look for others, then just stuck with it.

And good luck with unit testing :) If you feel a little lost, let me recommend you this book:

http://www.amazon.com/Test-Driven-Development-By-Example/dp/0321146530

It explains everything you need.