Skip to content

library before context throws error? #700

Closed

Description

@alexpghayes

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.

Activity

  1. hadley commented on Jan 8, 2018

    @hadley
    Member

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

  2. hadley commented on Jan 24, 2018

    @hadley
    Member

    Re-opening because this bug is super confusing

  3. gm209 commented on Feb 14, 2018

    @gm209

    Hi Chaps,

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

    Cheers

  4. victordavisyieldsio commented on Feb 22, 2018

    @victordavisyieldsio

    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.

  5. added a commit that references this issue on Mar 11, 2018
  6. added a commit that references this issue on Mar 20, 2018
  7. added
    bugan unexpected problem or unintended behavior
    on Mar 29, 2018
  8. added 3 commits that reference this issue on Apr 2, 2018
    dff9aea
    3dcc732
    b85e069
  9. added a commit that references this issue on Apr 4, 2018
    42e3a79
  10. ctberthiaume commented on Apr 8, 2018

    @ctberthiaume

    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)
    })
    
    ...
    
  11. added a commit that references this issue on Apr 8, 2018
  12. jimhester commented on Apr 9, 2018

    @jimhester
    Member

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

  13. added a commit that references this issue on May 18, 2018
  14. Christoph999 commented on May 30, 2018

    @Christoph999

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

  15. Fablepongiste commented on Jul 3, 2018

    @Fablepongiste

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions