Hoping this is reproducible:
usethis::use_testthat()
usethis::use_test("example")
test-example.R
to library(dplyr)
context("test-example.R")
test_that("multiplication works", {
expect_equal(2 * 2, 4)
})
devtools::test()
gives> devtools::test()
Loading tmp2
Testing tmp2
√ | OK F W S | Context
Error in x[[method]](...) : attempt to apply non-function
== Results =====================================================================
Duration: 0.2 s
OK: 0
Failed: 2
Warnings: 1
Skipped: 0
but if the context
comes before library
in test-example.R
, everything works. Is this expected? I'm using testthat 2.0.0
.
I'd say yes, context()
should always be the first line.
Re-opening because this bug is super confusing
Hi Chaps,
Did this one get resolved? I am still experiencing this issue.
Cheers
I didn't have a "context()" line in any of my tests either. Adding a context() line as the first line of each of my test-*.R scripts fixed this issue for me. No more "Error in x[method] : attempt to apply non-function" error.
Is there any way to update the example code in the "Testing" chapter of "R packages" (http://r-pkgs.had.co.nz/tests.html)? It currently has a context
call after a library
call. This chapter is linked to from the testthat
README.md
so it's a natural place to look for introductory documentation.
library(stringr)
context("String length")
test_that("str_length is number of characters", {
expect_equal(str_length("a"), 1)
expect_equal(str_length("ab"), 2)
expect_equal(str_length("abc"), 3)
})
...
@ctberthiaume I sent a PR to the book at https://github.com/hadley/r-pkgs/pull/478
I had to create a line with context() in each test. This seems to be a bug. (Version 2.0.0)
Many thanks @jimhester !!!
Is it not a bug to have context mandatory ?
Should not that not be allowed to have tests without context ?
Most helpful comment
Hi Chaps,
Did this one get resolved? I am still experiencing this issue.
Cheers