Reputation: 18784
I am interested in hearing about experiences using TDD and unit testing for C++ in general with Visual Studio 2005 (Professional)
First some background. We have a fairly large project and much of it has been developed on Linux using CppUnit for the unit tests. The project is divided into several libraries, each with their own set of tests. I have a simple script which compiles the library, compiles the test suite and then runs the tests. So after making changes in the code I just run "test" from a command line and the tests run.
Now, most of the developers are using Visual Studio 2005 on Windows for the development of this product. Of course they can still run the tests from a command line using nmake but involves extra steps and I would prefer to have a much more integrated solution.
So my question has two parts.
Firstly, what is the best way of laying out the code for tests on a large code base? Is it normal to create several test projects in a solution, one for each library?
Secondly are there any tools for integrating CppUnit tests into Visual Studio? With dependencies set up corectly running the test project should run the tests but currently the results still appear in a command window.
Upvotes: 11
Views: 1120
Reputation: 19063
Here is what I do:
Personally, I don't think the test framework (Google Test, Boost test, CppUnit, etc) matters that much. Most are pretty much functionally equivalent.
I'm not entirely happy with the number of projects generated, but I consider this a Visual Studio GUI issue, in the sense that its actually quite useful to have these projects included like this for purposes such as debugging.
I tried using post build steps to run the tests but this unfortunately mean that the build was not interrupted after the first failure has passed.
Upvotes: 2
Reputation: 136603
Upvotes: 2
Reputation: 46496
You can also use managed C++ to write unit tests in Visual Studio, using the unit testing framework that's built in.
Upvotes: 1
Reputation: 1433
One of the projects at my company does exactly this. We use a unit test framework called CXXTest (http://cxxtest.sourceforge.net/guide.html). We really like this framework for C++ because it only requires that you write a header file that contains your unit tests. The .CPP files are created by a script (both Python and Perl scripts are provided).
We integrate with visual studio by providing a post build step that builds the unit tests (if they need building) and then executes them. The output (showing what passed and what failed) is displayed in the output window -- you never need to leave the IDE.
Upvotes: 3
Reputation: 78598
I use the Boost Test framework. I tend to split my code into .lib files and will have a separate console-mode EXE test project for each. When the test project is built it makes use of the 'Post build stage' to launch itself, thus running the tests. You could make each test project a dependency of your main application so that each time it builds, all tests are run first, but this can be time-consuming. Instead I tend to run the test projects by hand as needed, but my automated nightly build system will run all test projects as a matter of course (I script this and if any tests fail, the build fails and I get an email notification).
More details here.
Upvotes: 2
Reputation: 1157
My team is currently using an system where we have an automated nightly build (that can also be run from the project build dashboard by anyone) that includes a VS2k5 "test" solution. The test solution holds all the unit test projects; one unit test project for every "unit" of code in the main project.
When the automated build runs, it builds the main solution, then the test solution, and finally runs all the executables produced by the test solution (a Perl script glues this together). The results of the compile as well as the test execution (EXIT _ SUCCESS, EXIT _ FAILURE) are used to update the project build dashboard.
That EXIT _ FAILURE trick can also be applied to a custom build step of the main project: if the unit test custom build step returns EXIT _ FAILURE, then the build itself fails.
Upvotes: 1