Repository navigation
Conversation
… setup when the tests end
…t test setups as skipped
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Some test files remove objects that testthat removes anyway. Other code in the tests changes the R session and does not undo the change:
setup.Rand several test files set options and the random seed, andsetup.Rcopies objects into the global environment. On master, adevtools::test()run oftest_misc.Ralone leaves 18 changed options in the session; on this branch it leaves none. The new restores also run when a test fails.This PR does not change the tested code. In two places it changes how a test fails or skips (items 4 and 6).
There is a plan for further testthat cleanup, but I'm making separate PRs to keep these easier to review
This PR was assisted by Claude.
Changes
One commit per item:
Remove the
ls_bu <- ls()...rm(list = setdiff(ls(), ls_bu))blocks (7 files). testthat runs each test file in its own environment, so these blocks never removed anything that reached another file.Restore options and the random seed after an error too:
withr::local_seed()andwithr::local_preserve_seed()replace therng_oldsave and restore blocks, andwithr::with_seed()replaces the short ones. The old blocks did not run when a test failed, andexists("rng_old")also found anrng_oldof an enclosing environment. testthat itself does not restore the random seed.withr::with_options()andwithr::local_options()replace thex_orig <- options(...)...options(x_orig)pairs.withr (>= 3.0.3)toSuggestsinDESCRIPTION. withr 3.0.2 has a bug:local_seed()does not restore the seed (fixed in 3.0.3, Problem with local_seed? r-lib/withr#286).setup.Rcleans up when the tests end (withwithr::defer(envir = testthat::teardown_env())):f_<family>objects that it copies into the global environment.test_parallel.Rresets its options and the parallel backend at the end of the file, also after an error.Keep test state out of the global environment and report empty tests:
test_div_minimizer.Rpasses the results between its tests in a file-level environment instead of the global environment. Before this change, the comparison test could use stale objects from an earlier run in the same session.test_varsel.Rundoes its<<-change oftrms_universe_splitwithwithr::defer().test_misc.Rchecks a fixture withstopifnot()instead of a skip.skip_if_no_setups()(tests/testthat/helpers/skips.R) skips the 11 tests that only loop overrefmodsordatafits, with a reason. On CRAN, these lists are empty, and testthat reported the tests as empty.test_helpers_tst.Rchecks thatskip_if_no_setups()skips only for an empty list.Remove the 30
context()calls (deprecated in testthat 3e).test_augdat.R: for K-fold CV only, if the comparison on the logit scale (log scale) fails, the test compares on the probability scale (exp scale). Before this change,tryCatch(expect_equal(...), error = ...)hid the failure of the first comparison, and for the other CV methods a failure ended as an error instopifnot(is_kfold). Now the first comparison is anall.equal()check, and the other CV methods get a normalexpect_equal()failure.test_varsel.R: the rstanarmcvfitstest reports failedkfold()refits as a skip with the setup names and the error messages, instead of amessage().NEWS.md: one "Tests:" entry.Not changed
setup.Rdrops test setups whosevarsel(),cv_varsel()orproject()call fails with a known error message (for example the gamm4getVb()problem). A new bug that gives the same message reduces the coverage with no failure. An explicit list of the setups that are allowed to fail would make this visible. I propose this as a separate change.test_proj_predfun.R,test_project.Randtest_varsel.Rcompare two code paths with tolerances of 1e-14 or 1e-15. They pass on Roihu. The result can depend on the BLAS library. I did not change them, because no case fails._snapsis in.gitignoreand.Rbuildignore.run_snapsis off on CI and inR CMD check.Tests
With
NOT_CRAN=true, the full suite gives the same result on master and on this branch, except for the new test ofskip_if_no_setups()(1 test, 2 expectations): 253 and 254 tests, 55,803 and 55,805 expectations, 0 failures. In CRAN mode (installed package,NOT_CRAN=false), master gives 253 tests and 927 expectations, and this branch 254 tests and 929 expectations; both have 180 skipped tests and 0 failures. The skip count is the same, because testthat already counted the empty tests as skipped. Now each of these skips states a reason. Tested with R 4.6.1, testthat 3.3.2, withr 3.0.3 and the development version of lme4 (2.1-1) on CSC Roihu (Linux, AMD EPYC).Overlap with other PRs
tests/testthat/test_glm_ridge.R, where this PR removes the cleanup and the seed blocks. The conflict is textual; keep both changes when you resolve it.context()calls. Faster augmented-data projection for ordinal and categorical submodels #583 and Faster projections onto multilevel and additive submodels #584 also addrng_oldblocks, and Faster augmented-data projection for ordinal and categorical submodels #583 addsls_bublocks. A follow-up PR can convert them after they are merged. (Vectorize the draw-wise predictive variance of the Gaussian and Student-t #575, Cluster the draws in the coordinates of their low-dimensional span #576, Avoid repeated formula updates, term extraction and training-data model matrices #578 and Gaussian forward search and validated LOO evaluation from cross-products of the model matrix #579 are not checked.)