测试看不到外部CSV数据

问题描述

我正在为我的包裹编写测试,我想使用宏观经济数据,而不是人为地随机分配。问题是,当我使用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”后,我未获得有关测试失败或通过的信息。问题出在哪里?

解决方法

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 

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...