views:

150

answers:

6

I'm just getting started with TDD and am curious as to what approaches others take to run their tests. For reference, I am using the google testing framework, but I believe the question is applicable to most other testing frameworks and to languages other than C/C++.

My general approach so far has been to do one of three things:

  1. Write the majority of the application in a static library, then create two executables. One executable is the application itself, while the other is the test runner with all of the tests. Both link to the static library.

  2. Embed the testing code directly into the application itself, and enable or disable the testing code using compiler flags. This is probably the best approach I've used so far, but clutters up the code a bit.

  3. Embed the testing code directly into the application itself, and, given certain command-line switches either run the application itself or run the tests embedded in the application.

None of these solutions are particularly elegant...

How do you do it?

A: 

I'm using a third-party test-runners with their framework and including testing in build script. Tests are outside of production code (external dll).

Aen Sidhe
What framework? Also, this doesn't explain how the tests access your application code. Is this black box or system testing?
kurige
Mainly, I use .net for developing, so either NUnit (if project can't afford Visual Studio with testing suppost) or MS Unit Test framework. I've heard something about adding C++ support to MS Unit, but not sure.For second part of question: yes, it is blackbox testing.
Aen Sidhe
+4  A: 

Your approach no. 1 is the way I've always done it in C/C++ and Java. Most of the application code is in the static library and I try to keep the amount of extra code needed for the application to a minimum.

The way I approach TDD in Python and other dynamic languages is slightly different in that I leave the source code for the application and tests lying around and a test runner finds the tests and runs them.

quamrana
+1  A: 

I use two approaches, for dlls I just link my unit tests with the dll, easy. For executables I include the source files that are being tested in both the executable project and the unit test project. This adds slightly to the build time but means I don't need to separate the executable in to a static lib and a main function.

I use boost.test for unit testing and cmake to generate my project files and I find this the easiest approach. Also I am slowly introducing unit-testing to a large legacy code base so I am trying to introduce the least amount of changes, in case I inconvenience other developers and discourage them from unit testing. I would worry that using a static library just for unit testing might be seen as an excuse not adopt it.

Having said this, I think the static library approach is a nice one especially if you are starting from scratch.

iain
I like the idea of simply recompiling the relevant source files, but you're correct, it does add considerable time to build.For larger projects it would be impractical.
kurige
+2  A: 

For C/C++ apps I try to have as much code as possible in one or more dlls, with the main application being the bare minimum to start-up and hand-off to the dll. Dlls are much easier to test because they can export as many entry points as I like for a test application to use.

I use a seperate test application that links to the Dll(s). I'm strongly in favour of keeping test code and "product" code in seperate modules.

Andy Johnson
+1  A: 

I go with #1, some reasons are

  • It allows to check that each lib links correctly
  • You don't want extra code in the product
  • It's easier to debug individual small test programs
  • You may need multiple executables for some tests (like communication tests)

For C++ build and test, I like to use CMake which can run a selection of the target executables as tests and print a summary of the results.

fa.
+1  A: 

I tend to favour static libs over dlls so most of my C++ code ends up in static libs anyway and, as you've found, they're as easy to test as dlls.

For code that builds into an exe I either have a separate test project which simply includes the source files that are under test and that are usually built into the exe OR I build a new static lib that contains most of the exe and test that in the same way that I test all of my other static libs. I find that I usually take the 'most code in a library' approach with new projects and the 'pull the source files from the exe project into the test project' approach when I'm retro fitting tests to existing applications.

I don't like your options 2 and 3 at all. Managing the build configurations for 2 is probably harder than having a separate test project that simply pulls in the sources it needs and including all of the tests into the exe as you suggest in 3 is just wrong ;)

Len Holgate