Questions tagged [testthat]

testthat is a testing tool for R.

testthat is an package for , which is inspired by the family of testing packages and various testing libraries.

Repositories

Other resources

Related tags

366 questions
15
votes
1 answer

Force testthat to ignore warnings

I have tests for a package that check functions that may or may not return warnings, for example: test_that("test", { expect_true(is.na(log(NA))) expect_true(is.na(log(-1))) }) I am not interested in checking weather the warnings appeared. Is…
Tim
  • 7,075
  • 6
  • 29
  • 58
13
votes
2 answers

How to make a database connection/query in R for unit tests

I am using the testthat library for unit testing in a R project. I want to test code that relies on database queries, but not test the actual queries themselves. In other words, I'd like to mock the database connections and queries (having them…
user2507320
  • 189
  • 7
13
votes
3 answers

How to write a testthat unit test for a function that returns a data frame

I am writing a script that ultimately returns a data frame. My question is around if there are any good practices on how to use a unit test package to make sure that the data frame that is returned is correct. (I'm a beginning R programmer, plus new…
Blue Otter Hat
  • 607
  • 3
  • 9
  • 19
13
votes
2 answers

testthat pattern for long-running tests

I have a bunch of tests that I don't want them running during CRAN checks or Travis CI builds. They are either long-running, or they could cause transaction/concurrency conflicts writing to a networked database. What approach of separating them…
wibeasley
  • 5,000
  • 3
  • 34
  • 62
12
votes
2 answers

How to run a package's testthat tests

For a given installed package, how do I run its testthat tests? I'm not a developer of the installed package, I'm a user. I just want to run its test suite to confirm its tests pass in my environment. I've tried test_check and test_package but I…
Matt Dowle
  • 58,872
  • 22
  • 166
  • 224
12
votes
2 answers

How to mock functions from base?

I am calling a function from base in my code and I want to mock this function in my testthat unit test. How can I do this? library(testthat) my.func <- function() { return(Sys.info()["sysname"]) # e. g. "Linux" } my.func() # sysname # "Linux"…
R Yoda
  • 8,358
  • 2
  • 50
  • 87
12
votes
1 answer

R - testthat using data file outside the testing directory

I am using testthat to test a package with a file tree similar to the following: . ├── data │   └── testhaplom.out ├── inst │   └── test │   ├── test1.r │   ├── tmp_S7byVksGRI6Q │   │   └── testm.desc …
qed
  • 22,298
  • 21
  • 125
  • 196
11
votes
1 answer

confused by testthat and skip_on_cran()

tl;dr I want to run devtools::test() on a package and have it skip tests etc. as though it were running on CRAN, but I can't figure out how. As I understand it, testthat::skip_on_cran() checks for the environment variable NOT_CRAN, which should be…
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
11
votes
2 answers

Where to place library calls with testthat?

I am looking for best practice help with the brilliant testthat. Where is the best place to put your library(xyzpackage) calls to use all the package functionality? I first have been setting up a runtest.R setting up paths and packages. I then run…
micstr
  • 5,080
  • 8
  • 48
  • 76
11
votes
2 answers

testthat: handling both warning and value

What's the best way to handle calls that generate a warning but then also return a value? e.g. > require(testthat) > expect_warning(log(-1)) > expect_equal(log(-1), NaN) Warning message: In log(-1) : NaNs produced I want to write the test such that…
Stephen Eglen
  • 591
  • 2
  • 15
10
votes
2 answers

r - data.table and testthat package

I am building a package which works with data.table and which should be tested using package testthat. While the code works fine when calling from the command line, I run into issues when calling from a test case. It seems that the [] function from…
utal
  • 101
  • 4
10
votes
2 answers

Include tests in binary R package

I am using testthat to write unit tests for my R packages. I have seen a few package authors (like those from Rcpp and ggplot2) distributing their unit tests with the binary files. However, when I build my packages with RStudio (0.98.1102) and…
cryo111
  • 4,444
  • 1
  • 15
  • 37
9
votes
1 answer

covr shows 0% of coverage while all tests with testthat pass

I have built a package with unit tests for most of the functions. The package has a standard structure: package: - R/ - file1.R - file2.R - ... - tests/ - testthat/ - tests_for_file1.R - tests_for_file2.R - ... …
jjankowiak
  • 3,010
  • 6
  • 28
  • 45
9
votes
2 answers

How to test for a message and an error message simultaneously in R testthat?

I'm looking to unit test a function that produces a connection. It outputs a message that contains the connection details during execution. I want to test the following: The message appears as expected (expect_message(fn(),"blah")) There is no…
Steph Locke
  • 5,951
  • 4
  • 39
  • 77
9
votes
3 answers

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

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…
Stuart R. Jefferys
  • 943
  • 1
  • 13
  • 23
1
2
3
24 25