Should i put unit tests in a separate library? Separate subdirectory?
-
16-06-2021 - |
Question
I have a large library of code which has a few (much too few) unit tests; i am adding code to the library, trying to make unit tests for the new code (i also want to add unit tests for old code in future, when i get more experienced).
We currently use a custom (not very elaborate) unit-testing framework; i would like to switch to boost.test
.
My library layout is:
mylib1 (300 files, 70000 lines of code)
- src
- foo
- foo.cpp
- foo_part.cpp
- foo_part.h
- UnitTests - should i move it outside
mylib
?- foo_test1.cpp
- foo_test2.cpp
- foo_test3.cpp
- bar
- bar.cpp
- bar_part.cpp
- bar_part.h
- UnitTests - does it belong here?
- bar_test1.cpp
- bar_test2.cpp
- bar_test3.cpp
- foo
- include
- foo.h
- bar.h
- src
mylib2 (70 files, 7000 lines of code)
- Looks like the mylib1 above
UnitTestFramework
- src
- MockObject1.cpp - does it belong here?
- Asserter.cpp -
boost.test
will replace it - TestFixture.cpp -
boost.test
will replace it
- include
- MockObject1.h
- Asserter.h -
boost.test
will replace it - TestFixture.h -
boost.test
will replace it
- src
I am asking the following questions here:
- Should unit tests be part of
mylib
DLL? (I linkmylib1
andmylib2
into one DLL) - Should source code for unit tests be located under the
mylib1
directory? - Since i have
mylib1
andmylib2
, how should the corresponding unit-tests be located - in sibling directories, together, etc? - Where should i put source code for mock objects? (they serve both
mylib1
andmylib2
)
Solution
- unit tests are kind of documentation, therefore I would leave the directory structure as it is
- mocks/fakes are helpers in unit tests, therefore I wouldn't leave them in the source directory. Create a new directory in the source, and move them there.
Actually, I would simplify it even further :
+ mylib1 (300 files, 70000 lines of code)
+ unit_tests
+ test_foo.cpp
+ test_bar.cpp
+ mocks
+ mock_foo.hpp
+ mock_bar.hpp
+ foo.cpp
+ foo.hpp
+ bar.cpp
+ bar.hpp
+ mylib2
similar as for mylib1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow