Unit test works as standalone but fails in test_th

2019-03-03 10:10发布

问题:

I’m running unit tests on the forecast package, specifically for the forecast.ar() function. I have a block of code that works just fine when run on its own.

require(testthat)
require(forecast)
set.seed(55)
simseries <- stats::arima.sim(model = list(ar = c(-0.42, 0.832, 0.1, -0.34)), n = 400)
arfit <- stats::ar(simseries)
fc1 <- forecast(arfit)$mean
fc2 <- forecast(arfit, bootstrap = TRUE, npaths = 100, fan = TRUE)$mean
expect_true(all(fc1 == fc2))

However, when I embed this in a test_that() function (after starting a new clean R session), I get an error that the ts is empty. I’ve determined that the error occurs on the two lines generating fc1 and fc2; simseries is saved correctly.

require(testthat)
require(forecast)
test_that("tests for forecast.ar", {
set.seed(55)
simseries <- stats::arima.sim(model = list(ar = c(-0.42, 0.832, 0.1, -0.34)), n = 400)
arfit <- stats::ar(simseries)
fc1 <- forecast(arfit)$mean
fc2 <- forecast(arfit, bootstrap = TRUE, npaths = 100, fan = TRUE)$mean
expect_true(all(fc1 == fc2))
})

Why is this happening? I thought it might be a namespace issue with using the ar and arima.sim functions from the stats package, so I included the stats namespace in the function call, but that does not fix the problem.

R 3.1.1 forecast 6.1 testthat 0.10.0

标签: r testthat