如何使用R测试生成jUnit fie(How to produce jUnit fie with R testthat)

编程入门 行业动态 更新时间:2024-10-12 10:26:11
如何使用R测试生成jUnit fie(How to produce jUnit fie with R testthat)

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

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

# -- 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功能,但大部分内容似乎都在内部。 目前尚不清楚如何保存测试结果以及功能的灵活性。

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

编辑

我现在找到了一种方法来测试目录,使用更好的语法和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调用,但这并没有太大作用。

I have R code (not a package) which I would like to cover with acceptance tests using testthat with output being used in Jenkins.

I can start with two files which demonstrate the code structure:

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

After the run I would like to get an xml file with results per test file or all tests in a single file.

I noticed there is a reporter capability within testthat, but most of it seems to be internal to the package. It is not clear how to save test results and how flexible is the functionality.

Unfortunately documentation on that part is not complete.

EDIT

I have now found a way to test the directory with better syntax and option for junit output:

# -- 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) })

This I believe produces an XML object inside the reporter, but I still don't see how to save it to a file.

I tried to wrap the test_dir call with with_reporter, but that does not do much.

最满意答案

有4天前提交的testthat引用了这个功能。 testthat的开发版本中引入了一个新选项。

如果您运行:

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

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

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

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

产生关于xml2的错误。 尝试xml2的dev分支并没有解决问题。 鉴于此更改是相当近期的,可能值得在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")

这对我有用! 由于某些原因,不包括上下文标题会导致问题。 这可能是设计的。

Update 2019:

Version 2.1.0 of testthat no longer needs context in order to work correctly. So, I would expect the question from your original code to work correctly.

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

Original Answer:

There was testthat commit 4 days ago which references this functionality. A new option is being introduced in the development version of testthat.

If you run:

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

this should produce an file in your working directory.

The catch is it may not work with the reporter you want. With the devtools version of testthat installed:

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

produces an error regarding xml2. Trying the dev branch of xml2 did not resolve the issue. Given that this change is fairly recent, it might be worth filing an issue over on github.

Not sure if this gets you any closer, but we're getting a report to output which is a start!

EDIT

This works, but you need to be sure and add a "context" to the top of your test or else you'll get an error. Try changing the top of your multiplication test to something like:

# -- 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) })

and then re-run:

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

that worked for me! For some reason not including a context header causes problems. This is probably by design though.

更多推荐

本文发布于:2023-08-05 23:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440917.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   测试   jUnit   testthat   produce

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!