| Crates.io | should_error |
| lib.rs | should_error |
| version | 0.1.1 |
| created_at | 2025-03-27 03:02:07.145414+00 |
| updated_at | 2025-03-27 07:21:47.248898+00 |
| description | The test should fail with Err. |
| homepage | |
| repository | https://github.com/PRO-2684/should_error |
| max_upload_size | |
| id | 1607475 |
| size | 5,844 |
should_errorNOTE: This crate is deprecated in favor of
should_match.
Pass a test if an error is returned.
This crate is primarily intended for use in tests, so add it to your dev-dependencies instead of dependencies:
cargo add --dev should_error
Recommended to work with macro_rules_attr:
cargo add --dev macro_rules_attr should_error
macro_rules_attrSimply apply the should_error macro (the order does not matter):
use macro_rules_attr::apply;
use should_error::should_error;
// This test will pass
#[apply(should_error)]
#[test]
fn test_apply_first() -> Result<(), &'static str> {
Err("error")
}
// This test will also pass
#[test]
#[apply(should_error)]
fn test_apply_second() -> Result<(), &'static str> {
Err("error")
}
To further specify the pattern that's expected to match, use the expected argument:
# use macro_rules_attr::apply;
# use should_error::should_error;
#
#[test]
#[apply(should_error, expected = Err("error"))]
fn test_with_error_expected() -> Result<(), &'static str> {
Err("error")
}
You can also match anything else other than an Err:
# use macro_rules_attr::apply;
# use should_error::should_error;
#
#[allow(dead_code, reason = "Only used in tests")]
enum MyEnum {
One,
Two,
}
#[test]
#[apply(should_error, expected = MyEnum::One)]
fn test_with_arbitrary_expected() -> MyEnum {
MyEnum::One
}
Wrap your tests with should_error! (note that the #[test] attribute must be wrapped inside the macro):
use should_error::should_error;
// This test will pass
should_error! {
#[test]
fn test() -> Result<(), &'static str> {
Err("error")
}
}
// This is not a valid test: the `#[test]` attribute may only be used on a non-associated function
#[test]
should_error! {
fn test() -> Result<(), &'static str> {
Ok(())
}
}
To further specify the pattern that's expected to match, use the expected argument:
# use should_error::should_error;
#
should_error! {
#[test]
fn test_with_error_expected() -> Result<(), &'static str> {
Err("error")
}, expected = Err("error")
}
You can also match anything else other than an Err:
# use should_error::should_error;
#
# #[allow(dead_code, reason = "Only used in tests")]
enum MyEnum {
One,
Two,
}
should_error! {
#[test]
fn test_with_arbitrary_expected() -> MyEnum {
MyEnum::One
}, expected = MyEnum::One
}