[英]Redefined Catch unit tests
我正在测试大量软件,并且想使用Catch来完成此任务。 我正在使用“单一包含”版本1.9,将其集成到Visual Studio 2012更新4中并使用C ++ 04标准。
如下所示,我使用三个“ .cpp”文件。 他们每个人参考:
#define Assert(x) REQUIRE(x)
); Assert(2 == getNumber())
)。 有关以下文件内容的更多详细信息。
此配置有效,但是其中一个测试文件一天比一天大,我想将其拆分为3个或更多。 现在,说我要执行以下操作:
test.cpp
的内容的一部分,并将其移动到新文件test2.cpp
; 运行测试时会弹出此错误:
=============================
No tests ran
error: TEST_CASE( "test 2" ) already defined.
First seen at c:\tests\catchtest2\catchtest2\test2.cpp(3)
Redefined at c:\tests\catchtest2\catchtest2\test2.cpp(3)
其中test2.cpp
是新文件。 如果我将内容移回test.cpp
那么所有方法都可以正常工作,但是与测试项目本身相比,处理数千行的测试几乎要困难得多,并且尺寸可能会增长3到4倍。 有没有办法将测试拆分为多个文件?
-注意-
我认为在标头中包含Catch并直接使用catch.cpp
标头而不是catch.cpp
是一个好主意,但是我成功地将此配置与3个(恰好3个)包含的.cpp
测试文件一起使用,并且无法与4个一起使用文件。
我记得读过这篇文章,它与定义组件的行有某种联系,但是我也可以移动代码,以便在不同的行中定义测试用例,并且行为不会改变。
我还尝试了“清理并重建”,因为很可能脏数据被保存在编译器/链接器的缓存中,但是没有用。
我现在无法创建实际的MWE,因此我为您提供了测试设置的草图,其准确程度与我认为可能需要的一样。 我非常愿意提供其他详细信息,或者尝试构建实际的MWE并共享它。
任何想法表示赞赏!
我的“工作”代码如下所示:
main.cpp
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
#include "test1.cpp"
#include "test2.cpp"
#include "test3.cpp"
int main( int argc, char* argv[] )
{
cleanDir("c:\\temp");
init (argv);
int result = Catch::Session().run( argc, argv );
return ( result < 0xff ? result : 0xff );
}
test1.cpp
#include "unitTestSuite.h"
#include "utils.h"
#include "systemundertest.h"
using namespace system::under::test;
my_test_case("test 1") {
my_section("does X") {
// tests...
}
}
my_test_case("test 2") {
my_section("does Y") {
// tests...
}
}
unitTestSuite.h
#ifndef UNIT_TEST_SUITE
#define UNIT_TEST_SUITE 1
#include "catch.hpp"
#define my_test_case(x) TEST_CASE("Testing: " x)
... // here is also a namespace with some unit test specific utils
#endif
实用程序
#ifndef _UTILS_
#define _UTILS_
// some global variables declared here and defined in utils.cpp
// template functions defined in the header
// non-template functions defined in utils.cpp
// a test generation namespace with some template functions and some non-template functions defined in utils.cpp
#endif
之后的“拆分”:
test1.cpp
#include "unitTestSuite.h"
#include "utils.h"
#include "systemundertest.h"
using namespace system::under::test;
my_test_case("test 1") {
my_section("does X") {
// tests...
}
}
test1.2.cpp
#include "unitTestSuite.h"
#include "utils.h"
#include "systemundertest.h"
using namespace system::under::test;
my_test_case("test 2") {
my_section("does Y") {
// tests...
}
}
main.cpp
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
#include "test1.cpp"
#include "test1.2.cpp"
#include "test2.cpp"
#include "test3.cpp"
int main( int argc, char* argv[] )
{
cleanDir("c:\\temp");
init (argv);
int result = Catch::Session().run( argc, argv );
return ( result < 0xff ? result : 0xff );
}
程序输出:
=============================
No tests ran
error: TEST_CASE( "test 2" ) already defined.
First seen at c:\tests\catchtest2\catchtest2\test1.2.cpp(3)
Redefined at c:\tests\catchtest2\catchtest2\test1.2.cpp(3)
不包括cpp文件,只需将它们添加到项目中即可。 您的main.cpp文件仅需要前两行(定义和包含)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.