Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cgreen中的多个descripe()_C_Unit Testing_Gcc_Tdd_Bdd - Fatal编程技术网

cgreen中的多个descripe()

cgreen中的多个descripe(),c,unit-testing,gcc,tdd,bdd,C,Unit Testing,Gcc,Tdd,Bdd,我正在为我的C代码编写测试,我的问题是: 短版: 是否可以在一个文件中放置多个descripe() 长版本: 我有不同的测试文件,它们有自己的descripe(),beforeach()和AfterEach()。因为在这些文件中,我使用了相同的模块,所以我必须一起编译它们。[为了防止编译器多次包含模块,我使用了以下技巧(include guard) #如果没有某些名称 #定义一些你的名字 ... #恩迪夫 我还有一个包含我所有测试的文件,比如说all\u tests.c。现在,我想将我的测试文

我正在为我的C代码编写测试,我的问题是:

短版: 是否可以在一个文件中放置多个
descripe()

长版本: 我有不同的测试文件,它们有自己的
descripe()
beforeach()
AfterEach()
。因为在这些文件中,我使用了相同的模块,所以我必须一起编译它们。[为了防止编译器多次包含模块,我使用了以下技巧(include guard)

#如果没有某些名称
#定义一些你的名字
...
#恩迪夫
我还有一个包含我所有测试的文件,比如说
all\u tests.c
。现在,我想将我的测试文件包含到
all\u tests.c
中,并将所有内容一起编译,如下所示:

#包括
#包括“/test1.c”
#包括“/test2.c”
int main(){
TestSuite*suite=创建测试套件();
添加_套件(套件,test1_tests());
添加_套件(套件,test2_tests());
返回run_test_suite(suite,create_text_reporter());
}
这导致了这些错误:

error: redefinition of ‘setup’
error: redefinition of ‘teardown’
显然,因为有多个定义
descripe()
beforeach()
AfterEach()
。好吧,我找不到更好的方法,使用推荐的

TestSuite*test1_tests();
TestSuite*test2_测试();
不是直接包含文件,而是单独组装和编译每个测试文件,然后将所有文件链接在一起,从而导致此错误:

multiple definition of `some_global_module'
这是预期的,在链接状态下,
some_global_module
有多个定义。为了清楚起见,
test1.c
文件如下所示(还有
test2.c
文件,只需在以下代码中将
test1
更改为
test2
):

#包括
#包括“/some\u global\u module.h”
#包括“/some\u other\u module\u我希望\u测试1.h”
描述(test1);
每次测试前(测试1){
...
}
每次之后(测试1){
...
}
确保(测试1,做点什么){
...
}
TestSuite*test1_测试(){
TestSuite*suite=创建测试套件();
添加带有上下文的测试(套件、测试1、做某事);
返回套房;
}

有什么想法吗?也许我可以使用一些编译器技巧或更简单的方法来管理整个过程?

没有办法在同一个测试文件中包含多个
descripe()
。这应该是不必要的

您没有确切解释如何组装测试/模块。有几个选项:

  • #在测试文件中包含
    您的模块源代码-这是可能的,但显然您无法将多个测试文件链接在一起,您需要单独运行它们,因为它们为被测模块中的所有模块定义了相同的符号

  • 让测试“模块”以与其他用户相同的方式使用您的被测模块(
    \\35;包括
    和调用公共接口)。在这种情况下,您的测试文件与任何其他模块一样都是模块,应该与被测模块的单个实例链接在一起

  • 使用
    cgreen runner
    。它加载共享库并发现库中的所有测试,使您不再需要记住将每个新测试用例添加到套件中

  • 仅当您将测试改装为现有的、不可测试的代码,或者确实需要测试内部组件(如
    静态
    函数)时,才使用#1

    #2是非常标准的,并且可能是您应该做的。在这种情况下,从您的测试文件中删除任何
    #包括“some_global_module.c”
    ,就像您在
    test1.c
    中所做的那样。您的
    Makefile
    应该如下所示:

    all: all_tests
    
    all_tests: all_tests.o some_global_module.o \
        test1.o some_global_module_under_test1.o \
        test2.o some_global_module_under_test2.o
    
    all: all_tests.so
        cgreen-runner ./$^ 
    
    all_tests.so: some_global_module.o \
            test1.o some_global_module_under_test1.o \
            test2.o some_global_module_under_test2.o
        $(LINK) -shared -o $@ $^
    
    因此,由于
    test1.c
    test2.c
    看起来像您所指出的,以及上面的Makefile内容,我无法理解为什么您应该在链接时获得“多个定义”

    #3是我最喜欢的。将所有内容链接到共享对象库(
    .so
    在Linux上)并在其上运行
    cgreen runner
    。然后,您的
    Makefile
    应该如下所示:

    all: all_tests
    
    all_tests: all_tests.o some_global_module.o \
        test1.o some_global_module_under_test1.o \
        test2.o some_global_module_under_test2.o
    
    all: all_tests.so
        cgreen-runner ./$^ 
    
    all_tests.so: some_global_module.o \
            test1.o some_global_module_under_test1.o \
            test2.o some_global_module_under_test2.o
        $(LINK) -shared -o $@ $^
    

    注意:如果你的
    测试1下的某个\u全局\u模块依赖于许多其他模块,你要么也需要链接它们,要么模仿它们。

    非常感谢Thomas。这是一个老问题,当时很遗憾,但我应该说我不了解c编译器的基础知识,这导致了多定义错误。希望你你的答案帮助其他人以正确的方式设置他们的考试结构。干杯