Skip to content
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

testthat::test_file prevents testthat::it from accessing global variables #1926

Closed
TymekDev opened this issue Feb 1, 2024 · 1 comment
Closed

Comments

@TymekDev
Copy link

TymekDev commented Feb 1, 2024

Hi, I just noticed that code inside testthat::it (specifically with testthat::) does not have access to global variables when invoked with testthat::test_file() (or other runners). Sourcing the same file works fine. Using library(testthat) and just it works fine too. See examples below.

testthat version: 3.2.0

library variant

File

# reprex_library.R
library(testthat)
x <- "asdf"
describe("a", {
  it("b", {
    force(x)
  })
})

Output

source("reprex_library.R")
# Test passed 🌈

testthat::test_file("reprex_library.R")
# ══ Testing reprex_library.R ═══════════════
# [ FAIL 0 | WARN 0 | SKIP 0 | PASS 0 ] Done!

:: variant

File

# reprex_colons.R
x <- "asdf"
testthat::describe("a", {
  testthat::it("b", {
    force(x)
  })
})

Output

source("reprex_colons.R")
# Test passed 🎊

testthat::test_file("reprex_colons.R")
# ══ Testing reprex_colons.R ══════════════════
# [ FAIL 1 | WARN 0 | SKIP 0 | PASS 0 ]
# 
# ── Error (reprex_colons.R:4:5): b ───────────
# Error in `force(x)`: object 'x' not found
# Backtrace:
#
#  1. └─base::force(x) at reprex_colons.R:4:5
# [ FAIL 1 | WARN 0 | SKIP 0 | PASS 0 ]
@hadley
Copy link
Member

hadley commented Oct 22, 2024

I agree that this seems like a bug, but since there's an obvious workaround (just use it() as we generally recommend), unfortunately I'm not going to have time to dig into it further.

@hadley hadley closed this as completed Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants