测试看不到外部CSV数据

时间:2020-09-21 10:17:59

标签: r unit-testing testthat

我正在为我的包裹编写测试,我想使用宏观经济数据,而不是人为地随机分配。问题是,当我使用read.csv('my_file.csv')然后运行test_that时,所有使用我的数据的测试都将被忽略。例如

library('tseries')
library('testthat')
data<-read.csv('my_file.csv')
test_that('ADF test',{
    vec<-data[,2]
    expect_is(adf.test(vec),'htest')
})

运行“ testpackage”后,我未获得有关测试失败或通过的信息。问题出在哪里?

1 个答案:

答案 0 :(得分:0)

testthat仅在测试未成功的情况下在控制台中返回错误:

library(testthat)
data<-iris
test_that('test1',{
  expect_is(data$Petal.Length,'numeric')
})

test_that('test2',{
  expect_is(data$Species,'numeric')
})
#> Error: Test failed: 'test2'
#> * <text>:8: data$Species inherits from `factor` not `numeric`.

reprex package(v0.3.0)于2020-09-21创建

您可以使用test_filetest_dir来获得结果:

res <- test_file('mytest.R',
                 reporter = "list",
                 env = test_env(),
                 start_end_reporter = TRUE,
                 load_helpers = TRUE,
                 wrap = TRUE)

√ |  OK F W S | Context
x |   1 1     | mytest
--------------------------------------------------------------------------------
mytest.R:8: failure: test2
data$Species inherits from `factor` not `numeric`.
--------------------------------------------------------------------------------

== Results =====================================================================
OK:       1
Failed:   1
Warnings: 0
Skipped:  0
Warning message:
`encoding` is deprecated; all files now assumed to be UTF-8