tinytest icon indicating copy to clipboard operation
tinytest copied to clipboard

Running `rm(list = ls())` in a test script

Open lindeloev opened this issue 3 years ago • 5 comments

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!

lindeloev avatar May 03 '21 11:05 lindeloev

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.

markvanderloo avatar May 03 '21 12:05 markvanderloo

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!

lindeloev avatar May 03 '21 14:05 lindeloev

note to self: I could make ls() not return the objects that tinytest creates.

markvanderloo avatar May 07 '21 14:05 markvanderloo

What about attach():ing such helper functions in a separate environment on search()?

HenrikBengtsson avatar May 08 '21 16:05 HenrikBengtsson

That's actually a really interesting idea. I will try that out. Thanks!

markvanderloo avatar May 12 '21 18:05 markvanderloo