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


i want ask experience getting tested source in unit-test application. following structure

nicelib |-src    |- myniceclass.h    |- myniceclass.cpp    |- cmakelists.txt |-test    |- tester_myniceclass.cpp    |- main.cpp    |- cmakelists.txt 

in src library compiled. in test test executable builded. best practice implementation of myniceclass.cpp in test application. @ moment know 2 options. option 1 link against library in test executable. test\cmakelists.txt 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 experience?have best-practices or maybe other solutions?

what experience?have best-practices or maybe other solutions?

i use both variants (successfully), which 1 suits best depends on situation. if need single class, library contains fair amount of other stuff, may overkill. there's no general "best solution".

personally recommend choose suits best needs. it's no problem switch between both @ later time.

however, better don't integrate plain source files tests, have compiled several times (for each test + productive code). can include actual object files instead , compile once. gives more flexibility , better dependency management of cmake.

therefore, here 2 variants use:

option 1: link library

this same first option.

build library:

add_library(dependencies-lib src1.cpp src2.cpp) 

link tests:

# create test target ... add_executable(exampletest sometests.cpp) target_link_libraries(exampletest dependencies-lib) 

option 2: add object library

this same option 1, can selective. however, prevents multiple compilation of plain source files method.

build object library:

add_library(dependencies-lib object src1.cpp src2.cpp) #                            ^^^^^^ 

add objects tests:

# create test target ... add_executable(exampletest sometests.cpp                          $<target_objects:dependencies-lib> # <-- !!                         ) 

as can see, don't link library, use compiled object files instead. if many other targets need these, compiled only once. btw. can pack them ordinary library / executable (eg. productive binary / lib):

add_library(all-together-lib source1.cpp                 source2.cpp                 # ...                  $<target_objects:dependencies-lib>                 $<target_objects:dependencies-lib2>                 $<target_objects:dependencies-lib3>                 $<target_objects:dependencies-and-many-more>                 # ...                  # , more source files ...                 example1.cpp                 example2.cpp                 # ...                 ) 

documentation


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -