繁体   English   中英

重新定义捕获单元测试

[英]Redefined Catch unit tests

我正在测试大量软件,并且想使用Catch来完成此任务。 我正在使用“单一包含”版本1.9,将其集成到Visual Studio 2012更新4中并使用C ++ 04标准。

如下所示,我使用三个“ .cpp”文件。 他们每个人参考:

  • 一个包含“抽象”宏的包含文件(例如#define Assert(x) REQUIRE(x) );
  • 提供测试工具的实用程序文件;
  • 具体的测试目标包括文件;
  • 某些“使用命名空间”语句,所有cpp文件“使用”同一命名空间;
  • 用宏编写的实际测试(例如Assert(2 == getNumber()) )。

有关以下文件内容的更多详细信息。

此配置有效,但是其中一个测试文件一天比一天大,我想将其拆分为3个或更多。 现在,说我要执行以下操作:

  • 接受test 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM