首页 > 解决方案 > 每次都赶上测试框架长链接时间

问题描述

我目前正在尝试使用 Catch 测试框架。我正在使用 cmake 来构建我的项目,目前我只是将所有 .h 和 .c 文件放在一起。出于测试目的,我取出了我的实际“主要”并将其替换为 Catch 的样本阶乘示例。我有两个文件:

// testmain.cpp

#define CATCH_CONFIG_MAIN

#include <catch2/catch.hpp>

//test.cpp

#include "catch2/catch.hpp"


int Factorial( int number ) {
    return number <= 1 ? number : Factorial( number - 1 ) * number;  // fail
  // return number <= 1 ? 1      : Factorial( number - 1 ) * number;  // pass
}

TEST_CASE( "Factorial of 0 is 1 (fail)", "[single-file]" ) {
 REQUIRE( Factorial(0) == 1 );
}

TEST_CASE( "Factorials of 1 and higher are computed (pass)", "[single-file]" ) {
 REQUIRE( Factorial(1) == 1 );
 REQUIRE( Factorial(2) == 2 );
 REQUIRE( Factorial(3) == 6 );
 REQUIRE( Factorial(10) == 3628800 );
}

现在发生的事情是它花费 3 秒构建和 1 分钟链接。在所有链接(1+分钟)之后,我得到了测试结果。我遵循以下两个教程,其中提到将这两个文件分开。

我阅读了 Catch 教程: https ://github.com/catchorg/Catch2/blob/master/docs/tutorial.md

“慢编译”维基页面: https ://github.com/catchorg/Catch2/blob/master/docs/slow-compiles.md

我不太确定为什么链接需要这么长时间。有没有人遇到过这样的问题?

更新:

有关我的环境的更多信息:

标签: c++unit-testingcatch-unit-test

解决方案


因此,从这个已知问题来看:

github.com/catchorg/Catch2/issues/1205

Mingw 在链接时间优化方面真的很糟糕。然而; 我偶然发现了一个适合我的解决方案。将 cmake 构建类型设置为

RELWITHDEBINFO

似乎将链接速度提高了 10 倍。


推荐阅读