Code Monkey home page Code Monkey logo

testthat's Introduction

testthat

CRAN status R-CMD-check Codecov test coverage

Overview

Testing your code can be painful and tedious, but it greatly increases the quality of your code. testthat tries to make testing as fun as possible, so that you get a visceral satisfaction from writing tests. Testing should be addictive, so you do it all the time. To make that happen, testthat:

  • Provides functions that make it easy to describe what you expect a function to do, including catching errors, warnings, and messages.

  • Easily integrates in your existing workflow, whether it’s informal testing on the command line, building test suites, or using R CMD check.

  • Displays test progress visually, showing a pass, fail, or error for every expectation. If you’re using the terminal or a recent version of RStudio, it’ll even colour the output.

testthat draws inspiration from the xUnit family of testing packages, as well as from many of the innovative ruby testing libraries, like rspec, testy, bacon and cucumber.

testthat is the most popular unit testing package for R and is used by thousands of CRAN packages.

If you’re not familiar with testthat, the testing chapter in R packages gives a good overview, along with workflow advice and concrete examples.

Installation

# Install the released version from CRAN
install.packages("testthat")

# Or the development version from GitHub:
# install.packages("pak")
pak::pak("r-lib/testthat")

Usage

The easiest way to get started is with usethis. Assuming you’re in a package directory, just run usethis::use_test("name") to create a test file, and set up all the other infrastructure you need. If you’re using RStudio, press Cmd/Ctrl + Shift + T (or run devtools::test() if not) to run all the tests in a package.

testthat's People

Contributors

batpigandme avatar bisaloo avatar brodieg avatar craigcitro avatar crowding avatar davisvaughan avatar dirkschumacher avatar dkesh avatar dpprdan avatar gaborcsardi avatar hadley avatar jennybc avatar jimhester avatar kalibera avatar karl-forner-quartz-bio avatar kevinushey avatar kforner avatar krlmlr avatar lionel- avatar maelle avatar maxheld83 avatar mbojan avatar michaelchirico avatar michaelquinn32 avatar romainfrancois avatar salim-b avatar schloerke avatar trestletech avatar wch avatar windelinckx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

testthat's Issues

Additional matchers

I've found the following matchers useful to highlight the intent of my expectations. If you think any of them could be useful more broadly, let me know and I'll submit a pull request.

context("MongoCollection")

test_that("local connection to test DB", { 
  subject <- MongoCollection$new(test_db(), 'test')

  expect_proto(subject, 'MongoCollection')
  expect_interface(subject, 'add', 'count', 'db', 'drop', 'find', 'find_one', 'full_name', 'insert', 'is_connected', 'name', 'remove')

  expect_true(s$is_connected())
  expect_equal(s$name(), 'test')
  expect_equal(s$full_name(), paste(DB, 'test', sep='.'))
})

test_that("CRUD", { 
  data <- list(name='sim', tags=c('founder', 'cto', 'angel'))
  coll <- MongoCollection$new(test_db(), 'test')
  coll$insert(data)

  subject <- coll$find(query = list(name = 'sim'))
  expect_length(subject, 1)

  subject <- subject[[1]]
  expect_mongo_doc(subject)
  expect_includes(subject, data)
})

return error status from test_dir()

It would be quite useful in some cases for test_dir() (and similar) to return a value indicating success/failure, or the number of failures, or something. I can fake it by running tests twice, once with "summary" and then once again with "stop" wrapped in a tryCatch, but that's sorta clunky. Thanks! Great package!

Code coverage?

Is there any tools to evaluate code coverage for R scripts using the testthat package?

I found nothing by Google except a mention of the topic in the Future work section of an RJournal article. I also asked around at StackOverflow but nobody was able to help :-)

I really appreciate any help, thanks in advance!

expect_message and expect_warning open window on Mac

On a Mac, the following start up the quartz device:

expect_message(message('foo'))
expect_warning(warning('foo'))

This doesn't happen on linux.

I think this is because they call evaluate(), which calls dev.new() by default.

using test_that with Jenkins (fork)

G'day Hadley

I'm wanting to get test_that reports into a form that can be understood by Jenkins (http://www.jenkins-ci.org/). Basically that means the jUnit XML format -- Jenkins will highlight failing tests, giving options for more detail, show timing of groups of tests, overall trends etc.

So I went ahead and wrote a JUnitReporter at https://github.com/floybix/test_that

One problem was the need to identify tests stably over time even though they may start failing. For that I am using the deparsed expect_that() call itself. Currently it works to my satisfaction with expect_that() but not with expect_equals() etc.

Also the invocation is a little awkward to redirect to a file... output goes to standard out by default so in the ideal case where nothing else gets printed one could simply

sink("testjunit.xml")
test_package("testthat", reporter = "jUnit")
sink()

But the safer option is something like

test_package("testthat", reporter = JUnitReporter$clone()$do(self$file <- "testjunit.xml"))

What do you think?

not_shows_message and not_prints_text

I think it might be useful for not_shows_message or not_prints_text which is similar to shows_message and prints_text except that the test is fail instead of pass.

For example, I need to test if executing an external program does not produces warning message.

auto_test and sourcing using relative paths

I am not certain this is a legitimate issue but it is a behavior I've only experienced when using auto_test and not when I'm doing package development with devtools + testthat. The problem seems to arise from how auto_test sets up the loading of files. It breaks relative path sourcing. I've described the issue here.

Is there a way to make auto_test interoperate with relative path sourcing?

Add `message` parameter to expect_* methods?

I find myself wanting to add an additional message to my tests that add more context to them so that I can quickly identify what broke.

For instance, imagine I'm testing the results of a list in a loop:

for (idx in 1:length(result)) {
  expect_identical(result[[1]], expected[[1]])
}

We don't really know that the 7th one fails, for instance, if something fails there. Allowing for a message parameter that is printed in the failure report could help to address this issue, ie:

for (idx in 1:length(result)) {
  expect_identical(result[[1]], expected[[1]], message=sprintf("%dth item failed", idx))
}

(Sorry if this isn't the right place of feature requests, please direct me elsewhere if it's not)

gives_warning on 2.15.2

I just updated R to 2.15.2 and ran my unit tests. I noticed something weird, when using gives_warning I briefly get to see an X11 window or some sort. I don't think that used to be the case in 2.15.1? I.e:

gives_warning()(warning('yay'))

Add `pattern` regexp parameter for test_dir and test_package

Can we add a pattern parameter to the test_package and test_dir in order to override the default file pattern that test_dir uses to find and run the testing files?

For example, test_dir would be changed to something like:

test_dir <- function(path, reporter = "summary", env = NULL, test.pattern="^test.*\\.[rR]$") {
  reporter <- find_reporter(reporter)
  if (is.null(env)) {
    env <- new.env(parent = globalenv())    
  }

  source_dir(path, "^helper.*\\.[rR]$", env = env)

  files <- dir(path, test.pattern, full.names = TRUE)
  with_reporter(reporter, lapply(files, function(file) {
    sys.source(file, chdir = TRUE, envir = new.env(parent = env))
    end_context()
  }))
}

I can submit a pull request with this + the obvious change to test_package if you like.

test that certain conditions dont occur

It would be nice to test that

  • some warning is not given,
  • some message not printed,
  • some text not printed,
  • etc.

Maybe either like this

dont_expect_that("foo", gives_warning("bar"))

or like this

expect_that("foo", doesnt_give_warning("bar"))

Test if there was no output when executing a test?

I would like to test if a specific function does not writes anything on the console, is there a workaround for that in the testthat package?

Unfortunately expect_that( foo(), prints_text("") ) is succesful even if there is an output (I guess that's because it's a regexp match).

Thanks for any guidance!

Encoding issues in sys.source()

I've got error when I run testthat in Windows.

  • checking for unstated dependencies in examples ... OK

  • checking examples ... NONE

  • checking for unstated dependencies in tests ... OK

  • checking tests ...
    Running 'test-all.R'
    ERROR
    Running the tests in 'tests/test-all.R' failed.
    Last 13 lines of output:
    Type 'demo()' for some demos, 'help()' for on-line help, or
    'help.start()' for an HTML browser interface to help.
    Type 'q()' to quit R.

    library(testthat)
    library(KoNLP)
    Loading required package: rJava
    Loading required package: bitops
    test_package("KoNLP")
    Error in parse(n = -1, file = file) :
    invalid multibyte character in parser at line 5
    Calls: test_package ... with_reporter -> force -> lapply -> FUN -> sys.source
    -> parse
    Execution halted

I was using UTF-8 test case source to check Korean NLP functions, Linux(UTF-8) or Mac was ok. but Windows was not.

I think, Usages of sys.source in testthat need to be sys.source(file(",,,,,", encoding=""), env).

Freely check with my package on "https://github.com/haven-jeon/KoNLP".

Add comma between packages in depends section of DESCRIPTION file

install_github("test_that") 
... stuff happens then....
   * checking DESCRIPTION meta-information ... ERROR
Malformed Depends or Suggests or Imports or Enhances field.
Offending entries:
  R (>= 2.12.0)
methods
Entries must be names of packages optionally followed by '<=' or '>=',
white space, and a valid version number in parentheses.

See the information on DESCRIPTION files in section 'Creating R
packages' of the 'Writing R Extensions' manual.

Error: Command failed (1)
In addition: Warning message:
In getDependencies(pkgs, dependencies, available, lib) : package ‘R
methods’ is not available (for R version 2.14.1)

Could this be a result of the fix to issue #32? lubridate seems to load when I have

  Depends:
       methods,
       R (>= 2.10)

in the DESCRIPTION file with methods first and then a comma

install_github("lubridate", branch = "S4version")

which is interesting in itself because it doesn't pass all of the tests yet...

Garrett

source_dir in auto_test doesn't work as it should

Hi Hadley,

Thanks for another wonderful R package, I'm using it a lot.
I'm using auto_test a lot, but auto_test at the moment contains a small bug.

The sources are sourced in a new environment that is not given to the test_dir, so testing fails.
Files that are edited and are sourced properly: in de parent environment.

Regards,

Edwin de Jonge

convenience function for expect_that

The following is a convenience function that is useful when you are working in an interactive session and you decide that you want to test one of the objects in your workspace. The function inputs the object you want to test and an argument that must be "equals", "is_equivalent_to", or "is_identical_to". It outputs the test that you can copy and paste into an R script.

export_test <- function(x, test = "equals") {
        stopifnot(test %in% c("equals","is_equivalent_to","is_identical_to"))
        substitute(expect_that(subme1,subme2(subme3)),list(subme1=substitute(x),subme2=as.name(test),subme3=x))
}

For example, suppose you have the following factor:

happiness <- factor(c("happy", "not at all happy", "not at all happy", "quite happy", "not very happy"))

If you run the following command,

export_test(levels(happiness),"is_identical_to")

You get this output:

expect_that(levels(happiness), is_identical_to(c("happy", "not at all happy", "not very happy", "quite happy")))

printed output from expectations can go wrong

Long expressions in an expect_that() statement can be deparsed into multiple lines, and this messes up the printed output when it is paste()d.

Example, of a failing test:

expect_that(fitted(armax.sriv.fit(cbind(U = U, Q = Q_n3s3), order = c(3,0))), equals(Q_n3s3, tol = 1e-5))
# Error: Test failure: 
# fitted(armax.sriv.fit(cbind(U = U, Q = Q_n3s3), order = c(3,  not equal to Q_n3s3Test failure: 
#0))) not equal to Q_n3s3

The relevant deparse() statements occur in expectations.r and in expect-that.r

Perhaps something like
name <- paste(deparse(substitute(object)), collapse = "\n")
or
name <- paste(deparse(substitute(object), width = 500), collapse = "\n")

expect_error to fails with some error texts with non-alphanumeric characters

Overall, love the package, but having some odd issues with error message handling.

See below demo code, note that "!" works, while "*" and a range of extended ASCII characters fail.

f <- function(x) {if(class(x) != "numeric") stop("No!") else x}

test_that("Working example", {
      expect_equal(f(1), 1)
}) 

test_that("Working example p2", {
      expect_error(f("a"), "No!")
})

f2 <- function(x) {if(class(x) != "numeric") stop("No*!") else x}

test_that("Non-working example", {
      expect_equal(f2(1), 1)
}) 

test_that("Non-working example p2", {
      expect_error(f2("a"), "No*!")                    # Fails due to inclusion of * character
})

> sessionInfo()
R version 2.13.1 (2011-07-08)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] testthat_0.5 stringr_0.5

loaded via a namespace (and not attached):
[1] evaluate_0.3    grid_2.13.1     lattice_0.19-30 mutatr_0.1.2    plyr_1.5.2      tools_2.13.1   

run-all.R passes tests on file that has commented out tests

[tests] 
✈  Rscript run-all.R 
<many load messages>
Yellow order Book is consistent  : ..........
Yellow trade statistics are consistent  : ...........................

I have a file in inst/tests called test_yellow.R but have since modified it with commenting out some lines so that the correct output is as follows:

> test_file('test_yellow.R')

<printout of transactions>

Yellow order Book is consistent  : ..........
Yellow trade statistics are consistent  : ..1


1. Failure: K.Ratio is 1 -------------------------------------------------------
stats$K.Ratio not equal to 1
Modes: numeric, NULL
Lengths: 1, 0
target is numeric, current is NULL

Even if I remove the test file from the inst/tests directory altogether, run-all.R is still running tests and passing them.

So for completeness, run-all.R is in quantstrat/tests and test_yellow.R is in quantstrat/inst/tests

Change wording in test_that output

The syntax of the test_that() functions encourages users to write names for tests so that they have a natural kind of flow to the language:

test_that("forecast values match expected values", {
  ...
})

However, the output generated upon failure doesn't follow this same flow:

 Error: Test failure in 'forecast values match expected values'

Perhaps it could be changed to something more like this:

 Error: Testing whether 'forecast values match expected values'

praise given if an error prevents self$n from incrementing

First of all, thanks for writing such a great package. I just wanted to let you know that I encountered a minor bug. It's not a big deal in the grand scheme of things of course, but it could be a symptom of a larger issue if self$n is used as a diagnostic elsewhere.

Let me know if you have trouble reproducing it and I'll code up a simple example.'

Dave

Inaccurate error reporting from tests

I have noticed that the error context reported by test_that sometimes seems inaccurate while also lacking important debugging information such as line numbers.

Consider the following error:

1. Error: connect ----------------------------------------------------------------------------------------------------------------------------------
$ operator is invalid for atomic vectors
1: AnalyticsSystem$connect()
2: get(x, envir = this, inherits = inh)(this, ...)
3: .$db()
4: get(x, envir = this, inherits = inh)(this, ...)
5: MongoDB$new(host = config$hosts, db = config$database, replica = config$set_name, username = config$username, password = config$password)
6: get(x, envir = this, inherits = inh)(this, ...)
7: mongo.create(host, replica, username, password, db, timeout)

It seems that the error is coming from mongo.create but here is the implementation from the rmongodb packages:

function (host = "127.0.0.1", name = "", username = "", password = "", 
    db = "admin", timeout = 0L) 
{
    mongo <- .Call(".mongo.create")
    attr(mongo, "host") <- host
    attr(mongo, "name") <- name
    attr(mongo, "username") <- username
    attr(mongo, "password") <- password
    attr(mongo, "db") <- db
    attr(mongo, "timeout") <- timeout
    .Call(".mongo.connect", mongo)
}

The implementation doesn't seem to use the $ operator at all.

Here is the implementation of the calling function:

MongoDB <- proto(
  ..Name = 'MongoDBFactory', 

  new = function(., host="127.0.0.1", replica="",
                    username="", password="", 
                    db="test", timeout=60*1000L) {

    mongo <- mongo.create(host, replica, username, password, db, timeout)
    stopifnot(mongo.is.connected(mongo))

    .$proto(
      ..Name = 'MongoDB'
      # rest of implementation 
    )
  }
)

What can developers do in their code to make error context more accurate? Also, can we get files & line numbers in error messages? It's the kind of thing users of a testing framework tend to rely on...

Colourise and R.app gui

Currently for those of us who use the R.app GUI the colourise function still tries to use the xterm coloring. Not sure if it is possible to output colored text in the GUI but a simple solution would be to detect if the GUI is being used and skip the coloring. Not sure if this is the best approach but I was able to use the following (no clue if this would work in Windows or if this is a nonissue):

if ("R_GUI_APP_VERSION" %in% names(Sys.getenv())) {
return(text)
}

running via Rscript fails

This script (rscript_test.R):

#!/usr/bin/Rscript

library(testthat)

a <- data.frame(x=1:3, y=4:6)
b <- data.frame(x=1:3, y=5:7)

context('rscript test')

test_that('dfs match',
    expect_equivalent(a, b)
)

works when sourced or run in batch mode:

hharris test> R CMD BATCH ./rscript_test.R 
hharris test> !cat
cat rscript_test.Rout 

R version 2.14.1 (2011-12-22)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.    

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Previously saved workspace restored]

> #!/usr/bin/Rscript
> 
> library(testthat)
> 
> a <- data.frame(x=1:3, y=4:6)
> b <- data.frame(x=1:3, y=5:7)
> 
> context('rscript test')
> 
> test_that('dfs match',
+   expect_equivalent(a, b)
+ )
Error: Test failure in 'dfs match'
a not equal to expected
Component 2: Mean relative difference: 0.1666667
Execution halted

But fails when run via Rscript:

hharris test> ./rscript_test.R 
Error in context("rscript test") : attempt to apply non-function
Execution halted

Similar scripts with a test_dir() have failed with a slightly more useful errors:

Error in find_reporter(reporter) : attempt to apply non-function
Calls: test_dir -> find_reporter
Execution halted

Export `expectation` class/method through the NAMESPACE

I'd like to use test_that to unit test another package.

To make my life easier (and the tests more intuitive), I also want to build my own/custom "expectations" for some data structures I'm defining in this package. Currently we have to "finish" each such expecation with testthat:::expectation(TEST, MSG).

I'm hoping you could export expectation so that I can use it with a guilt-free manner.

For example, I'd like to test that two of my own/new data structures are equivalent to each other. Say my custom type is called object, I define two functions:

is_equivalent_object <- function(expected, label=NULL) {
  ## some param beuracracy
  function(actual) {
    ## ... other beuracracy
    testthat:::expectation({
       ## block that evaluates to TRUE if actual_object == expected_object
    }, "some message")
  }
}

expect_equivalent_object <- function(expected, actual, ...) {
  ## ... stuff
  expect_that(expected, is_equivalent_object(actual), ...)
}

test helper objects don't persist in R CMD check

Hi - this package has greatly improved the quality of my coding - thanks.

I just noticed that the objects created by helper-.R scripts don't persist into the test-.R scripts when running R CMD check on the package. [R version 2.11.0 Under development (unstable) (2010-01-30 r51070)]

For example you have a helper in plyr, which I predict would cause an error to be thrown in R CMD check
http://github.com/hadley/plyr/blob/master/tests/helper-no-attributes.r

Am I missing something, or do you recommend to put in explicit source() statements?

bare expectations in a test file cause an error

> library(testthat)
> context('Simplest test')
> expect_equal(1,2)
Error: 1 not equal to 2
Mean relative difference: 0.5
> test_file('tests/test_01_df.R')
Simplest test : 1
Error: Input to str_c should be atomic vectors

(where the contents of that test file are the three lines pasted above.)

print_text removes line endings from output

print_text is currently defined as:

function(regexp, ...) {
  function(expr) {
    output <- str_c(capture.output(force(expr)), collapse = "")
    matches(regexp, ...)(output)
  }
}

capture.output returns a character vector with no line endings, and the result is collapsed together with an empty string.

No more colorized text in non-interactive terminals?

5371ed1 added !interactive() to the list of conditions under which colorization would not be applied to text. If I remove this change, everything seems to work fine under Rscript running in a terminal emulating xterm-color.

Any reason for the change?

Use getOption to find default reporter...

Just a small feature request...

I've written a custom reporter, which subclasses the summaryReporter and adds a few extra features. It would be nice if I could use this in place of the normal summary reporter by default.

Would it be possible to change the signature of find_reporter to be something like:

find_reporter <- function(reporter = getOption('testthat.default.reporter', 'summary'))

That way, the default behaviour is preserved, but I can set an option to override the default for my whole session.

Thanks,
Ash.

simple bugs in gives_warning and shows_message

both functions dont work, when no warnings / messages are generated in the epxression.

minimal examples:

gives_warning("foo")(1)
Error: String must be an atomic vector
shows_message("foo")(1)
Error: String must be an atomic vector

cause:
warnings <- sapply(Filter(is.warning, res), "[[", "message")
Filter and sapply return an empty list, but matches later on requires a char vector.

possible fix:
vapply(Filter(is.warning, res), "[[", "message", fun.value = character(1))

also:
shows_message is copy-paste from gives_warning
at some places in the code and expecially the result-message: warning(s) --> message(s)

Bernd

named vectors

Is this intended behavior?

> (x <- unlist(list(foo=TRUE)))
 foo 
TRUE 
> expect_true(x)
Error: x isn't true

source() ignores environment when test_file() called on file

I have a function called do_something.r that does something, but I want it to do something else when I'm testing it. The reason is because it does something different every day and I want to make it do something over the same time period for testing, so my test assertions can remain fixed.

I have the following block in do_something.r that allows it to do it's thing unless I call it in a testing mode. Testing mode is on if the microphone is TRUE

if(!exists("microphone") || !isTRUE(microphone)){
      voice = "do_something code"}

The rest of do_something is this:

 cat("Voice is from the", voice, "\n")

When this file is sourced from R, we get the following result

> source('do_something.R')
Voice is from the do_something code 

The result we would expect. Next I wrote the source_something.r script to make sure my assumptions about how source() works were correct.

 microphone = TRUE
 voice      = "source_something code"
 source("do_something.R")

When source_something.r is itself sourced, we get the following result in R.

> source('source_something.R')
Voice is from the source_something code 

Now let's put the source_something block inside a test script called test_something.r

require(testthat)

microphone = TRUE
voice      = "source_something code"
source("do_something.R")

When this file is tested in R, we don't get the expected result

> test_file('test_something.r')
Voice is from the do_something code 

Somehow, the microphone = TRUE assignment gets lost as does (presumably) the voice = "source_something code" assignment when the file is run under the testthat framework.

Is there a work around for this I can use?

fail function for forcing a test failure

There's been a request for a fail function in testthat, that just makes a test fail.

http://stackoverflow.com/questions/12507229/how-to-make-a-test-fail-with-the-testthat-package

I had a go at writing one, for possible inclusion in the package.

' Makes a testfail

'

' The fail function forces a test to fail.

'

' @param message a string to display.

' @param infoextra information to be included in the message (useful when writing tests in loops).

fail <- function(message = "Failure has been forced.", info = NULL)
{
expect_that(
NULL,
function(message)
{
expectation(FALSE, message)
},
info = info,
NULL
)
}

Usage is, for example,

test_that("failure test", fail())

Error in str_c when using test_dir

@update2
I suspect that at this stage when using test_dir you always need to use test_that, not single expect_that calls. The problem is that single test_that calls do not have a 'tests' object, which, according to the example below, returns NULL when trying to extract it and causes the error in str_c

Paul

@update1
I just found out that when I use the setup:

test_that("It is not equal", expect_that(1, equals(2))))

That the function runs fine. The following code fails when using test_dir:

expect_that(1, equals(2)))

regards,
Paul

@FIRST message
Hi Hadley,

I've looking into unit testing and found your package. However, I ran
into some issues. I had the following testfile:

context("context 1")
expect_that(1, equals(2))
expect_that(5, equals(2))

context("context2")
expect_that(3, equals(1))

located in the subdirectory "tests". When running:

test_dir("tests")

I got the error:

test_dir("tests")

context 1 : 12
context2 : 3Error: Input to str_c should be atomic vectors

traceback()
5: stop("Input to str_c should be atomic vectors", call. = FALSE)
4: str_c(label, ". ", type, ": ", tests, " ")
3: reporter$end_reporter()
2: with_reporter(reporter, {
source_dir(path, "^test.*.[rR]$", env = env)
})
1: test_dir("tests")

I traced the problem down to the summary reporter (reporter-summary.r).
In line 65:

tests <- sapply(self$failures, "[[", "test")

the function tries to extract the "test" attribute from self$failures.
The problem is that there is no "test" attribute and returns NULL and
the line after that fails with the above mentioned error:

header <- str_c(label, ". ", type, ": ", tests, " \n")

In other reporters, failure$test is assigned the value 'desc' (e.g. in
self$start_test of reporter-stop.r), we could this in self$start_context
for reporter-summary. But I'm not really sure what the best solution is
right here. Would it be possible to fix this?

regards,
Paul

sessionInfo()
R version 2.12.0 (2010-10-15)
Platform: i686-pc-linux-gnu (32-bit)

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] testthat_0.3

loaded via a namespace (and not attached):
[1] evaluate_0.3 mutatr_0.1.2 plyr_1.2.1 stringr_0.4 tools_2.12.0

Documentation example for tolerance

Some users might benefit if you demonstrate how to include a tolerance. Somewhere like the 'equals' help page, include an examples like:

Test the ABSOLUTE difference is within .002

expect_equal(object=10.01, expected=10, tolerance=.002, scale=1)

Test the RELATIVE difference is within .002

expect_equal(object=10.01, expected=10, tolerance=.002, scale=NULL)

By the way, I really like using this package.

@templateVar in roxygen doesn't appear to work properly

I'm trying to use brew templates. Basic templates work just fine, but variables don't seem to work the way they're documented.

The docs say that I need to set variables inside the roxygen section. So I have something like this:

#' @templateVar foo value
#' @template some_template

I set up a simple template:

(man-roxygen/some_template.R)
#' <%=foo%>

However, roxygen will complain that "foo" doesn't exist. I tried doing an ls() inside of the template tag, and found that only templateVar exists. So, this will work:

(man-roxygen/some_template.R)
#' <%=templateVar%>

Note that I still need to have some sort of arbitrary "foo" variable in the @templateVar directive, but they all get coerced to have the name "templateVar" in the template.

Line number information in error messages

It would be very nice if errors included line number information since traceback() doesn't work given the exception handling model of testthat. Showing the expression alone is not sufficient sometimes.

Feature request: extend flexibility of arg 'code_path' in 'auto_test()'

Hi Hadley,

first of: thanks a LOT for this package! It's really awesome and makes code testing a lot of fun!

One question/feature request though:
AFAIU, arg 'code_path' expects a directory containing one ore numerous definition scripts (for classes, methods, functions etc.).

Now, I've come up with a custom project structure for my code that serves as an intermediate step towards the creation of a full-grown R package. Hence, it does not completely resemble the structure of an R package yet (with R files residing in directory 'R'). Instead, actual definition code is organized as follows:

~/lib
~/lib/clss (class defs)
~/lib/fcns
~/lib/fcns/gnrcs (defs of generic methods)
~/lib/fcns/mthds (defs of S4 methods)
~/lib/fcns/stnds (defs of regular/standard R functions)

The problem is, that besides those directories, 'lib' also contains other dirs such as 'app' (applications scripts), 'prof' (profiling scripts), 'tests', 'examples' etc.
Therefore

auto_test(code_path="/lib", test_path="/lib/tests")

will throw errors. So before going ahead and reorganizing my directory structure, I wondered if it'd be possible to extend arg 'code_path' to a vector of character strings instead of just allowing an atomic string. That way, I could launch

auto_test(code_path=c("/lib/clss", "/lib/fcns"), test_path="~/lib/tests")

It'd be great if you could consider this for future releases!

Thanks a lot,
Janko

Introduce error margin for expect_equal

How about introducing an error margin for expect_equal to shorten expressions like

expect_true( abs(foo() - 1) < 1e-4 )

to something convenient like

expect_equal( foo(), 1e-4 )

Thanks for this wonderful package!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.