测试软件QuickCheck有R接口吗?

Is there an R port of the testing software QuickCheck?

现在我发现自己花了很多时间在 R 中编程,我真的很想回到自动化测试(这是我在 Perl 中习惯做的)。除了对用户友好之外,我还对能够为像 Perl 的 Test::LectroTest or Haskell's QuickCheck 这样的测试生成随机输入特别感兴趣。 R 有类似的东西吗?

查看 R 包 quickcheck on GitHub

Test::LectroTest 一样,R 程序包 quickcheckQuickCheck 的一个端口,Koen Claessen 和 John Hughes 为 Haskell 编写了该程序包。

除了 QuickCheck 功能外,quickcheck 还向 Hadley Wickam 广受欢迎的 testthat R 包致敬,通过有意合并他的 "expectation" 函数(他们称之为 "assertions").除了数字和字符串测试之外,还有针对失败和警告等的测试

这是一个使用它的简单示例:

library(quickcheck)

my_square <- function(x){x^2}        # the function to test

test( function(x = rinteger())  min(my_square(x)) >= 0 )
# Pass  function (x = rinteger())  
#  min(my_square(x)) >= 0 
# [1] TRUE

test( function(x = rdouble())
      all.equal(
                my_square(x),
                x^2
      )
)
# Pass  function (x = rdouble())  
#  all.equal(my_square(x), x^2) 
# [1] TRUE

第一个测试确保 my_square 生成的任何内容都是肯定的。第二个测试实际上复制了 my_square 的功能并检查每个输出以确保它是正确的。

请注意,rinteger() 生成由整数值组成的任意长度的向量。其他随机生成的输入数据可以使用 rcharacterrdoublermatrix.

等函数生成