Unit test works as standalone but fails in test_that call

旧时模样 提交于 2019-12-02 15:21:58

问题


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

来源:https://stackoverflow.com/questions/32728107/unit-test-works-as-standalone-but-fails-in-test-that-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!