Managing unit tests
I want to start using unit tests now (I picked the Boost.Test library as a start), but still have some basic questions.
1) Do you create a second project specifically for the tests?
2) If you do so: How do you set up the projects? I'm using the Visual Studio 2005 IDE and created 2 projects (located in the same solution). How can I make the cpp files from the main program available to the test program? Is there any option to specify some kind of reference?
I think you can add .cpp files to your second project by using Project->Add Existing Item->your cpp`s. Or you can build your main program as .dll and link to it from your second application.
Quote:Original post by Soth
I think you can add .cpp files to your second project by using Project->Add Existing Item->your cpp`s. Or you can build your main program as .dll and link to it from your second application.
Both solutions are certainly possible, although there are some drawbacks:
Adding the files manually to the test project involves some extra work. You have to keep both projects in sync (It would be perfect if you could specify some kind of reference folder, but I don't know whether this is possible in Visual Studio). When the project increases in size there is another problem: compilation time. Having two separate projects (with more or less identical source code files) the code has to be compiled twice.
A .DLL might incur substantial work (i.e. exporting classes etc.), but I tried a different approach using static libraries (.lib):
In the solution you create 3 different projects:
1) Code
2) Application
3) Tester
The Code project contains the classes you want to use in your application and which should also be tested. The project compiles into a lib, that is linked to by the other two projects.
I guess that the optimal way would look like this: There are two projects (the application with the code to be tested and the test project). Instead of sharing common code files the projects should share the compiled code files (.obj). The application is responsible for generating these files, whereas the linker of the test project just includes the obj files.
But I couldn't find a way to set this up in the Visual Studio IDE.
Any further ideas?
How do you set up your projects when you do unit testing?
I have two program entry points. When in NDEBUG mode, the release entry point is the normal one. When in _DEBUG mode, the debug entry point checks if "test" is passed as the first command line argument, and performs the tests (otherwise, it behaves normally).
Then, as a post-build step, I call the executable with "test" as the first command line argument.
When I have control over my own makefile, I usually create a test executable from the test files (which are adequately named) and run it as part of the build process — it is then removed unless it failed.
Then, as a post-build step, I call the executable with "test" as the first command line argument.
When I have control over my own makefile, I usually create a test executable from the test files (which are adequately named) and run it as part of the build process — it is then removed unless it failed.
Quote:Original post by ToohrVyk
I have two program entry points. When in NDEBUG mode, the release entry point is the normal one. When in _DEBUG mode, the debug entry point checks if "test" is passed as the first command line argument, and performs the tests (otherwise, it behaves normally).
Then, as a post-build step, I call the executable with "test" as the first command line argument.
When I have control over my own makefile, I usually create a test executable from the test files (which are adequately named) and run it as part of the build process — it is then removed unless it failed.
Which unit testing framework do you use? (something written by yourself?)
I would also prefer having both the application code and the unit testing code in one single executable. The problem is that the frameworks I've seen define a main() function themselves, which only executes the test code.
How can I use the test code and the application code side by side? (I'm using Boost.Test)
Just for comparison, here is my setup:
I am running CxxTest, and all my unit tests are in separate header files (they don't have to be). Because of this, I put all my files together into one VS project. Because it's a small project, I can run the tests manually. I have one configuration that compiles and runs the unit tests (with a separate executable), and then my normal debug/profile builds.
I don't think there's any need to have both the unit tests and the actual application code in the same executable. The only time you'll run the unit tests is during development (and should stay there), and actual application code is what you will be giving to the end user.
I am running CxxTest, and all my unit tests are in separate header files (they don't have to be). Because of this, I put all my files together into one VS project. Because it's a small project, I can run the tests manually. I have one configuration that compiles and runs the unit tests (with a separate executable), and then my normal debug/profile builds.
I don't think there's any need to have both the unit tests and the actual application code in the same executable. The only time you'll run the unit tests is during development (and should stay there), and actual application code is what you will be giving to the end user.
Quote:Original post by SlimTimmy
Which unit testing framework do you use? (something written by yourself?)
Usually, I use my own, yes.
Quote:Original post by psykr
Just for comparison, here is my setup:
I am running CxxTest, and all my unit tests are in separate header files (they don't have to be). Because of this, I put all my files together into one VS project. Because it's a small project, I can run the tests manually. I have one configuration that compiles and runs the unit tests (with a separate executable), and then my normal debug/profile builds.
How do you make the C++ files (which contain the classes to be tested) available to the unit test project? Do you add them manually to the project?
Quote:
I don't think there's any need to have both the unit tests and the actual application code in the same executable. The only time you'll run the unit tests is during development (and should stay there), and actual application code is what you will be giving to the end user.
You could control this by using some global defines (like the _DEBUG define). During development you would for example define "UNIT_TEST" and the unit test code gest compiled into the executable.
Quote:Original post by SlimTimmy
How do you make the C++ files (which contain the classes to be tested) available to the unit test project? Do you add them manually to the project?
Depends on the type of project your "main" project is. If it's a .lib, you can just add the project as a reference. (Right click on your test project, 'add reference', select the other project). Then all you need is to #include the headers, and the linker will take care of the rest.
If it's an exe or a dll? I guess you have to load it the usual way. (Haven't tried, but I'm pretty sure an exe can be loaded like a dll)
Hope that helps. I personally tend to use lots of static libraries, which has the nice side effect that they're trivial to link into a test project. :)
I've chosen the UnitTest++ framework (http://unittest-cpp.sourceforge.net/) for my first unit-tested application (it's a console application).
The structure:
Test code and application code are compiled into a single executable. By specifying "--test" as an argument you start the execution of the test code.
There are three different configurations:
1) DebugTest
2) ReleaseTest
3) Release
The first two configurations contain the test code, the last is used for shipping.
I'm using a global define to conditionally insert the test-specific code. In the end it looks like this:
Main.cpp
Test.cpp
[Edited by - SlimTimmy on January 11, 2007 8:41:35 AM]
The structure:
Test code and application code are compiled into a single executable. By specifying "--test" as an argument you start the execution of the test code.
There are three different configurations:
1) DebugTest
2) ReleaseTest
3) Release
The first two configurations contain the test code, the last is used for shipping.
I'm using a global define to conditionally insert the test-specific code. In the end it looks like this:
Main.cpp
#ifdef UNIT_TEST #include <UnitTest++.h>#endif#include <iostream>int main(int argc, char *argv[]) { #ifdef UNIT_TEST if( argc > 1 ) { string argument(argv[1]); if( argument == "--test" ) return UnitTest::RunAllTests(); } #endif cout << "Application";}
Test.cpp
#ifdef UNIT_TEST#include <UnitTest++.h>using namespace UnitTest;TEST(XYZTest) { CHECK(...);}#endif
[Edited by - SlimTimmy on January 11, 2007 8:41:35 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement