-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Introduce expect snapshot testing library into rustc #75773
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
That's the thing i've mentioned in zullip |
This comment has been minimized.
This comment has been minimized.
5f46a7d
to
9cefe0e
Compare
This comment has been minimized.
This comment has been minimized.
9cefe0e
to
83c6b66
Compare
This comment has been minimized.
This comment has been minimized.
src/librustc_lexer/src/tests.rs
Outdated
/** outer doc block */ | ||
/*! inner doc block */ | ||
", | ||
expect A particular library we use, `expect_test` provides an `expect!` macro, which creates a sort of self-updating string literal (by using `file!` macro). Self-update is triggered by setting `UPDATE_EXPECT` environmental variable (this info is printed during the test failure). This library was extracted from rust-analyzer, where we use it for most of our tests. There are some other, more popular snapshot testing libraries: * https://github.com/mitsuhiko/insta * https://github.com/aaronabramov/k9 The main differences of `expect` are: * first-class snapshot objects (so, tests can be written as functions, rather than as macros) * focus on inline-snapshots (but file snapshots are also supported) * restricted feature set (only `assert_eq` and `assert_debug_eq`) * no extra runtime (ie, no `cargo insta`) See rust-lang/rust-analyzer#5101 for a an extended comparison. It is unclear if this testing style will stick with rustc in the long run. At the moment, rustc is mainly tested via integrated UI tests. But in the library-ified world, unit-tests will become somewhat more important (that's why use use `rustc_lexer` library-ified library as an example in this PR). Given that the cost of removal shouldn't be too high, it probably makes sense to just see if this flies!
83c6b66
to
f7be59c
Compare
@bors r=Mark-Simulacrum |
📌 Commit f7be59c has been approved by |
☀️ Test successful - checks-actions, checks-azure |
Snapshot testing is a technique for writing maintainable unit tests.
Unlike usual
assert_eq!
tests, snapshot tests allowto automatically upgrade expected values on test failure.
In a sense, snapshot tests are inline-version of our beloved
UI-tests.
Example:
A particular library we use,
expect_test
provides anexpect!
macro, which creates a sort of self-updating string literal (by using
file!
macro). Self-update is triggered by settingUPDATE_EXPECT
environmental variable (this info is printed during the test failure).
This library was extracted from rust-analyzer, where we use it for
most of our tests.
There are some other, more popular snapshot testing libraries:
The main differences of
expect
are:rather than as macros)
assert_eq
andassert_debug_eq
)cargo insta
)See rust-lang/rust-analyzer#5101 for a
an extended comparison.
It is unclear if this testing style will stick with rustc in the long
run. At the moment, rustc is mainly tested via integrated UI tests.
But in the library-ified world, unit-tests will become somewhat more
important (that's why use use
rustc_lexer
library-ified library asan example in this PR). Given that the cost of removal shouldn't be
too high, it probably makes sense to just see if this flies!