首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R testthat_result保存在文件中

R testthat_result保存在文件中
EN

Stack Overflow用户
提问于 2021-01-11 13:48:50
回答 1查看 117关注 0票数 0

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

如何将测试结果保存为testthat_result对象文件(txt或csv文件)

代码语言:javascript
复制
library(testthat)
result<-test_file('utils_test.R', reporter = "minimal")

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

但这并不管用

EN

回答 1

Stack Overflow用户

发布于 2021-01-11 14:30:47

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

代码语言:javascript
复制
library(testthat)
path <- testthat_example("success")
result<-test_file(path, reporter = "minimal")
str(result)

在使用writeLines保存之前,您需要将其转换为字符流。

代码语言:javascript
复制
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"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65661911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档