Skip to content

Start using testthat 3e - #910

Merged
MichaelChirico merged 11 commits into
masterfrom
MichaelChirico-patch-2
Mar 11, 2022
Merged

MichaelChirico merged 11 commits into
masterfrom
MichaelChirico-patch-2

Conversation

@MichaelChirico

Copy link
Copy Markdown
Collaborator

No description provided.

@MichaelChirico MichaelChirico mentioned this pull request Mar 4, 2022
10 tasks done
@MichaelChirico MichaelChirico changed the title Start using testthat 3e WIP: Start using testthat 3e Mar 4, 2022
@MichaelChirico

Copy link
Copy Markdown
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
Copy Markdown
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

@AshesITR AshesITR left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks fine so far

Comment thread NEWS.md Resolved
Comment thread tests/testthat/test-cache.R Resolved
Comment thread tests/testthat/test-knitr_formats.R Resolved
Comment thread tests/testthat/test-rstudio_markers.R Resolved
Comment thread tests/testthat/test-cache.R Outdated

expect_equal(e1, e2)
)
expect_identical(as.list(e1), as.list(e2))

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread tests/testthat/test-cache.R Resolved
@MichaelChirico

Copy link
Copy Markdown
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?

Comment thread tests/testthat/test-cache.R Resolved

@AshesITR AshesITR left a comment

Copy link
Copy Markdown
Collaborator

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