I’m a little bit confused how to work with mock and dynamic linked libraries. Let’s assume a project structure like the following
subdirs.pro (subdir project)
\- app (subdir project)
\-- app (executable, include and use lib)
\-- lib (dynamic library)
\- test (subdir project)
\-- test_app (test the app WITHOUT test lib again)
\-- test_lib (fully test of lib functions)
The ‘app’ is using a library; the library is tested within the test_lib project.
Now I want to test the app, but I don’t want to test the whole lib stuff again (which is stupid and double work with no effort!). So I need some way to mock away the whole lib. Have anyone done this before in Qt and can help me out? Is this possible within the Qt test framework? I read already a lot of articles and SO questions, but I didn’t find any solution for this special problem.
I use Qt Creator 4.0.3 based on Qt 5.6.1, qmake with mscv2013 and the included Qt test framework.
As I proposed in comments to create a library that will just mock the API of the original library, here is how it can be done. Let's assume you have a class
Foo
with some trivial API in our original library:To create a mock library you have to follow the same class hierarchy as in the original library preserving the public API (private and protected stuff can be ignored), so that the application can compile and link against the mock library too. So, our
Foo
class will look like:You will have to do the same trick with all classes that your application uses. If you want to build your application for testing you have to link it rather against the mock library than the original one. You will not need to change the application's code, but it will call functions that will not do anything.
NOTE: This approach may not always work properly, because your application behavior may depend on how and what library functions do things.
UPDATE
Another approach is preserving single library, however make content of each public function to be build conditionally. With the same sample
Foo
class in the original library:When you need to build your application for testing, just build the library with defined
TEST_MODE
preprocessor macro. With this you will just bypass the unnecessary execution of library's code.