在 Visual Studio 中使用 Catch2 进行单元测试的最佳实践 [英] Best practices for Unit testing with Catch2 in Visual Studio

查看:23
本文介绍了在 Visual Studio 中使用 Catch2 进行单元测试的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C++ 单元测试的新手,想就此获得一些建议.我使用 Visual Studio 2019 进行开发,我选择 Catch2 作为我的测试库,我还安装了 Catch2 的测试适配器.

我在 GitHub 上阅读了 Catch2 和 Catch2 测试适配器的文档,但我仍然无法找到在 Visual Studio 中使用单元测试的正确方法.

假设我已经有一个包含一些类的项目,我想测试这些类.我应该将带有测试代码的文件放在同一个项目中,还是应该在解决方案中创建新的测试项目?

当我尝试第一种方法时,除非我注释掉项目的 main() 函数,否则测试资源管理器不会发现测试.使用第二种方法,虽然我设置了头文件的正确相对路径并从测试项目中引用了主项目,但我的类方法会遇到一堆未解决的外部符号错误:

LNK2019 未解析的外部符号 "public: bool __thiscall MyClass::Check(int,int)" (?Check@MyClass@@QAE_NHH@Z) 引用在函数 "void __cdecl ____C_A_T_C_H____T_E_S_T____0(void)" (?_____C__T_S_T____C__T_A@@YAXXZ)

如果有人向我展示在 VS 中使用 Catch2 进行单元测试的正确方法,我将不胜感激.

解决方案

好吧,我想我找到了一个合适的工作流来使 Catch2 测试在 Visual Studio 2019 中工作:

  1. 使用被测项目 (PuT) 的解决方案创建一个新项目.这将是我们的测试项目.
  2. 从测试项目中添加对 PuT 的引用.
  3. 在 Test 项目中,创建一个源文件并将以下几行放入其中:

#define CATCH_CONFIG_MAIN#include "path_to_catch2/catch.hpp"

  1. 编写一些测试.您可以根据需要拥有任意多个带有测试的源文件,但请记住,其中只有一个必须具有 #define CATCH_CONFIG_MAIN 声明.
  2. 在测试项目配置属性中,设置以下设置:
    • 链接器 -> 常规 -> 附加库目录 - 在此处添加指向 PuT 目标文件目录的路径.
    • Linker -> Input -> Additional Dependencies - 在这里,放置 PuT 的目标文件名称(不是路径,只是 .OBJ 文件的名称!),用分号分隔,用于测试.例如,如果您要测试在 MyCode.h 中声明的某些代码,请在此处输入 MyCode.obj 文件名.当您从 PuT 中引用更多头文件时,请不要忘记更新此设置.
  3. 打开测试资源管理器.
  4. 将 .runsettings 文件添加到解决方案根文件夹.最小配置如下所示:

<预><代码><?xml version="1.0" encoding="utf-8"?><运行设置><Catch2Adapter><文件名过滤器>^测试_</Catch2Adapter></运行设置>

where 控制测试项目文件名的过滤器.在此特定示例中,只会在名称以Test_"开头的项目中发现测试.

  1. 测试 -> 配置运行设置 -> 选择解决方案范围的运行设置文件中选择此 .runsettings 文件.您可以拥有多个不同配置的 .runsettings 文件,在这里您可以随时切换它们.
  2. 重新构建解决方案以发现测试.

现在您应该可以在测试资源管理器中看到您的测试.

<小时>

一些有用的链接:

https://docs.microsoft.com/en-us/visualstudio/test/how-to-use-microsoft-test-framework-for-cpp?view=vs-2019

https://github.com/JohnnyHendriks/TestAdapter_Catch2/blob/master/Docs/Walkthrough.md

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:

  1. Create a new project withing the solution of the Project under test (PuT). This will be our Test project.
  2. Add a reference to PuT from the Test project.
  3. In the Test project, create a source file and put the following lines in it:

#define CATCH_CONFIG_MAIN
#include "path_to_catch2/catch.hpp"

  1. Write some tests. You can have as many source files with tests as you want, but remember that only one of them must have the #define CATCH_CONFIG_MAIN declaration.
  2. In Test project Configuration Properties, set following settings:
    • Linker -> General -> Additional Library Directories - add here a path to object files directory of the PuT.
    • Linker -> Input -> Additional Dependencies - here, put object files names of the PuT (not paths, just the names of .OBJ files!), separated by semicolon, which were used in tests. For example, if you want to test some code declared in MyCode.h, put MyCode.obj file name here. When you reference more header files from the PuT, don't forget to update this setting.
  3. Open Test Explorer.
  4. Add a .runsettings file with to the solution root folder. Minimal config looks like this:


    <?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_".

  1. Select this .runsettings file in Test -> Configure Run Settings -> Select Solution Wide runsettings File. You can have multiple .runsettings file with different configuration, here you can switch them at any moment.
  2. Rebuild the solution to discover tests.

Now you should see your tests in Test Explorer.


Some useful links:

https://docs.microsoft.com/en-us/visualstudio/test/how-to-use-microsoft-test-framework-for-cpp?view=vs-2019

https://github.com/JohnnyHendriks/TestAdapter_Catch2/blob/master/Docs/Walkthrough.md

这篇关于在 Visual Studio 中使用 Catch2 进行单元测试的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆