NUnit Assert class is used to determine whether a particular test method gives expected result or not.
In a test method, we write code the check the business object behavior. That business object returns a result. In Assert method we match the actual result with our expected result. If result comes according to our expected result then our test case is passed else failed.
Constraint Model
NUnit provides a new Constraint Model to improve the test method readability. In constraint model, we use a single method “That” and specify constraints to check our expected response.
That method applies a constraint to the actual value. If a constraint is satisfied our test case is succeeded else failed.
Helper Classes
Below are helper classes to provide a constraint to assert the method.
- Is
- Has
- Contains
- Does
- Throws
Constraint Categories
These constraints can be divided into eight categories:
Comparison Constraints
Assert.That(result, Is.EqualTo(5));
Assert.That(result, Is.Not.EqualTo(7));
Greater Than Constraint Example
Assert.That(result, Is.GreaterThan(2)); Assert.That(result, Is.GreaterThanOrEqualTo(5));
Assert.That(result, Is.LessThan(9)); Assert.That(result, Is.LessThanOrEqualTo(5));
Assert.That(result, Is.InRange(5, 10));
String Constraints
String Equal / Not Equal Constraint Example
Assert.That(result, Is.EqualTo("abcdefg"));
Assert.That(result, Is.Not.EqualTo("mnop"));
String Equal Ignore Case Example
Assert.That(result, Is.EqualTo("ABCDEFG").IgnoreCase);
Assert.That(result, Does.Contain("def").IgnoreCase);
Assert.That(result, Does.Not.Contain("igk").IgnoreCase);
Assert.That(result, Is.Empty); Assert.That(result, Is.Not.Empty);
Starts With / Ends With Examples
Assert.That(result, Does.StartWith("abc"));
Assert.That(result, Does.Not.StartWith("efg"));
Assert.That(result, Does.EndWith("efg"));
Assert.That(result, Does.Not.EndWith("mno"));
string result = "abcdefg";
Assert.That(result, Does.Match("a*g"));
Assert.That(result, Does.Not.Match("m*n"));
Collection Constraints
All Items Examples
int[] array = new int[] { 1, 2, 3, 4, 5 };
Assert.That(array, Is.All.Not.Null);
Assert.That(array, Is.All.GreaterThan(0));
Assert.That(array, Is.All.LessThan(10));
Assert.That(array, Is.All.InstanceOf<Int32>());
Assert.That(array, Is.Empty); Assert.That(array, Is.Not.Empty);
Assert.That(array, Has.Exactly(5).Items);
Assert.That(array, Is.Unique);
Assert.That(array, Contains.Item(4));
Ordered Examples
Assert.That(array, Is.Ordered.Ascending);
Assert.That(array, Is.Ordered.Descending);
List<Employee> employees = new List<Employee>();
employees.Add(new Employee { Age = 32 });
employees.Add(new Employee { Age = 49 });
employees.Add(new Employee { Age = 57 });
Assert.That(employees, Is.Ordered.Ascending.By("Age"));
Assert.That(employees, Is.Ordered.Descending.By("Age"));
Assert.That(employees, Is.Ordered.Ascending.By("Age").Then.Descending.By("Name"));
int[] array = new int[] { 1, 2, 3, 4, 5 };
int[] array2 = { 3, 4 };
Assert.That(array2, Is.SubsetOf(array));
Conditional Constraints
Assert.That(array, Is.Null); Assert.That(array, Is.Not.Null);
bool result = array.Length > 0; Assert.That(result, Is.True); Assert.That(result, Is.False);
Assert.That(array, Is.Empty);
Compound Constraints
Assert.That(result, Is.GreaterThan(4).And.LessThan(10));
Assert.That(result, Is.LessThan(1).Or.GreaterThan(4));
Assert.That(result, Is.Not.EqualTo(7));
Directory / File Constraints
Assert.That(new FileInfo(path), Does.Exist);
Assert.That(new FileInfo(path), Does.Not.Exist);
Assert.That(new DirectoryInfo(path), Does.Exist);
Assert.That(new DirectoryInfo(path), Does.Not.Exist);
Assert.That(path, Is.SamePath(@"c:\documents\imp1").IgnoreCase);
Empty Directory. Is.Empty returns true when directory has no files.
Assert.That(new DirectoryInfo(path), Is.Empty);
Type / Reference Constraints
IEmployee emp = new Employee();
Assert.That(emp, Is.InstanceOf<IEmployee>());
Assert.That(emp, Is.Not.InstanceOf<string>());
Assert.That(emp, Is.TypeOf<Employee>());
For e.g. interface to implemented class.
Assert.That(emp, Is.AssignableTo<Employee>());
Exceptions Constraints
Is Exception Throws By Method
IEmployee emp = new Employee();
emp.Age = 0;
Assert.That(emp.IsSeniorCitizen(), Throws.Exception);
Expected / Same Type Exception
Assert.That(emp.IsSeniorCitizen(), Throws.TypeOf<ArgumentException>());
Exception ex = Assert.Throws<ArgumentException>(() => emp.IsSeniorCitizen());
Assert.That(ex.Message, Is.EqualTo("Age can not 0."));