-
Notifications
You must be signed in to change notification settings - Fork 340
Incorporate doc feedback from @maelle #2231
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,35 @@ This vignette shows you how to write your own expectations. Custom expectations | |
|
|
||
| In this vignette, you'll learn about the three-part structure of expectations, how to test your custom expectations, see a few examples, and, if you're writing a lot of expectations, learn how to reduce repeated code. | ||
|
|
||
| ## Do you need it? | ||
|
|
||
| But before you read the rest of the vignette and dive into the full details of creating a fully 100% correct expectation, consider if you can get away with a simpler wrapper. For example, take this expectation from tidytext: | ||
|
||
|
|
||
| ```{r} | ||
| # from tidytext | ||
| expect_nrow <- function(tbl, n) { | ||
| expect_s3_class(tbl, "data.frame") | ||
| expect_equal(nrow(tbl), n) | ||
| } | ||
| ``` | ||
|
|
||
| If we use it in a test you can see there's a subtle problem: | ||
|
|
||
| ```{r} | ||
| #| error: true | ||
| test_that("success", { | ||
| expect_nrow(mtcars, 32) | ||
| }) | ||
| test_that("failure 1", { | ||
| expect_nrow(mtcars, 30) | ||
| }) | ||
| test_that("failure 2", { | ||
| expect_nrow(matrix(1:5), 2) | ||
| }) | ||
| ``` | ||
|
|
||
| Each of these tests contain a single expectation, but they report a total of 2 successes and failures. It would be confusing if testthat didn't report these numbers correctly. But as a helper in your package, it's probably not a big deal and you save yourself some pain by not reading this vignette 😀. You might also notice that these failures generate a backtrace whereas most failures don't. Again this is not a big deal, and the backtrace is correct, it's just not needed. | ||
|
|
||
| ## Expectation basics | ||
|
|
||
| An expectation has three main parts, as illustrated by `expect_length()`: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.