Gradle 如何使用CUnit测试具有多个组件的本机应用程序
我有一个gradle项目,用于gradle(2.10版)中的本机c应用程序,由多个组件组成:Gradle 如何使用CUnit测试具有多个组件的本机应用程序,gradle,cunit,Gradle,Cunit,我有一个gradle项目,用于gradle(2.10版)中的本机c应用程序,由多个组件组成: components { component_1(NativeLibrarySpec) component_2(NativeLibrarySpec) ... component_n(NativeLibrarySpec) main_component(NativeExecutableSpec){ sources { c.lib lib
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\u组件是一个应用程序,因此有一个main函数,这会产生以下错误:对“main”的多个定义
gradle\u cunit\u main.c:(.text+0x0):首先在此处定义
。这是可以预料的,并且在文档中提到过,但是我想知道是否有办法避免这个问题,例如,防止在测试中包含主组件。这与下一个问题有关
CUnit插件(从gradle的2.10版开始)在我试图定义要测试的组件时抱怨如下:
testSuites {
component_1Test(CUnitTestSuiteSpec){
testing $.components.component_1
}
}
}
格雷德尔抱怨说:
无法使用创建规则创建“testSuites.component_1Test”
'组件测试(org.gradle.nativeplatform.test.cunit.cunitestsuitespec)
{…}@build.gradle第68行第9列作为规则
“CUnitPlugin.Rules#CreateCUnitTestSuitepComponent>
已注册“创建(组件测试)”以创建此模型
元素
总之,我想指示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"
}
}
}
}
}