user746461
user746461

Reputation:

Output additional information when tests fail

One of my test containing Assert.Equal(2, list.Count); fails on Appveyor, a continuous integration server, but I cannot reproduce the failure on my local machine.

enter image description here

I hope to get more information from the error message, but don't know how to do.

The authors of xUnit.net insist they should not allow users to specify custom error messages, see https://github.com/xunit/xunit/issues/350 . That's why there is not API allowing me to write eg. Assert.Equal(2, list.Count, "The content of the list is " + ...);

I also looked at Fluent Assertions. If I write list.Should().HaveCount(3, "the content of the list is " + ...);, the output reads as

Expected collection to contain 3 item(s) because the content of the list is
..., but found 2.

The "because" clause doesn't make sense in English grammar. The because parameter seems to be used to describe expected behavior, not actual behavior.

Considering xUnit.net and Fluent Assertions both discourage us from providing additional information regarding the failure, is outputting additional information when tests fail a good way to debug remote errors?

What's the best way to output additional information?

Upvotes: 2

Views: 1435

Answers (1)

Dennis Doomen
Dennis Doomen

Reputation: 8899

If you want to see the actual contents of the list for debugging purposes and you're using Fluent Assertions, you can do this:

using (new AssertionScope(Formatter.ToString(list)) { list.Should().HaveCount(3); }

The assertion scope will replace the collection part of the message with something else. It's not nice, but will work.

Alternatively, you could exploit the because parameter like this:

list.Should().HaveCount(3, "because I expected list {0} to contain that many items", list);

FA will format every placeholder in the because phrase using that same Formatter.String.

Upvotes: 2

Related Questions