I'd like travis to fail my build if warnings are generated within tests. I thought this was the default, but it doesn't seem to be happening for me. Is there a way to set this up? Here's a build that passes with a warning from this test:
test_that("warnings cause travis to fail", {
odd <- 1:2 * 1:5
expect_equal(length(odd), 5)
})
Travis has the default optionwarnings_are_errors: true. However the warnings referred to here are the WARNING notes from R CMD check, not the r level warnings you get from warning().
As jenny said if there is a particular piece of code you know should not have warnings you can use expect_warning(regexp = NA). Note you can nest expectations, so you could theoretically have one of these expectations around all the exceptions in the test_that() block if you wanted.
If you wanted to fail the tests on any warning in any file what you would have to do is write a custom testthat reporter which caused any warning condition to fail.
Was thinking that maybe a slight variation to @jennybryan reply could be simpler to implement:
library(testthat)
test_that("warnings cause travis to fail", {
expect_warning({
odd <- 1:2 * 1:5
expect_equal(length(odd), 5)
}, NA)
})
#> Error: Test failed: 'warnings cause travis to fail'
#> * `{ ... }` generated warnings:
#> * longer object length is not a multiple of shorter object length
This way you would not have to wrap any possibly "offending" line within a expect_warning call: just wrap the whole test within it (though you would lose reference to the line which triggered the warning). Any other caveats?
But maybe at this point it would be better to open an issue in the testthat repository and we can consider adding a stop_for_warning option to the check reporter.