Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit Assert AreEqual strings diff

I'm writing unit tests for a code generator, and most of my tests compare output that spans several lines against some golden output. Currently, I'm comparing outputs with Assert.AreEqual(string, string) which displays the context around the first difference.

Unfortunately, this isn't very helpful. Ideally, I'd like to get a diff of the outputs. What is the best way of doing this? My current plan is to write the generated code to a file and call the executable referenced by the DIFFTOOL env var (else diff.exe from PATH, else the default NUnit behavior) and call Assert.Fail() with an appropriate message.

like image 584
Alex Reinking Avatar asked Jan 04 '23 19:01

Alex Reinking


1 Answers

You can eliminate the clipping of long strings by specifying .NoClip as a modifier of an EqualConstraint. This requires you to switch from the legacy Assert.AreEqual to use the constraint model of assertion:

C# Assert.That(actual, Is.EqualTo(expected).NoClip);

This will give you the full value of both strings, but may be hard to read without reformatting.

We have long had an interest in improving the display of differences between two long strings. Come and do a PR! :-)

like image 63
Charlie Avatar answered Jan 13 '23 22:01

Charlie