I'm new to unit testing in C++ and want to get some advice on this. I use Visual Studio 2019 for development and I chose Catch2 as my testing library, I also got the Test Adapter for Catch2 installed.
I read docs for both Catch2 and Test Adapter for Catch2 on GitHub, but I still cannot figure out a proper way to use unit test in Visual Studio.
Let's assume that I already have a project with some classes in it and I want to test those classes. Should I put files with test code in the same project or should I create new test projects within the solution?
When I try the first approach, Test Explorer doesn't discover tests unless I comment out the main() function of the project. With second approach, I get a bunch of unresolved external symbol errors for my classes' methods, although I set correct relative paths to header files and referenced the main project from the test project:
LNK2019 unresolved external symbol "public: bool __thiscall MyClass::Check(int,int)" (?Check@MyClass@@QAE_NHH@Z) referenced in function "void __cdecl ____C_A_T_C_H____T_E_S_T____0(void)" (?____C_A_T_C_H____T_E_S_T____0@@YAXXZ)
I would appreciate if someone show me a correct way to do unit testing with Catch2 in VS.
OK, I guess I found a suitable workflow to make Catch2 test work in Visual Studio 2019:
#define CATCH_CONFIG_MAIN
#include "path_to_catch2/catch.hpp"
#define CATCH_CONFIG_MAIN
declaration.
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<Catch2Adapter>
<FilenameFilter>^Test_</FilenameFilter>
</Catch2Adapter>
</RunSettings>
where <FilenameFilter>
controls filter for test projects filenames. In this particular example, tests will be discovered only in projects which names start with "Test_".
Now you should see your tests in Test Explorer.
Some useful links:
https://github.com/JohnnyHendriks/TestAdapter_Catch2/blob/master/Docs/Walkthrough.md