【问题标题】:Unit tests. How to run tests in the main()单元测试。如何在 main() 中运行测试
【发布时间】:2019-05-04 13:30:27
【问题描述】:

我试图在main函数中运行一个测试,但是显示错误“你不能重载main()函数”。

#define CATCH_CONFIG_RUNNER // -- main() создавать нужно --
#include "catch.hpp"
int main(int argc, char* argv[])
{
    setlocale(LC_ALL, "Russian");
    int result = Catch::Session().run(argc, argv);
    system("pause");
    return result;
}

【问题讨论】:

标签: c++ catch2


【解决方案1】:

您应该以其他方式使用 Catch。类似的东西对我有用:

#include <iostream> // some standard includes, whatever you need

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

TEST_CASE("My first test") {
    // --- test code here ---
}

TEST_CASE("My second test") {
    // --- test code here ---
}

试试框架的tutorial 了解更多 =)

【讨论】:

  • 运行此代码后,控制台窗口迅速消失。所以我试图通过 main 函数运行它。
  • 这看起来像是您的 IDE 的问题。你用什么编辑器来运行程序?在您的 IDE 中找到一个选项,例如“使控制台窗口在程序终止后持续存在”。
  • 这个答案和教程没有解释为什么他没有使用自己的main(),就像他应该能够根据文档的这一部分:github.com/catchorg/Catch2/blob/master/docs/own-main.md#top
  • 从问题中获取catch.hpp + exact 代码。工作得很好。我的意思是,如果他遇到main() 重载的问题,他可以先尝试一种更简单的方法,正如@KamilCuk 正确所说,控制台窗口关闭更多的是关于 IDE 配置而不是框架使用。
猜你喜欢
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 2018-01-12
  • 1970-01-01
  • 2020-01-11
  • 2011-02-10
  • 1970-01-01
  • 2021-09-25
相关资源
最近更新 更多