Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
917 views
in Technique[技术] by (71.8m points)

testthat - R testthat_result saving in file

I am trying to save unit tests result generated by "testthat" package result

How to save testthat result which is "testthat_result" object into a file(txt or csv file)

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

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

But this is not working

question from:https://stackoverflow.com/questions/65661911/r-testthat-result-saving-in-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is your result object, which is a list.

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

You need to convert it to a character stream before saving it using 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.
Equal"
  .. .. ..$ 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.
Equal"
  .. .. ..$ 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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...