-
Notifications
You must be signed in to change notification settings - Fork 17
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
Allow error message like standard library assertions #25
Comments
Whats your use case? The error messages are quite involved and contain newlines and stuff. I'm thinking it would be quite cumbersome for a user to have to type all that. |
I'm not saying that the user should be able to replace the error messages that are already produced by something like Also from the perspective of writing readable tests, this allows you to specify the intention of the assertion with an error message that is immediately visible on failure instead of writing a comment. Let's take this somewhat contrived example: let tests = [
(Value::Number(1u8.into()), json!(1u8), "Numbers should be compared."),
(Value::String("hell".into()), json!("hello"), "Strings should be compared."),
];
for (expected, actual, test_name) in tests {
assert_json_eq!(expected, actual);
} This would currently print something like:
But this could instead be: for (expected, actual, test_name) in tests {
assert_json_eq!(expected, actual, "Error: {test_name}");
} With a message like:
|
The concrete use case that came up for me was when writing an integration test for a REST API. I want to compare both what's stored in the database and what's returned from the create endpoint. So it would look something like this: let expected_entity = json!(...);
assert_json_eq!(&expected_entity, entity_from_database, "Didn't store created entity to database correctly");
assert_json_eq!(&expected_entity, response_json, "Entity from HTTP response was incorrect"); Without the context, it's easy to mix up the two. |
Ah okay sorry! I misunderstood. I see it now. You just wanna print something extra with the error message thats already displayed. Yeah that sounds like a good idea! |
It would be great if the assertion macros were to support specifying an error message with format parameters as the standard library assertions do.
Example:
The text was updated successfully, but these errors were encountered: