C++ – How to Structure Simple Projects for TDD

ceclipsetddunit testing

I am introducing Google Test into our development environment at work, and would like to encourage a TDD mindset, making this as accessible as possible for new developers to start using. I am looking for recommendations on standard project structure to follow for relatively simple C++ Eclipse projects. These would generally be of the order of around 10-30 classes for engineering projects.

I am currently organizing the folders in this fashion:

  1. src
  2. include
  3. test
  4. gtest_src

Also, is it recommended for testing to split any project into three sub projects?

Ie:

  1. Library (for testing)
  2. Executable project
  3. Test project

Should 1. and 3. be combined into one project?

I am coming back to C++ after a while with more focus on C# and Python and this structure seems a bit painful for every new project.

Best Answer

Adding this as an answer ( because long :) ), but the "best" structure of your project is subjective.

I tend to apply this exact architecture for my C++ projects, both at home (CMake + XCode) and at work (Visual Studio):

  • use separate unit test library (your gtest_src)
  • use static libraries implementing core functionality
  • use thin executable applications
  • use separate applications for testing.

It is a good idea to keep the tests separate from the code (either as another extra library, or written directly into the test applications.

At home, I have the following:

static libs:

  • unittest (unit testing static lib)
  • logging (logging static lib)
  • net (networking primitives static lib)
  • tools (generic, reusable code and tools, algorithms, etc)

test libs:

  • unittest-tests (dynamic tests lib)
  • logging-tests (dynamic tests lib)
  • net-tests (dynamic tests lib)
  • tools-tests (dynamic tests lib)

  • test-runner (app) (works with the test libs passed as input cli arguments)
  • various other network applications