Improve readability for IsNotNullOrEmpty statement with constraint based constraints

I am currently rewriting some unit tests to use NUnit 3 instead of NUnit 2 and you need to change some statements to statements based on restrictions. I have the following statements:

Assert.IsNullOrEmpty(result); 

What I changed to:

 Assert.That(result, Is.Null.Or.Empty); 

However, I'm not entirely happy with the readability of the IsNotNullOrEmpty statement:

 Assert.That(result, Is.Not.Null.And.Not.Empty); 

My current suggestion is to create the following static class:

 public static class Text { public static EmptyConstraint IsNullOrEmpty => Is.Null.Or.Empty; public static EmptyConstraint IsNotNullOrEmpty => Is.Not.Null.And.Not.Empty; } 

Using:

 Assert.That(result, Text.IsNotNullOrEmpty); 

This provides better readability by introducing a user restriction. Is there a standard way to make the same statement, or should I continue to use Is.Not.Null.And.Not.Empty instead?

+9
c # unit-testing nunit
source share
1 answer

Your statement for Is.Null.Or.Empty reads fine without the Test class. Moreover, when this statement fails, you know exactly what happened: you have a valid string object that is not empty.

The problems that I see with the "negative version" of the version of its denial, i.e. Is.Not.Null.And.Not.Empty , that it is too long, and it does not read almost as well as Is.Null.Or.Empty .

However, instead of making a separate restriction for him, I would have to defend my parts separately, i.e.

 Assert.That(result, Is.Not.Null); Assert.That(result, Is.Not.Empty); 

The reason I do this is because the two failure conditions do not overlap, i.e. result may be null , or it may be empty, but it cannot be simultaneously at the same time. In one connection statement, no distinction is made between the two situations, so you end up sending a debugger to determine if the result null or empty.

Separate statements, on the other hand, tell you what happens while remaining very readable. They "stand" for one additional line per statement; I think it's a reasonable cost to get more accurate information about potential failures.

+14
source share

All Articles