2
votes

I am starting to use GoogleTest. It seems that it needs a main file for running the tests:

Separate test cases across multiple files in google test

But currently in my demo application I already have a main file:

src/
-> MyType.h
-> main.cpp
-> Makefile

Which will eventually be my "production" application. I don't want to clutter that with gtest includes, macros etc.

Should I just create another main.cpp file in another folder e.g.: test/ that will contain all the specific gtest configuration so I would end up with:

src/
-> MyType.h
-> main.cpp
-> Makefile // Makefile for producing production code/binaries
Test/
-> MyTypeTest.h // Unittest for MyType
-> main.cpp // The "Test runner"
-> Makefile // Makefile for producing test executable

EDIT:

Found this based on cmake:

http://www.kaizou.org/2014/11/gtest-cmake/

which seems to be exactly what I am looking for.

3
You might want to switch to a different build system before your project gets big. Makefiles tend to get really convoluted for bigger projects unless you already have much experience managing makefiles.smerlin
I tend to setup a tests subdirecty for each project/library, and generate a google test binary for each test source file. That way, even if one testcase causes an application crash, all other tests and testcases will still run. (I am using CMake as build tool and use ctest to run all tests)smerlin
I always liked the idea of the tests living in a separate directory.Daniel Kamil Kozar

3 Answers

3
votes

The most sensible approach to this is to have a library for your production code and then two executables, one for production and another one for tests:

|-lib/
| |-Makefile
| |-mytype.h
| `-mytype.cpp
|-app/
| |-Makefile
| `-main.cpp
`-test/
  |-Makefile
  `-mytypetest.cpp

Notice that gtest distribution provides the gtest library and a gtest_main library with the standard main function for your test executable. So unless you need a custom main (rare case) you don't need to provide a main.cpp for your tests and can simply link against gtest_main, e.g. $(CC) mytypetest.cpp -o apptests -lapplib -lgtest_main -lgtest.

The library approach involves slightly more complex Makefiles, but it pays off in compilation time, since not having it implies you need to compile mytype.cpp once for production application and once for test executable.

0
votes

There are probably a lot of ways to do this, but generally speaking, yes, you should add a test-specific main function to your project. This makes compilation a little bit more complex since you'll have to produce two separate binaries (one for your application and another for your tests) but this is a fairly typical setup.

0
votes

I'd simply add a test.cpp file with a main and create a test target in my makefile so that I could either make - to build my production code - or make test - to build the tests. In actual projects I use cmake in very similar fashion (I sometimes bundle all common dependencies in a core.a library and then link both main and test against it).