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

Start using testthat 3e #910

Merged
merged 11 commits into from
Mar 11, 2022
Merged

Start using testthat 3e #910

merged 11 commits into from
Mar 11, 2022

Conversation

MichaelChirico
Copy link
Collaborator

No description provided.

@MichaelChirico MichaelChirico changed the title Start using testthat 3e WIP: Start using testthat 3e Mar 4, 2022
@MichaelChirico
Copy link
Collaborator Author

We have a bunch of migrations to do here..

@MichaelChirico MichaelChirico changed the title WIP: Start using testthat 3e Start using testthat 3e Mar 9, 2022
@MichaelChirico MichaelChirico requested a review from AshesITR March 9, 2022 10:37
@MichaelChirico
Copy link
Collaborator Author

Finished the first pass here. Notes:

  • Couldn't finish all the with_mock() migrations. I don't understand how to use mockery() for some of them. I raised a comment on a similar mockery issue here: Stubbing out a function called within S3 dispatch mockery#43. Any ideas?
  • It looks like the code tested test-cache has been refactored -- the with_mock() appear to be no-ops, so I just removed them. Let's see.
  • Also in test-cache, expect_equal(e1, e2) became the much uglier expect_identical(as.list(e1), as.list(e2)). Raised Documentation request: how to compare environments? waldo#127 about it.
  • Also did some more general clean-ups of tests, e.g. preferring withr::local_* functions over withr::with_* and using withr more comprehensively
  • The tests in test-get_source_expressions.R are in incomprehensible mess IMO (and I've only made it worse with the expect_identical() shift) -- we should file a follow-up to refactor it to a more maintainable approach

Copy link
Collaborator

@AshesITR AshesITR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine so far

NEWS.md Outdated Show resolved Hide resolved
tests/testthat/test-cache.R Show resolved Hide resolved
tests/testthat/test-knitr_formats.R Outdated Show resolved Hide resolved
tests/testthat/test-rstudio_markers.R Show resolved Hide resolved

expect_equal(e1, e2)
)
expect_identical(as.list(e1), as.list(e2))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use this approach, I think you need all.names = TRUE to verify the property implied by the test title.

OTOH I'm not sure what "non-hidden" means as I would have expected hidden to imply a variable starting with ., but that doesn't appear in the test.

Additionally, I'd use expect_equal() here because expect_identical() just implies exact numerical comparisons, which I don't think is important here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's not my reading of the docs (?expect_identical):

expect_identical() is the baseline; expect_equal() relaxes the test to ignore small numeric differences.

i.e., only use expect_equal to ignore numerical differences

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. I guess it missing another sentence that says something like: "Since in most cases tiny numeric differences aren't important, we recommend using expect_equal()."

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈 our internal policy encourages expect_identical() now and we've basically switched all the expect_equal() tests already, except where ignore*= or tolerance= are needed. I have come to quite like it TBH. It encourages stronger tests.

tests/testthat/test-cache.R Outdated Show resolved Hide resolved
@MichaelChirico
Copy link
Collaborator Author

Hmm @AshesITR the new helper function uses rlang as suggested in the testthat vignette: https://testthat.r-lib.org/articles/custom-expectation.html

That means we need a Suggests for rlang, but testthat already Imports rlang, so our total dep tree doesn't really change. WDYT?

Copy link
Collaborator

@AshesITR AshesITR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, thanks!

@MichaelChirico MichaelChirico merged commit 23b06bd into master Mar 11, 2022
@MichaelChirico MichaelChirico deleted the MichaelChirico-patch-2 branch March 11, 2022 19:55
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

Successfully merging this pull request may close these issues.

3 participants