Assertion Types Supported by xUnit
Get familiar with a wide range of assertion types supported by xUnit.
In this lesson, we’ll expand our knowledge of assertion types supported by xUnit. We’ll do so with the aid of the following playground:
namespace MainApp; public class DataManager { private readonly List<string?> _collection; public DataManager() { _collection = new List<string?>(); } public List<string?> ReadAll() { return _collection; } public void DeleteAt(int index) { _collection.RemoveAt(index); } public void DeleteAll() { _collection.Clear(); } public void Insert(string? newItem) { _collection.Add(newItem); } }
In this playground, we test a relatively simple CRUD app with the following twist: we add more assertion statements than necessary in most test methods. This demonstrates the types of validations available to us under each type of scenario.
Note: In a real-life project, having so many redundant assertions is considered bad practice. Ideally, we should only have enough assertions to validate the behavior we want.
Assertion types supported by xUnit
Let’s now group various assertion types into categories and see how they’re used in the code in the playground below. All our assertion statements are in the DataManagerTests.cs
class inside the MainApp.Tests
project.
Value assertions
These assertions verify that a specific field, property, or variable is of a specific value. This is achieved by invoking the following methods on the Assert
class: