Open dbsxdbsx opened 1 year ago
Tests already have this ability through the #[should_panic]
attribute
Tests already have this ability through the
#[should_panic]
attribute
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.
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
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 #[should_panic]
does not work with things like quickcheck
. That necessitates custom handling similar to what this macro would provide. I incidentally ended up implementing something like this yesterday in an attempt to narrow down a bug. I'm not saying it should be used often, but it definitely has its use cases.
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.