[R-pkg-devel] Unit test functions as standalone block of code but not in test_that()

Shaub, David DAVID.SHAUB at dish.com
Wed Sep 23 19:37:47 CEST 2015


I'm creating 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 assigned 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

David Shaub
Business Operations Analyst II
In-Home Services |Dish Network L.L.C
IHS Command Center | Forecasting Team
David.Shaub at dish.com<mailto:gavin.redelman at dish.com>



	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list