【问题标题】:R testthat_result saving in fileR testthat_result 保存在文件中
【发布时间】:2021-04-16 02:29:14
【问题描述】:

我正在尝试保存“testthat”包结果生成的单元测试结果

如何将“testthat_result”对象的测试结果保存到文件(txt 或 csv 文件)中

library(testthat)
result<-test_file('utils_test.R', reporter = "minimal")

outfile<-file("output.txt")
writeLines(c(result), outfile)
close(outfile)

但这不起作用

【问题讨论】:

  • 请考虑分享一些数据和代码。

标签: r testthat


【解决方案1】:

问题是你的结果对象,它是一个列表。

library(testthat)
path <- testthat_example("success")
result<-test_file(path, reporter = "minimal")
str(result)

您需要先将其转换为字符流,然后再使用 writeLines 进行保存。

str(result)
List of 4
 $ :List of 7
  ..$ file   : chr "test-success.R"
  ..$ context: NULL
  ..$ test   : chr "one plus one is two"
  ..$ user   : num 0.006
  ..$ system : num 0
  ..$ real   : num 0.006
  ..$ results:List of 1
  .. ..$ :List of 6
  .. .. ..$ message    : chr "1 + 1 not equal to 2.\nEqual"
  .. .. ..$ srcref     : 'srcref' int [1:8] 2 3 2 24 3 24 2 2
  .. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5589bd9e6a20> 
  .. .. ..$ trace      : NULL
  .. .. ..$ start_frame: int 31
  .. .. ..$ end_frame  : num 32
  .. .. ..$ test       : chr "one plus one is two"
  .. .. ..- attr(*, "class")= chr [1:3] "expectation_success" "expectation" "condition"
 $ :List of 7
  ..$ file   : chr "test-success.R"
  ..$ context: NULL
  ..$ test   : chr "you can skip tests if needed"
  ..$ user   : num 0.003
  ..$ system : num 0
  ..$ real   : num 0.003
  ..$ results:List of 1
  .. ..$ :List of 6
  .. .. ..$ message    : chr "Reason: This tests hasn't been written yet"
  .. .. ..$ srcref     : 'srcref' int [1:8] 6 3 6 44 3 44 6 6
  .. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5589bd9e6a20> 
  .. .. ..$ trace      : NULL
  .. .. ..$ start_frame: int 31
  .. .. ..$ end_frame  : num 31
  .. .. ..$ test       : chr "you can skip tests if needed"
  .. .. ..- attr(*, "class")= chr [1:3] "expectation_skip" "expectation" "condition"
 $ :List of 7
  ..$ file   : chr "test-success.R"
  ..$ context: NULL
  ..$ test   : chr "some tests have warnings"
  ..$ user   : num 0.006
  ..$ system : num 0
  ..$ real   : num 0.007
  ..$ results:List of 2
  .. ..$ :List of 6
  .. .. ..$ message    : chr "NaNs produced"
  .. .. ..$ srcref     : 'srcref' int [1:8] 10 3 10 28 3 28 10 10
  .. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5589bd9e6a20> 
  .. .. ..$ trace      : NULL
  .. .. ..$ start_frame: int 31
  .. .. ..$ end_frame  : num 33
  .. .. ..$ test       : chr "some tests have warnings"
  .. .. ..- attr(*, "class")= chr [1:3] "expectation_warning" "expectation" "condition"
  .. ..$ :List of 6
  .. .. ..$ message    : chr "log(-1) not equal to NaN.\nEqual"
  .. .. ..$ srcref     : 'srcref' int [1:8] 10 3 10 28 3 28 10 10
  .. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5589bd9e6a20> 
  .. .. ..$ trace      : NULL
  .. .. ..$ start_frame: int 31
  .. .. ..$ end_frame  : num 32
  .. .. ..$ test       : chr "some tests have warnings"
  .. .. ..- attr(*, "class")= chr [1:3] "expectation_success" "expectation" "condition"
 $ :List of 7
  ..$ file   : chr "test-success.R"
  ..$ context: NULL
  ..$ test   : chr "some more successes just to pad things out"
  ..$ user   : num 0.003
  ..$ system : num 0
  ..$ real   : num 0.003
  ..$ results:List of 2
  .. ..$ :List of 6
  .. .. ..$ message    : chr "TRUE isn't true."
  .. .. ..$ srcref     : 'srcref' int [1:8] 14 3 14 19 3 19 14 14
  .. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5589bd9e6a20> 
  .. .. ..$ trace      : NULL
  .. .. ..$ start_frame: int 31
  .. .. ..$ end_frame  : num 32
  .. .. ..$ test       : chr "some more successes just to pad things out"
  .. .. ..- attr(*, "class")= chr [1:3] "expectation_success" "expectation" "condition"
  .. ..$ :List of 6
  .. .. ..$ message    : chr "FALSE isn't false."
  .. .. ..$ srcref     : 'srcref' int [1:8] 15 3 15 21 3 21 15 15
  .. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5589bd9e6a20> 
  .. .. ..$ trace      : NULL
  .. .. ..$ start_frame: int 31
  .. .. ..$ end_frame  : num 32
  .. .. ..$ test       : chr "some more successes just to pad things out"
  .. .. ..- attr(*, "class")= chr [1:3] "expectation_success" "expectation" "condition"
 - attr(*, "class")= chr "testthat_results"

【讨论】:

  • 感谢 Saurabh,但我无法在此处找到特定测试用例的 PASS 或 FAIL 标志。如果可能的话,请通知这个标志
  • 我认为您正在寻找 tryCatch 语句。尝试将 test_file 函数包含在 tryCatch 块中。这是一个关于如何做到这一点的例子 - stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r
  • HI Saurabh,我只是在寻找通过和失败的测试数量以及测试名称,我想将其保存在文本文件中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 2012-02-09
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多