【问题标题】:Testthat does not see external csv data没有看到外部 csv 数据的测试
【发布时间】:2021-01-07 10:23:55
【问题描述】:

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

【问题讨论】:

    标签: r unit-testing testthat


    【解决方案1】:

    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 年 9 月 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 
    

    【讨论】:

    • 但是从包级别运行测试我们会得到包含通过测试数量的摘要。例如。确定:3 失败:.. 警告:... 跳过:...
    • 结果和在包级别开启测试是一样的
    • 在我刚刚编辑的示例中,它显示一个错误,一个通过:这不是预期的吗?
    • @John,这回答了你的问题还是我误解了你的问题?感谢您的反馈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 2014-04-11
    • 2011-03-25
    • 1970-01-01
    相关资源
    最近更新 更多