如何使用 CUnit 测试具有多个组件的本机应用程序

How to use CUnit to test a native application with multiple components

我在 gradle(版本 2.10)中有一个 gradle 原生 c 应用程序项目,由多个组件组成:

components {

   component_1(NativeLibrarySpec)
   component_2(NativeLibrarySpec)
   ...
   component_n(NativeLibrarySpec)

   main_component(NativeExecutableSpec){
       sources {
            c.lib library: "component_1", linkage: "static"
            c.lib library: "component_2", linkage: "static"
            ...
            c.lib library: "component_n", linkage: "static"
        }

   }
}

采用这种形式的应用程序的主要想法是更容易测试各个组件。但是,我有两个大问题:

main_component 是一个应用程序,因此具有主要功能,这会生成此错误:multiple definition of 'main' ... gradle_cunit_main.c:(.text+0x0): first defined here。这是意料之中的事情,并且在文档中有所提及,但我想知道是否有办法避免这个问题,例如,防止将主要组件包含在测试中。这与下一个问题有关

CUnit 插件(从 gradle 的 2.10 版开始)在我尝试按如下方式定义要测试的组件时抱怨:

testSuites {
    component_1Test(CUnitTestSuiteSpec){
            testing $.components.component_1
        }
   }
}

gradle抱怨:

Cannot create 'testSuites.component_1Test' using creation rule 'component_1Test(org.gradle.nativeplatform.test.cunit.CUnitTestSuiteSpec) { ... } @ build.gradle line 68, column 9' as the rule 'CUnitPlugin.Rules#createCUnitTestSuitePerComponent > create(component_1Test)' is already registered to create this model element

综上所述,我想指示cunit插件只测试部分组件,并防止编译主要组件(具有主要功能)进行测试。再次请注意,我使用的是 gradle 2.10,无法升级到更新的版本,因为那样会破坏我们的 CI 工具链。

提前感谢您的意见。

以下是如何构造项目以允许对组件进行单元测试,但阻止对主程序进行单元测试:将组件拆分为子项目并将 cunit 插件应用于子项目,如下图:

project(':libs'){
apply plugin: 'c'
apply plugin: 'cunit'
model{

    repositories {
        libs(PrebuiltLibraries) {
            cunit {
                headers.srcDir "/usr/include/"
                    binaries.withType(StaticLibraryBinary) {
                        staticLibraryFile = file("/usr/lib/x86_64-linux-gnu/libcunit.a")
                    }
            }
        }
    }

    components {
        component_1(NativeLibrarySpec)
        component_2(NativeLibrarySpec)
        ...
        component_n(NativeLibrarySpec)
    }

    binaries {
        withType(CUnitTestSuiteBinarySpec) {
            lib library: "cunit", linkage: "static"
        }
    }
}
}
apply plugin: 'c'
model {
    components{
        myprogram(NativeExecutableSpec) {
            sources.c {
                lib project: ':libs', library: 'component_1', linkage: 'static'
                lib project: ':libs', library: 'component_2', linkage: 'static'
                lib project: ':libs', library: "component_n", linkage: 'static'
                source {
                     srcDir "src/myprogram/c"
                          include "**/*.c"
                }
            }
        }
    }
}