简体   繁体   中英

Test coverage of print functions in R package

I have a simple print function in an R package:

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).

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:

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; this result is different than capture.output(print.data.frame(mtcars %>% tabyl(am, cyl))) which still has the row numbers.

Using capture.output both tests the function and counts toward coverage on codecov.io's tracking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM