tinytest
tinytest copied to clipboard
Running `rm(list = ls())` in a test script
I'm loving tinytest
, but hit a roadblock while implementing tests for the job
package. I called rm(list = ls())
in the top of the test and this caused tinytest
to run but not collect test results:
> tinytest::test_all()
# Running test_addins.R................. 0 tests 0.1s
# Running test_job.R.................... 0 tests 28.3s
# All ok, 0 results (28.4s)
It took me some debugging to learn that tinytest
fills the testing environment with functions etc. while testing and that rm()
was the cause of this behavior. Calling ls()
from within a test-script yielded:
c('at_home', 'checkEqual', 'checkEqual_to_reference', 'checkEquivalent', 'checkEquivalent_to_reference', 'checkError', 'checkFalse', 'checkIdentical', 'checkInherits', 'checkMessage', 'checkNull', 'checkSilent', 'checkStdout', 'checkTrue', 'checkWarning', 'exit_file', 'expect_equal', 'expect_equal_to_reference', 'expect_equivalent', 'expect_equivalent_to_reference', 'expect_error', 'expect_false', 'expect_identical', 'expect_inherits', 'expect_message', 'expect_null', 'expect_silent', 'expect_stdout', 'expect_true', 'expect_warning', 'ignore', 'options', 'report_side_effects', 'Sys.setenv', 'using')
Request: Would it would be possible run tinytest::test_all()
without filling the test environment this way, i.e., staying within the tinytest
´namespace like other packages do?
Motivation: Because the job()
package exports stuff from the calling environment and imports results from an RStudio job, I want to test which variables are in environments, i.e., calling ls()
before/after launching job::job()
. So this inconvenient. Naturally, I can just make a list of pre-defined stuff in the start of the test and test differences to that, but if there's a way to collect results without filling the test environment, that would be great!
I don't think its easy to fulfill that request as it is part of how tinytest works (it uses locally, and dynamically masked functions to capture test output). A workaround is to do tt <- ls()
at the beginning of your script and to do rm(list=setdiff(ls(), tt))
where needed. I will see if I can make this a bit easier.
For now, I'll be using testthat
for this particular purpose as it doesn't assign anything to the test environment. Implicitly assigning variables in the environment is hopefully an edge case that few packages would make use of, so this "feature" isn't a priority for me. So feel free to close this issue if it's a "wontfix".
I'm enjoying tinytest
for other projects. Thanks!
note to self: I could make ls()
not return the objects that tinytest
creates.
What about attach():ing such helper functions in a separate environment on search()?
That's actually a really interesting idea. I will try that out. Thanks!