Reputation: 23014
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?
Upvotes: 5
Views: 353
Reputation: 23014
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.
Upvotes: 2