testthat

Why is helper file in testthat sourced twice

做~自己de王妃 提交于 2019-12-13 14:01:55
问题 I run into trouble when using uuids in tests, as the helper file is sourced twice. Why does that happen? Is there a way to avoid second sourcing? For a reproducible example, just create a new package, put a file called "helper-data.R" in /tests/testhat/ with th following content if (!exists("test_ind")) { test_ind <- 1 print(paste0("test_ind = ", test_ind)) test_ind <- test_ind + 1 } else { print(paste0("test_ind = ", test_ind)) test_ind <- test_ind + 1 } and create an file "test-1.R" in

With Roxygen and testthat, what is the proper way to make internal helper functions available to testcases called during R CMD check?

喜你入骨 提交于 2019-12-12 20:41:13
问题 I am creating an R package, and found it useful to break parts of the logic in one file into internal helper functions, which I define in the same file. I have sort of a special case where my function decides which helper function to use via match.fun() . Since they won't be useful to other functions or people, I don't want to put these in separate files, and I don't want to export them. All my testthat cases pass using test_dir() . When I don't export these functions, my testthat cases fail

test_that With match.fun Throws Unexpected Error when Used Two Levels Deep

送分小仙女□ 提交于 2019-12-12 14:58:17
问题 I'm having a problem using match.fun together with test_that when match.fun is used inside nested functions. To illustrate, I've built a quick toy example R package containing two functions. The latter simply calls the former: i_dont_throw_error <- function(function_name) match.fun(function_name)("hello") i_throw_error <- function(function_name) i_dont_throw_error(function_name) I then wrote testthat tests as follows: test_that("Testing for an error with match.fun one level deep.",{ print

Using filter argument in test_package to skip tests

為{幸葍}努か 提交于 2019-12-12 11:57:55
问题 I would like to run my package unit tests during R CMD check , but skip tests that require an internet connection. By convention, all unit tests that require internet have the word network in their filename. Hence my run-all.R contains: library(testthat) test_package("mypackage", filter="^((?!network).)*$") However this gives an invalid regular expression error. How do I specify the filter argument such that it runs each unit test except the ones with the word network in them? 回答1: Use the

git2r::summary() produces different results when called from console and by RStudio

蹲街弑〆低调 提交于 2019-12-12 06:58:58
问题 I am trying to write an R package that analyzes Apache Pig GitHub repository with the help of git2r package. I also use testthat package for unit testing. I have a function, let's call it compute() , which contains code along the lines of: repo <- repository("C:\normalized\path\to\apache\pig"); allCommits <- commits(repo); commitSummary <- capture.output(summary(allCommits[[1]])); print(commitSummary); The commitSummary is an important part, because I run several regexes on it to recover data

How to test for multiple warnings in an unknown order using testthat?

狂风中的少年 提交于 2019-12-12 04:21:07
问题 I want to test that a function generates multiple warnings (4 or more), when the order of the warnings can vary. My best attempt to do this is based on look-ahead RegExp matching. Simplifying to just 2 warnings, I know my RegExp work on a single string output, because both of the following are true: grepl("(?s)(?=.*2)(?=.*1)", "* warn 1.\n* warn 2.", perl=TRUE) grepl("(?s)(?=.*2)(?=.*1)", "* warn 2.\n* warn 1.", perl=TRUE) However, this does not work when testing multiple warnings with

Printing custom diagnostic information if `testthat` test fails in `R`

倖福魔咒の 提交于 2019-12-11 02:37:15
问题 I use a testthat unit test to check whether the data.frame returned by a function is identical to the one I would expect it to return. If the test fails, testthat prints some diagnostic information, for instance: MyFunction(df.orig) is not identical to df.expected. Differences: Names: 1 string mismatch However, I would really like to see the actual output of the function, i.e. print the returned data.frame . Is there a way to print the output of the tested function (or some other custom

How to ensure english error messages in testthat unit tests

与世无争的帅哥 提交于 2019-12-11 02:13:07
问题 I have a lot of unit tests using the testthat package that expect english error messages. If other developer run the tests on a computer configured for a non-english locale the error message are emitted in a different language and my tests fail. How can I initialize testthat to change the language settings only during the test run-time without manually or permanently changing the language or test environment from outside of R (like e. g. proposed here: in R how to get error messages in

Test for exact string in testthat

最后都变了- 提交于 2019-12-10 23:37:57
问题 I'd like to test that one of my functions gives a particular message (or warning, or error). good <- function() message("Hello") bad <- function() message("Hello!!!!!") I'd like the first expectation to succeed and the second to fail. library(testthat) expect_message(good(), "Hello", fixed=TRUE) expect_message(bad(), "Hello", fixed=TRUE) Unfortunately, both of them pass at the moment. For clarification: this is meant to be a minimal example, rather than the exact messages I'm testing against.

function expect_that from testthat runs into error

老子叫甜甜 提交于 2019-12-10 17:49:52
问题 Can anybody help me and explain why expect_that doesn't work if [] is added to the stop message, i.e. f1 works but f2 doesn't. library(testthat) f1 <- function(x){ if( x >= 1 ){ stop("error 1") } } expect_that(f1(x=1.4), throws_error("error 1")) f2 <- function(x){ if( x >= 1 ){ stop("error [1]") } } expect_that(f2(x=1.4), throws_error("error [1]")) 回答1: expect_that is looking for a regular expression to match the error, so you need to escape the square-brackets so that they are interpreted