[英]Test coverage of print functions in R package
I have a simple print function in an R package: 我在R包中有一个简单的打印功能:
print.tabyl <- function(x){
print.data.frame(x, row.names = FALSE)
}
I'm trying to achieve full test coverage of my package and it annoys me that my untested print function lowers my test coverage to 99% (it would be 100% otherwise). 我正在努力实现我的软件包的全面测试覆盖,让我感到恼火的是,我未经测试的打印功能将我的测试覆盖率降低到99%(否则将是100%)。
But I can't figure out how to capture of the output of the print function in order to write a test for it. 但我无法弄清楚如何捕获打印功能的输出,以便为它编写测试。 How can I write a test for a print function?
如何为打印功能编写测试?
Per the suggestion from @alistaire, I used capture.output
to write a test: 根据@alistaire的建议,我使用
capture.output
编写测试:
test_that("print.tabyl prints without row numbers", {
expect_equal(
mtcars %>% tabyl(am, cyl) %>% capture.output(),
c(" am 4 6 8", " 0 3 4 12", " 1 8 3 2")
)
})
This captures that the row.names
are missing; 这捕获了缺少
row.names
的内容; this result is different than capture.output(print.data.frame(mtcars %>% tabyl(am, cyl)))
which still has the row numbers. 此结果与
capture.output(print.data.frame(mtcars %>% tabyl(am, cyl)))
,后者仍具有行号。
Using capture.output
both tests the function and counts toward coverage on codecov.io's tracking. 使用
capture.output
测试函数并计算codecov.io的跟踪覆盖率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.