Unit Testing in C#
  • Unit testing in C#
  • Unit testing
    • What to test
    • When to test
    • Qualities of a good unit test suite
    • Qualities of a good unit test
    • Dealing with dependencies
    • Running the tests
  • NUnit
    • Quick glance at NUnit
    • Creating a NUnit test project
    • Anatomy of a test fixture
    • Lifecycle of a test fixture
    • Assertions
    • Asynchronous executions
    • Parameterized tests
    • Assumptions
    • Describing your tests
  • Moq
    • Quick glance at Moq
    • Method arguments
    • Method calls
    • Properties
    • Results
    • Callbacks
    • Exceptions
    • Events
    • Verifications
    • Base class
    • Mock customization
    • Implicit mocks
    • Mock repository
    • Custom matchers
    • Multiple interfaces
    • Protected members
    • Generic methods
    • Delegates
  • AutoFixture
    • Quick glance at AutoFixture
    • Fixture
    • Create and Build
    • Type customization
    • Data annotations
    • Default configurations
    • Building custom types
    • Relays
    • Tricks
    • Idioms
    • Integration with NUnit
    • Integration with Moq
    • Combining AutoFixture with NUnit and Moq
    • Extending AutoFixture
  • Advanced topics
    • Testing HttpClient
Powered by GitBook
On this page
  1. NUnit

Asynchronous executions

C# 5.0 baked asynchrony into the language by introducing the keywords async and await. Given how asynchronous code is handled in C#, many components now expose asynchronous methods that need to be properly tested.

NUnit supports out of the box asynchronous methods by wrapping properly executing them in an asynchronous context and unwrapping eventual exception raised by the code itself or by failed assertions. To make sure the runner properly waits for the completion of the asynchronous tests, these cannot be async void methods.

Here are two tests testing the synchronous and asynchronous methods of the same component.

public class FileLoader
{
    public string Load(string filePath) { ... }

    public Task<string> LoadAsync(string filePath) { ... }
}

[Test]
public void Load_retrieves_file()
{
    // ARRANGE
    var fileName = "my_file.txt";

    var sut = new FileLoader();

    // ACT
    var result = sut.Load(fileName);

    // ASSERT        
    Assert.That(result, Is.Not.Null);
}

[Test]
public async Task LoadAsync_retrieves_file()
{
    // ARRANGE
    var fileName = "my_file.txt";

    var sut = new FileLoader();

    // ACT
    var result = await sut.LoadAsync(fileName);

    // ASSERT
    Assert.That(result, Is.Not.Null);
}

Please notice how the second test:

  • Returns a Task instead of void

  • Is declared as an asynchronous method by the keyword async

  • Awaits the execution of the LoadAsync method

  • Is extremely easy to read while taking full advantage of the async/await functionality

PreviousAssertionsNextParameterized tests

Last updated 4 years ago