作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在 RUnit 中自动生成测试用例?
例如,假设我有一个简单的 sum() 函数:
sum <- function(x, y) {
return (x + y)
}
我想在一系列不同的测试用例上测试这个函数:
test_cases <- c( c(2, 2, 4),
c(3, 3, 6),
c(0, 0, 0),
c(-1, 2, 1)
)
每个向量的前两个元素是 x 和 y,第三个是 sum(x,y) 函数的预期输出。
在 python 中,我可以轻松编写一个函数,为 test_cases 中的每个元素生成一个测试用例,但我不知道如何在 R 中实现它。我查看了 RUnit 和 testthat 文档,但是有没有什么相似之处。这里最好的解决方案是什么?
这就是我用 python 编写它的方式(使用 nosetest 启动测试单元):
for triplet in test_cases:
yield test_triplet(triplet)
def test_triplet(triplet):
assert(sum(triplet[0], triplet[1]) == triplet[2])
最佳答案
# You simply take advantage of R's vector orientation.
test_cases <- matrix(c(2, 2, 4,
3, 3, 6,
0, 0, 0,
-1, 2, 1), ncol = 3, byrow = TRUE)
my_sum <- function(x, y) { x + y}
## testthat
library(testthat)
expect_equal(my_sum(test_cases[ , 1], test_cases[ , 2]), test_cases[ , 3])
## RUnit
library(RUnit)
test_my_sum <- function() {
checkEquals(my_sum(test_cases[ , 1], test_cases[ , 2]), test_cases[ , 3])
}
关于r - 在 RUnit 或 testthat 中自动生成测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12479713/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!