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
Proposal: Introducing the assert_panic!
Macro for Testing Panic Messages in Rust
#3479
Comments
Tests already have this ability through the |
I know. But what if there needs more than one panic check in a unit test? |
This feels like something niche enough to be provided by an external crate. |
Agreed - https://github.com/Tamschi/assert-panic looks like it does a lot of things pretty nice. It does look unmaintained though. @dbsxdbsx if you are interested in the topic, maybe you'd like to reach out to the owner and offer to help with maintenance |
If possible, I'd like to see Rust's official team take over it. |
I will note that |
Introduction
In Rust, the standard library provides macros like
assert!
andpanic!
for handling errors and testing code correctness. However, there is no built-in macro for capturing a panic and checking its message. This proposal aims to introduce a new macro,assert_panic!
, which will allow developers to test if a specific panic message is triggered in their code.Motivation
When writing tests in Rust, it is often necessary to ensure that a function or method panics under certain conditions. While the standard library provides macros like
assert!
andpanic!
, they do not offer a way to capture the panic message and check if it matches the expected message.Having a macro like
assert_panic!
would simplify the process of testing panic messages and make it more convenient for developers to verify that their code behaves as expected in panic scenarios.Proposed Solution
The
assert_panic!
macro will be designed to capture a panic and check if its message matches the expected message. Here's an example of how the macro can be used:The
assert_panic!
macro will be implemented as follows:Without this, users have to manually write code like this:
Other thoughts
I don't know if this behavior is not encouraged or not, I see the macro
assert_ne!
is merged from crate assert_ne, while the similar assert-panic didn't.Conclusion
Introducing the
assert_panic!
macro will provide a convenient way for Rust developers to test panic messages in their code. This macro will simplify the process of verifying that a function or method panics with the expected message, making it easier for developers to ensure that their code behaves correctly in panic scenarios.The text was updated successfully, but these errors were encountered: