Assert! message best pratices?

Hi,

I am creating integration tests.
I realize that we can pass a message with assert! like this for example :

const MY_CUSTOM_ERROR :&str = "Error occurs when...";
assert!(false, "{}", MY_CUSTOM_ERROR);

But I wonder if you have eventually best pratices when using assert! message ?

Have a nice day !

In my opinion, the most important quality for any test failure message to have is that it should give enough information for the reader to understand what the test failure means without having to modify the test, attach a debugger, or (ideally) even read the test code in detail. (This is so that, if a change causes multiple test failures, the developer can scan the failures to gain an understanding of what they broke when they weren’t expecting to break anything.)

Given that principle, the best message depends entirely on what the assertion is actually asserting. (And an assert_eq! may not need any custom message, if the test name and the values printed make things clear enough.)

I don’t see what benefit you gain from declaring a const here.

2 Likes

The const is because I can have a lot of assert in my tests with the same message...

Thank you for your answer.
I try to give the explicit reasons while the assert fails with the expected value at the moment.
I compare response and data expected...

Consider if the assert_eq default message (which prints both values being compared) will suffice, when combined with the test name.

4 Likes

Sorry I have others questions... Maybe best in another topic...