Best Practice - Link to lib or compile source in Unit-Tests

Viewed 1169

I want to ask what your experience is with getting the tested Source in your Unit-Test Application. Following structure

NiceLib
|-src
   |- MyNiceClass.h
   |- MyNiceClass.cpp
   |- CMakeLists.txt
|-test
   |- tester_MyNiceClass.cpp
   |- main.cpp
   |- CMakeLists.txt

In src a Library get compiled. In test a Test executable get builded. What is the best practice to get the implementation of MyNiceClass.cpp in your test application. At the moment I know two Options. Option one is to link against the library in the test executable. The test\CMakeLists.txt would look like:

enable_testing()

include_directories(../src/)

add_executable(tester_NiceLib
   main.cpp
   tester_MyNiceClass.cpp
   )

add_test(NicLibTest tester_NiceLib)

target_link_libraries(tester_NiceLib NiceLib)

The second solution is:

enable_testing()

include_directories(../src/)

add_executable(tester_NiceLib
   main.cpp
   tester_MyNiceClass.cpp

   ../src/MyNiceClass.h
   ../src/MyNiceClass.cpp
   )

add_test(NicLibTest tester_NiceLib)

What is your experience?Have you any Best-Practices or maybe other solutions?

1 Answers
Related