简体   繁体   English

使用 testthat 测试函数参数的条件

[英]Testing conditions on a function's parameter using testthat

I have a function of the form func(expr, data.sizes) , where the parameter expr is an expression and the one named data.sizes is ordinarily as the name suggests, a vector or sequence of data sizes.我有一个func(expr, data.sizes)形式的 function ,其中参数expr是一个表达式,而名为data.sizes的参数通常顾名思义是一个向量或数据大小序列。 (eg: c(10, 100, 1000) or 10^seq(1, 3) ) (例如: c(10, 100, 1000)10^seq(1, 3)

I would like to write some test-cases for my function's parameters using testthat , but am new to unit-testing in R so I would like to know how does one test conditions for a function's parameter(s) using testthat ?我想使用testthat为我的函数参数编写一些测试用例,但是对于 R 中的单元测试是新手,所以我想知道如何使用testthat来测试函数参数的条件?

For instance, I want to check for a possible NA value for the parameter data.size in my function and I wrote this snippet to test in my console: (function is available in Global Env.)例如,我想在我的 function 中检查参数data.size的可能 NA 值,并且我编写了这个代码段以在我的控制台中进行测试:(函数在 Global Env 中可用。)

test_that("NA test for data.sizes", {
  expect_false(is.na(data.sizes %in% func(expression, data.sizes = c(10, 100))))
})

which throws the error:引发错误:

* object 'data.sizes' not found
1: expect_false(is.na(data.sizes %in% func(expression, data.sizes = c(10, 
       10)))) at :2
2: quasi_label(enquo(object), label, arg = "object")
3: eval_bare(get_expr(quo), get_env(quo))
4: data.sizes %in% func(expression, data.sizes = c(10, 10))

What might I be possibly doing wrong?我可能做错了什么? Also what would be the syntax in general to apply test conditions on a function's parameters using testthat?另外,使用 testthat 在函数参数上应用测试条件的一般语法是什么?

testthat checks if the result of the function is what you expect. testthat 检查 function 的结果是否符合您的预期。

If you want to test the result of the function to NAs, you can make a specific test for this:如果要测试 function 到 NAs 的结果,可以对此做一个具体的测试:

library(testthat)
library(assertthat)

func <- function(expr,data.sizes) {
  assert_that(noNA(data.sizes))
  eval(expr)
}

test_that("data.sizes test for func", {
  # Success
  expect_error(func(expression(data.sizes[1]+data.sizes[2]), data.sizes = c(1,NA)))
  expect_equal(func(expression(data.sizes[1]+data.sizes[2]), data.sizes = c(1,2)),3)
  # Failure
  expect_equal(func(expression(data.sizes[1]+data.sizes[2]), data.sizes = c(1,2)),4)
})

To test validity of parameters inside the function, you can use assertive programming packages like assertthat.要测试 function 内部参数的有效性,您可以使用 assertthat 等断言编程包。

Your unit test can test the output of your function with a given input.您的单元测试可以使用给定的输入测试 function 的 output。 It is not a way of allowing your function to screen the given variables for inappropriate inputs.不是让您的 function 筛选给定变量以查找不适当输入的方法。 This has to be done inside your function.这必须在您的 function 内部完成。

The unit test must simply call your function with some parameters and ensure the output is what is expected.单元测试必须简单地使用一些参数调用您的 function 并确保 output 符合预期。

Suppose for example that you want func to return a list containing repetitions of the evaluated expression according to data.sizes , like this:例如,假设您希望func返回一个列表,其中包含根据data.sizes计算的表达式的重复,如下所示:

func <- function(expression, data.sizes)
{
  expression <- as.expression(as.list(match.call())$expression)
  if(!all(!is.na(data.sizes))) stop("data.sizes may not contain NAs")
  lapply(data.sizes, function(x) rep(eval(expression), x))
}

So that it does something like this:所以它做这样的事情:

y <- 5
func(y^2 + 3, 1:3)
#> [[1]]
#> [1] 28
#> 
#> [[2]]
#> [1] 28 28
#> 
#> [[3]]
#> [1] 28 28 28

But you want an exception to be thrown if there are NA values in data.sizes :但是如果data.sizes中有NA值,您希望抛出异常:

func(y^2 + 3, c(1, NA))
#> Error in func(y^2 + 3, c(1, NA)) : data.sizes may not contain NAs

Then your unit test would look like this:然后您的单元测试将如下所示:

test_that("NA test for data.sizes", {
  expect_list(func(y^2 + 3, 1:3))
  expect_error(func(y^2 + 3, c(1, NA)))
})

So you know if this test passes, your error is being picked up appropriately所以你知道如果这个测试通过了,你的错误就会被恰当地发现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM