nUnit is an open-source unit testing framework for the .NET Framework and Mono. By using nUnit you can test your Services, but also your Controllers, Actions, etc. It was initially ported from JUnit (a Java unit testing framework) and it has been developed and continually rewritten with many new features and support for a wide range of .NET framework versions.
You can learn more about nUnit be navigating to their open source GitHub repository, but which are some must-know helpers that you can use when testing your apps?
nUnit has 5 helper classes:
- Is
- Has
- Contains
- Does
- Throws
These helper classes can be used with the constrain model that NUnit offers for better method readability. In the constrained model, the most important method that you can use is the that() method. Inside this method, we specify the expected response which we use to check against the actual response.
But, which are these constrain categories. Constain categories can be divided into eight categories:
- Comparison
- String
- Collection
- Conditional
- Compound
- Directory/File
- Type/Reference
- Exceptions
Let us have a look at all these categories and their methods including some examples.
1. Comparison
Comparison category has 5 methods that you can use.
Equal
int result = 5;
Assert.That(result, IsEqualTo(5));
Not Equal
int result = 5;
Assert.That(result, Is.Not.EqualTo(13));
Greater Than
int result = 5;
Assert.That(result, Is.GreaterThan(1));
Assert.That(result, Is.GreaterThanOrEqualTo(5));
Less Than
int result = 5;
Assert.That(result, Is.LessThan(10));
Assert.That(result, Is.LessThanOrEqualTo(5));
Ranges
int result = 5;
Assert.That(result, Is.InRange(1, 10));
Assert.That(result, Is.LessThanOrEqualTo(4, 8));
2. String
Equal / Not Equal
string blogName = "dotnethow";
Assert.That(blogName, Is.EqualTo("dotnethow"));
Assert.That(blogName, Is.Not.EqualTo("dotnethow-not"));
Equal with Ignore Case
string blogName = "dotnethow";
Assert.That(blogName, Is.EqualTo("dotNEThow").IgnoreCase);
Contains substring
string blogName = "dotnethow";
Assert.That(blogName, Does.Contain("net"));
Assert.That(blogName, Does.Not.Contain("NET").IgnoreCase);
Empty
string blogName = "dotnethow"; string blogNameEmpty="";
Assert.That(blogNameEmpty, Is.Empty);
Assert.That(blogName, Is.Not.Empty);
Starts/Ends With
string blogName = "dotnethow";
Assert.That(blogName, Does.StartWith("dot"));
Assert.That(blogName, Does.Not.StartWith("net"));
Assert.That(blogName, Does.EndWith("how"));
Assert.That(blogName, Does.Not.EndWith("net"));
Regex/Match
string blogName = "dotnethow";
Assert.That(blogName, Does.Match("d*w"));
Assert.That(blogName, Does.Not.Match("m*n"));
3. Collection
Not Null
int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
Assert.That(array, Is.All.Not.Null);
All Greater Than
int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
Assert.That(array, Is.All.GreaterThan(0));
All Less Than
int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
Assert.That(array, Is.All.LessThan(0));
Instance Of
int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
Assert.That(array, Is.All.InstanceOf<Int32>());
Empty
int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
Assert.That(array, Is.Empty);
Assert.That(array, Is.Not.Empty);
Number of items
int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
Assert.That(array, Has.Exactly(8).Items);
Unique Items
int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
Assert.That(array, Is.Unique);
Contains Item
int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
Assert.That(array, Contains.Item(4));
Ordered in Ascending/Descending
int[] arrayAscending = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
Assert.That(arrayAscending , Is.Ordered.Ascending);
int[] arrayDescending = new int[]{4, 3, 1};
Assert.That(arrayDescending , Is.Ordered.Descending);
By Single Property/Multiple Properties
List<Book> books = new List<Book>();
books.Add(new Book(){ Title = "Book 1", NrOfPages = 450 });
books.Add(new Book(){ Title = "Book 2", NrOfPages = 350 });
books.Add(new Book(){ Title = "Book 3", NrOfPages = 150 });
Assert.That(books, Is.Ordered.Ascending.By("Title"));
Assert.That(books, Is.Ordered.Descending.By("Title"));
Assert.That(books, Is.Ordered.Ascending.By("Title").Then.Descending.By("NrOfPages"));
Subset/SuperSet
int[] array1 = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
int[] array2 = new int[]{4, 5, 6};
Assert.That(array2 , Is.SubsetOf(array1));
Assert.That(array1 , Is.SupersetOf(array2));
4. Conditional
Null
int[] array = new int[]{4, 5, 6};
Assert.That(array, Is.Null);
Assert.That(array, Is.Not.Null);
Boolean
int[] array = new int[]{4, 5, 6};
bool result = array.Length > 1;
Assert.That(result, Is.True);
Assert.That(result, Is.False);
Empty
int[] array = new int[]{4, 5, 6};
Assert.That(array, Is.Empty);
5. Compound
AND
int result = 10;
Assert.That(result, Is.GreaterThan(5).And.LessThan(20));
OR
int result = 10;
Assert.That(result, Is.GreaterThan(5).Or.LessThan(20));
NOT
int result = 10;
Assert.That(result, Is.Not.EqualTo(5));
6. Directory/File
File/Directory Exists
Assert.That(new FileInfo(filePath), Does.Exist);
Assert.That(new FileInfo(filePath), Does.Not.Exist);
Assert.That(new DirectoryInfo(directoryPath), Does.Exist);
Assert.That(new DirectoryInfo(directoryPath), Does.Not.Exist);
Same Path
Assert.That(filePath, Is.SamePath(@"c:\docs\folder\filehere").IgnoreCase);
Directory Is Empty
Assert.That(new DirectoryInfo(directoryPath), Is.Empty);
7. Type/Reference
Instance
IBook book = new Book();
Assert.That(book, Is.InstanceOf<IBook>());
Assert.That(book, Is.Not.InstanceOf<string>());
Exact Same Type
IBook book = new Book();
Assert.That(book, Is.TypeOf<Book>());
Assingable
IBook book = new Book();
Assert.That(book, Is.AssignableTo<Book>());
8. Exceptions
Is Exception Thrown
var booksService = new BooksService();
Assert.That(books.GetAllBooks(), Throws.Exception);
Expected/Same Type Exception
var booksService = new BooksService();
Assert.That(books.GetBookById(), Throws.Exception.TypeOf<ArgumentException>());
Exception Message
var booksService = new BooksService();
Assert.That(() => booksService.Add(null), Throws.Exception.TypeOf<ArgumentNullException>()
.With.Message.EqualTo($"Please, provide a book title"));