New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`library` before `context` throws error? #700

Closed
alexpghayes opened this Issue Jan 8, 2018 · 8 comments

Comments

Projects
None yet
8 participants
@alexpghayes

alexpghayes commented Jan 8, 2018

Hoping this is reproducible:

  1. Created a new R package (via new project) using RStudio.
  2. usethis::use_testthat()
  3. usethis::use_test("example")
  4. Modify test-example.R to
library(dplyr)

context("test-example.R")

test_that("multiplication works", {
  expect_equal(2 * 2, 4)
})
  1. 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.

@hadley

This comment has been minimized.

Member

hadley commented Jan 8, 2018

I'd say yes, context() should always be the first line.

@hadley hadley closed this Jan 8, 2018

@hadley

This comment has been minimized.

Member

hadley commented Jan 24, 2018

Re-opening because this bug is super confusing

@gm209

This comment has been minimized.

gm209 commented Feb 14, 2018

Hi Chaps,

Did this one get resolved? I am still experiencing this issue.

Cheers

@victordavisyieldsio

This comment has been minimized.

victordavisyieldsio commented Feb 22, 2018

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.

dankelley added a commit to dankelley/oce that referenced this issue Mar 11, 2018

add context() to some test files
I noticed that if a test failed and there was no context() call, then
devtools::test() reports no error on the file, but does report an error
in the summary at the end.  Well, anyway, that's my hypothesis based on
a recent bug (which came up whilst setting up firebreaks for the C++
transition). The following URL makes me think it's *mandatory* to have
context() calls. Who knew, and how come devtools() doesn't tell us about
that problem?

r-lib/testthat#700

newtux added a commit to newtux/humanize that referenced this issue Mar 20, 2018

@jimhester jimhester added the bug label Mar 29, 2018

jimhester added a commit to jimhester/testthat that referenced this issue Apr 2, 2018

Generate a context if one does not exist
Test files without an explicit `context()` would cause

    Error in x[[method]](...) : attempt to apply non-function

errors in the Progress reporter. This generates a
context from the filename if there is not already a defined context,
fixing the issue.

Fixes r-lib#700
Fixes r-lib#705

jimhester added a commit to jimhester/testthat that referenced this issue Apr 2, 2018

Generate a context if one does not exist
Test files without an explicit `context()` would cause

    Error in x[[method]](...) : attempt to apply non-function

errors in the Progress reporter. This generates a
context from the filename if there is not already a defined context,
fixing the issue.

Fixes r-lib#700
Fixes r-lib#705

jimhester added a commit to jimhester/testthat that referenced this issue Apr 2, 2018

Generate a context if one does not exist
Test files without an explicit `context()` would cause

    Error in x[[method]](...) : attempt to apply non-function

errors in the Progress reporter and a different (unreported) error in
the JUnit reporter. This generates a context from the filename if there
is not already a defined context, fixing the issue.

Fixes r-lib#700
Fixes r-lib#705

jimhester added a commit that referenced this issue Apr 4, 2018

Generate a context if one does not exist (#738)
Test files without an explicit `context()` would cause

    Error in x[[method]](...) : attempt to apply non-function

errors in the Progress reporter and a different (unreported) error in
the JUnit reporter. This generates a context from the filename if there
is not already a defined context, fixing the issue.

Fixes #700
Fixes #705
@ctberthiaume

This comment has been minimized.

ctberthiaume commented Apr 8, 2018

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 added a commit to armbrustlab/popcycle that referenced this issue Apr 8, 2018

@jimhester

This comment has been minimized.

Member

jimhester commented Apr 9, 2018

@ctberthiaume I sent a PR to the book at hadley/r-pkgs#478

nokome added a commit to stencila/r that referenced this issue May 18, 2018

@Christoph999

This comment has been minimized.

Christoph999 commented May 30, 2018

I had to create a line with context() in each test. This seems to be a bug. (Version 2.0.0)
Many thanks @jimhester !!!

@Fablepongiste

This comment has been minimized.

Fablepongiste commented Jul 3, 2018

Is it not a bug to have context mandatory ?
Should not that not be allowed to have tests without context ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment