nunit
nunit copied to clipboard
Improve error message when comparing strings
[Test]
public void Test()
{
Assert.AreEqual(" ", "\u00A0"); // space and no breaking space
}
Results in:
String lengths are both 1. Strings differ at index 0.
Expected: " "
But was: " "
-----------^
Which looks confusing as they look the same.
Preferable would be:
String lengths are both 1. Strings differ at index 0.
Expected: " "
But was: "\u00A0"
-----------^
This makes sense to me since we already do \r
, \n
, \t
, \v
, \a
, \0
, and \\
.
(We missed \"
, I guess? 😄)
Would we simply use the \u
representation for all whitespace characters other than space and the existing escaped characters?
Not sure what is a good strategy here, maybe check the char that differs and print as is if both are ASCII? There are many symbols that will render similar. Greek omega: https://www.fileformat.info/info/unicode/char/03a9/index.htm Ohm: http://www.fileformat.info/info/unicode/char/2126/index.htm
Just one example, the list is probably very long.
What if we leave the display as-is and call out the mismatching numeric char
values no matter what they are?
also, please show the full expected and actual string value as it will make our life easier. The following is not enough, especially when a unit test passes locally but fails on a GitHub pipeline.
Error Message:
Expected string length 419 but was 422. Strings differ at index 14.
Expected: "8=FIX.4.4`9=396`35=8`6=0.667077`14=33025`15=AUD`17=450333-MOC..."
But was: "8=FIX.4.4`9=399`35=8`6=0.667077`14=33025`15=AUD`17=450333-MOC..."
-------------------------^
If you want to see all characters of a long string and not just the relevant part with the difference use the .NoClip
modifier:
Assert.That(s1, Is.EqualTo(s2).NoClip);
String lengths are both 256. Strings differ at index 100.
Expected: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
But was: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
---------------------------------------------------------------------------------------------------------------^
From the doc.nunit.org:
When an equality test between two strings fails, the relevant portion of both strings is displayed in the error message, clipping the strings to fit the length of the line as needed. Beginning with 2.4.4, this behavior may be modified by use of the NoClip modifier on the constraint. In addition, the maximum line length may be modified for all tests by setting the value of TextMessageWriter.MaximumLineLength in the appropriate level of setup.