32

似乎这应该很容易在詹金斯中集成 CMake+CTest。cmakebuilder插件非常容易配置(只需设置源代码树和构建树,完成!)。但是我不明白如何调用 CTest 步骤。

根据xUnit 主页,从 1.58 版开始支持 CTest 的 XML 输出,请参阅错误报告

这就是我能找到的所有文档。当我在 google 或 stackoverflow 上搜索时,我只能找到需要手动步骤的非常古老的文档。

我想知道如何使用 xUnit (1.81) 设置最近的 jenkins (1.532.1)。我应该创建一个“添加构建步骤”吗?我应该创建一个“构建后操作”吗?我需要填写什么才能让 CTest 运行并生成正确的 XML 文件,以便 jenkins 可以集成它们?

4

3 回答 3

51

这是一个小示例,演示如何让 xUnit 获取 CTest 生成的 XML 测试结果文件。该示例由单个 C++ 源文件组成main.cpp

#include <cstdlib>
int main() {
    std::exit(-1);
}

和随附的CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(JenkinsExample)
enable_testing()
add_executable(main main.cpp)
add_test(test1 main)
add_test(test2 main)
set_tests_properties(test2 PROPERTIES WILL_FAIL ON)

CMake 列表文件会生成一个可执行文件main并从两个 CMake 测试中运行该可执行文件,出于演示目的,其中一个将始终失败,而另一个将始终成功。

使用 Jenkins,设置一个新作业并添加一个新的 cmakebuilder 构建步骤。将 CMake 源目录指向本地计算机上包含示例项目的文件夹。CMake 构建目录应设置为build. 这将使 Jenkins 在作业的工作区目录中创建一个 CMake 构建文件夹。Clean Build设置标志以使作业始终以干净状态开始是个好主意。

然后,假设您正在运行 Unix,添加一个Execute shell作业步骤并在框中输入以下 shell 脚本Command

cd build
/usr/local/bin/ctest --no-compress-output -T Test || /usr/bin/true

ctest使用该选项运行-T Test将使 CTestTesting在构建文件夹内的子文件夹中生成一个 XML 输出文件,然后xUnit插件可以在构建后操作中获取该文件。如果某些测试失败,|| /usr/bin/true则必须防止 Jenkins 过早中止构建(不运行 xUnit 插件)。

如果您使用的是 Windows,请改为设置类似的Execute Windows batch command作业步骤:

cd build
"C:\Program Files (x86)\CMake 2.8\bin\ctest.exe" --no-compress-output -T Test || verify > NUL

最后xUnit必须按以下方式配置插件:

添加Publish xUnit test result report构建后操作,然后使用插件的Add按钮创建CTest-Version测试结果报告。在CTest-Version (default) Pattern输入文件模式build/Testing/**/Test.xml

于 2014-02-10T21:57:25.827 回答
13

对于那些想要在 Jenkins 声明式管道中解析 CTest 输出的人,您现在可以使用xUnit 插件非常轻松地做到这一点,因为它可以直接解析 CTest 输出。

在 build 目录中添加一个阶段Jenkinsfile以运行 ctest,并添加一个post阶段以使用 xUnit 处理输出。这是一个骨架示例:

pipeline {
  agent any
  stages {
    stage('Configure') {
      steps {
        dir('build') {
          sh 'cmake ../'
        }
      }
    }
    stage('Build') {
      steps {
        dir('build') {
          sh 'cmake --build .'
        }
      }
    }  
    stage('Test') {
      steps {
        dir('build') {
          sh 'ctest -T test --no-compress-output'
        }
      }
    }
  }
  post {
    always {
      // Archive the CTest xml output
      archiveArtifacts (
        artifacts: 'build/Testing/**/*.xml',
        fingerprint: true
      )

      // Process the CTest xml output with the xUnit plugin
      xunit (
        testTimeMargin: '3000',
        thresholdMode: 1,
        thresholds: [
          skipped(failureThreshold: '0'),
          failed(failureThreshold: '0')
        ],
      tools: [CTest(
          pattern: 'build/Testing/**/*.xml',
          deleteOutputFiles: true,
          failIfNotNew: false,
          skipNoTestFiles: true,
          stopProcessingIfError: true
        )]
      )

      // Clear the source and build dirs before next run
      deleteDir()
    }
  }
}

有关如何xUnit在声明式管道中配置插件的示例,Jenkins Snippets Generator是一个非常有用的资源。

于 2018-08-28T17:56:34.963 回答
3

由于 CMake 3.21.0 在 CTest 中还有一个新选项:

$ man ctest
[...]
  --output-junit <file>        = Output test results to JUnit XML file.

参考:

于 2021-10-05T13:47:46.643 回答