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
57
votes
4 answers

Where should I put data for automated tests with testthat?

I am using Hadley's testthat-based approach for automated testing of my package. With this approach, what is the most suitable place to put test data files that is files only used by the test scripts in tests/testthat), but not by any other…
Aditya
  • 1,554
  • 1
  • 13
  • 23
45
votes
3 answers

How to write a test for a ggplot plot

I have a lot of functions that generate plots, typically with ggplot2. Right now, I'm generating the plot and testing the underlying data. But I'd like to know if there's a reasonable way to test that the plot contains the layers/options I expect it…
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
43
votes
2 answers

How to test graphical output of functions?

I am wondering how to test functions that produce graphics. I have a simple plotting function img: img <- function() { plot(1:10) } In my package I like to create a unit test for this function using testthat. Because plot and its friends in base…
sgibb
  • 25,396
  • 3
  • 68
  • 74
31
votes
1 answer

How to set the tolerance of expect_equal in testthat framework

I would like to know if it is possible in R using testthat test framework to set the tolerance for equality. Currently, if example.R is: library(testthat) three_times<-function(x) 3*x context('Test three_times') test_that('Three times returns 3…
Oneira
  • 1,365
  • 1
  • 14
  • 28
28
votes
4 answers

Is there something in testthat like expect_no_warnings()?

I'm writing tests for a function that under some conditions will generate warnings. I want to ensure that under the other conditions it does not produce warnings. I don't see an obvious way to easily test that with testthat. I guess I could do…
BrodieG
  • 51,669
  • 9
  • 93
  • 146
25
votes
4 answers

testthat in R: sourcing in tested files

I am using the testthat package in R and I am trying to test a function defined in a file example.R. This file contains a call source("../utilities/utilities.R") where utilities.R is a file with functions written by me. However, when I am trying to…
paljenczy
  • 4,779
  • 8
  • 33
  • 46
24
votes
2 answers

Debugging 'testthat' tests in RStudio

Is it possible to invoke the debugger in RStudio when running testthat tests? I haven't been able to find a setup that allows this (various combinations of "use devtools package functions if available" in the settings, hitting the "Test Package"…
Ken Williams
  • 22,756
  • 10
  • 85
  • 147
24
votes
2 answers

How to use testthat expect_error() correctly?

What is the correct usage of expect_error() in testthat package? I've tried to adapt example from help, but this fails when I use brackets in error message. library(testthat) # Works tmp1 <- function() stop("Input is not correct") …
Tomas Greif
  • 21,685
  • 23
  • 106
  • 155
23
votes
2 answers

R testthat unit test data and helper function conventions

I am writing a R package, and I'm using testthat for unit testing. Many of my unit tests are for testing functions that work on a certain object specific to my package. For these tests I have made a helper function to set up a mock object. I have a…
Sam De Meyer
  • 2,031
  • 1
  • 25
  • 32
22
votes
2 answers

Run unit tests with testthat without package

I have a shiny application which uses like 4 functions. I would like to test these functions but it's not a package. How am i supposed to structure my code ? and execute these tests without devtools ?
Q. Eude
  • 841
  • 11
  • 24
22
votes
2 answers

Where to put external files for testthat tests

Suppose I have test like this: require(testthat) context("toy test") test_that("toy", { df = my.read.file("test.txt", header=TRUE) expect_true(myfunc(df) == 3.14) }) and this test relies on a external file test.txt, where…
qed
  • 22,298
  • 21
  • 125
  • 196
21
votes
1 answer

How to setup testthat for R CMD check?

There are apparently two ways to integrate testthat with R CMD check. I can't get either to work. Approach #1: (perhaps deprecated) According to the devtools wiki: When developing a package, put your tests in inst/tests and then create a file…
Nathan VanHoudnos
  • 12,923
  • 2
  • 23
  • 29
18
votes
7 answers

testthat fails within devtools::check but works in devtools::test

Is there any way to reproduce the environment which is used by devtools::check? I have the problem that my tests work with devtools::test() but fail within devtools::check(). My problem is now, how to find the problem. The report of check just…
drmariod
  • 11,106
  • 16
  • 64
  • 110
16
votes
1 answer

Error in x[[method]](...) : attempt to apply non-function in testthat test when sourcing file

I am developing an R app and came up with a workaround to integrate testthat in it (it usually requires your project to be a package) by adding a DESCRIPTION file to the project root. I got this method from a colleague who managed to get it to work…
Teodor Ciuraru
  • 3,417
  • 1
  • 32
  • 39
16
votes
3 answers

Is it possible to determine test order in testthat?

I am using testthat to check the code in my package. Some of my tests are for basic functionality, such as constructors and getters. Others are for complex functionality that builds on top of the basic functionality. If the basic tests fail, then it…
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
1
2 3
24 25