Skip to content

describe_it doesn't find the right environment when it() is prefixed with testthat:: #2085

Closed

Description

@averissimo

I believe the it function should pass the parent.frame() explicitly

See reproducible example below of failure

testthat::describe("a test", {
  aa <- 1
  it("should pass", {
    testthat::expect_equal(aa, 1)
  })
})
#> Test passed 🌈


testthat::describe("a test", {
  aa <- 1
  testthat::it("should pass", {
    testthat::expect_equal(aa, 1)
  })
})
#> ── Error: should pass ──────────────────────────────────────────────────────────
#> Error in `eval(code, test_env)`: object 'aa' not found
#> Backtrace:
#>     ▆
#>  1. └─testthat::expect_equal(aa, 1)
#>  2.   └─testthat::quasi_label(enquo(object), label, arg = "object") at testthat/R/expect-equality.R:62:3
#>  3.     └─rlang::eval_bare(expr, quo_get_env(quo)) at testthat/R/quasi-label.R:45:3
#> Error:
#> ! Test failed

library(testthat)
describe("a test", {
  aa <- 1
  testthat::it("should pass", {
    testthat::expect_equal(aa, 1)
  })
})
#> ── Error: should pass ──────────────────────────────────────────────────────────
#> Error in `eval(code, test_env)`: object 'aa' not found
#> Backtrace:
#>     ▆
#>  1. └─testthat::expect_equal(aa, 1)
#>  2.   └─testthat::quasi_label(enquo(object), label, arg = "object") at testthat/R/expect-equality.R:62:3
#>  3.     └─rlang::eval_bare(expr, quo_get_env(quo)) at testthat/R/quasi-label.R:45:3
#> Error:
#> ! Test failed

Created on 2025-06-04 with reprex v2.1.1

Activity

  1. changed the title [-]`describe_it` doesn't find the right environment when prefixed with `testthat::`[/-] [+]`describe_it` doesn't find the right environment when `it()` is prefixed with `testthat::`[/+] on Jun 4, 2025
  2. averissimo commented on Jun 26, 2025

    @averissimo
    ContributorAuthor

    Another use case when calling testthat::it alone or within a testthat::describe parent.

    It tries to set the class definition in the package namespace.

    testthat::it("setting an S4 class", {
      methods::setClass("NewS4Class", slots = list(value = "numeric"))
      withr::defer(removeClass("NewS4Class"))
      new_obj <- methods::new("NewS4Class", value = 42)
      testthat::expect_s4_class(new_obj, "NewS4Class")
    })
    #> ── Error: setting an S4 class ──────────────────────────────────────────────────
    #> Error in `assign(mname, def, where)`: cannot add bindings to a locked environment
    #> Backtrace:
    #>     ▆
    #>  1. └─methods::setClass("NewS4Class", slots = list(value = "numeric"))
    #>  2.   └─methods::assignClassDef(Class, classDef, where)
    #>  3.     └─base::assign(mname, def, where)
    #> Error:
    #> ! Test failed

    Created on 2025-06-26 with reprex v2.1.1

  3. added
    bug
    on Jul 16, 2025
  4. hadley commented on Jul 16, 2025

    @hadley
    Member

    Maybe so, but it's definitely not expected that you preface every testthat function with testthat::. This is definitely off label usage of testthat and not something that we generally support.

  5. added a commit that references this issue on Jul 23, 2025
    b8a4c47
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

    BDD 🧑‍🏫bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions