r - 如何使用 R testthat 生成 jUnit fie

标签 r jenkins junit testthat

我有 R 代码(不是包),我想使用 testthat 进行验收测试,并在 Jenkins 中使用输出。

我可以从两个演示代码结构的文件开始:

# -- test.R
source("test-mulitplication.R")

# -- test-mulitplication.R
library(testthat)
test_that("Multipilation works ", {
  res <- 5 * 2
  expect_equal(res, 10)
})

运行后,我想获取一个 xml 文件,其中包含每个测试文件或单个文件中的所有测试的结果。

我注意到 testthat 中有一个 reporter 功能,但其中大部分似乎是包内部的。目前尚不清楚如何保存测试结果以及该功能的灵 active 如何。

不幸的是,该部分的文档并不完整。

编辑

我现在找到了一种使用更好的语法和 junit 输出选项来测试目录的方法:

# -- tests/accpetance-tests.R
options(testthat.junit.output_file = "test-out.xml")
test_dir("tests/")

# -- tests/test-mulitplication.R
library(testthat)
test_that("Multipilation works ", {
  res <- 5 * 2
  expect_equal(res, 10)
})

我相信这会在报告器内生成一个 XML 对象,但我仍然不知道如何将其保存到文件中。

我尝试用 with_reporter 包装 test_dir 调用,但这并没有多大作用。

最佳答案

2019 年更新:

testthat 版本 2.1.0 不再需要context 才能正常工作。因此,我希望您的原始代码中的问题能够正常工作。

来源:https://www.tidyverse.org/articles/2019/04/testthat-2-1-0/

原始答案:

testthat commit 4 days ago它引用了这个功能。 testthat 的开发版本中引入了一个新选项。

如果你运行:

devtools::install_github("r-lib/testthat")
options(testthat.output_file = "test-out.xml")
test_dir("tests/")

这应该在您的工作目录中生成一个文件。

问题是它可能不适用于您想要的记者。安装了 testthat 的 devtools 版本:

options(testthat.output_file = "test-out.xml")
test_dir("tests/", reporter = "junit")

产生有关 xml2 的错误。尝试 xml2 的 dev 分支没有解决问题。鉴于此更改是最近发生的,它可能值得 filing an issue over on github.

不确定这是否会让您更接近,但我们正在输出一份报告,这是一个开始!

编辑

这可行,但您需要确保在测试顶部添加一个“上下文”,否则您会收到错误。尝试将乘法测试的顶部更改为:

# -- test-mulitplication.R
library(testthat)
context("Testing Succeeded!")
test_that("Multipilation works ", {
  res <- 5 * 2
  expect_equal(res, 10)
})

context("Test Failed!")
test_that("Multipilation works ", {
  res <- 5 * 2
  expect_equal(res, 12)
})

然后重新运行:

options(testthat.output_file = "test-out.xml")
test_dir("tests/", reporter = "junit")

这对我有用!由于某种原因,不包含上下文 header 会导致问题。但这可能是设计使然。

关于r - 如何使用 R testthat 生成 jUnit fie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46655071/

相关文章:

r - 计算每个因素按另一个因素分组的数量

java - 使用 java 的 R arules 包中的问题

c++ - Rcpp 创建具有可变列数的 DataFrame

oop - is.object 和 S3 类系统

git - 使用 Jenkins Git Publisher 推送标签 - 多个 Git 存储库(fork)

jenkins - 如何从 Jenkins 上的 mstest 中排除使用 "/Category"的测试

jenkins - 如何在 Jenkins 声明式管道的阶段中对同一函数进行并行调用

java - 在 Junit-4.11 中找不到类

java - 如何编写一个 JUnit 运行程序,在 @Category 中的 TestCase 之前运行 @BeforeCategory 方法?

java - 如何在java中使用注释相关参数检查自定义运行时异常属性