1

I use tinytest to test a package. As I am testing multiple similar inputs with the same expected results, I created a small test-helper-function within the test_*.R file.

However, if a test failed, I do not get the typical failed notification. For example, in the following function, I would expect to get a summary or a direct failure but instead it returns

library(tinytest)

test_function <- function() {
  expect_equal(1, 1)
  expect_equal(2, 1)
  expect_equal(1, 1)
}

test_function()
#> ----- PASSED      : <-->
#>  call| test_function() 

Note that if I replace tinytest with testthat the function stops and returns an error as expected.

Is this expected behavior or what did I miss when reading the documentation and how can I use tinytest within such a testing function?

1 Answer 1

1

So what I was able to gather is that your test_function() is returning the result of the last expect_equal() inside the function. Which means if you change the last expect_equal(1, 1) to expect_equal(2, 1) or any other false result, the test_function will return FAILED results.

Now if you want to fetch the results of each expect_eqaul() inside your function, you can try this:

library(tinytest)
test_function <- function(){
    Results <- c(expect_equal(1, 1),expect_equal(2, 1),expect_equal(1, 1))
    return(Results)
}
test_function()

This will give you a list of Boolean Values as a result.

> test_function()
[1]  TRUE FALSE  TRUE

or to test if all tests where positive:

> expect_true(all(test_function()))
----- FAILED[data]: <-->
call| expect_true(all(test_function()))
diff| Expected TRUE, got FALSE 
Sign up to request clarification or add additional context in comments.

1 Comment

This is a good workaround (haven't actually thought about returning the result values), but unfortunately this does not inform me on which test has failed (compared to testthat). Thanks for your answer nonetheless!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.