Documentation

ArturRios.Util.Test is a small .NET library of test-support utilities for xUnit projects. It bundles the helpers that come up again and again when testing…

ArturRios.Util.Test is a small .NET library of test-support utilities for xUnit projects. It bundles the helpers that come up again and again when testing services and web APIs: extra assertions, environment-aware test attributes, in-memory fakes for repositories and schedulers, and a base class for functional web API tests.

Installation

dotnet add package ArturRios.Util.Test

It is built on xUnit v3, so the test project references xunit.v3, or xunit.v3.mtp-off to keep running through VSTest. Versions before 5.0 are built on xUnit v2.

The package targets net10.0 and builds on other ArturRios.* packages (ArturRios.Util, ArturRios.Data.Relational.Core, ArturRios.Mediator, ArturRios.Configuration and ArturRios.Util.WebApi) plus xUnit v3’s xunit.v3.extensibility.core and xunit.v3.assert, Microsoft.AspNetCore.Mvc.Testing and Microsoft.EntityFrameworkCore.

What’s inside

ComponentRole
CustomAssertExtra xUnit assertions for null/empty checks on collections and strings
UnitFactAttribute, UnitTheoryAttribute, FunctionalFactAttribute, FunctionalTheoryAttributeTest attributes that can skip tests per environment or on a condition
FakeRepository<T, TKey>In-memory IRepository<T, TKey> implementation, for any entity key type
AsyncFakeRepository<T, TKey>In-memory IAsyncRepository<T, TKey> implementation with cancellation support and async-capable Query()
FakeSchedulerSimulates a delayed command/query dispatch through a CommandQueryMediator
WebApiTest<T>Base class for functional web API tests using an in-memory host
TestExceptionException raised by the utilities when a test-support operation fails

Guides

Quick Start

Custom assertions

CustomAssert.NullOrEmpty(collection);      // passes for null or empty
CustomAssert.NotNullOrEmpty(collection);   // passes only for a non-empty collection
CustomAssert.NullOrWhiteSpace(text);
CustomAssert.NotNullOrWhiteSpace(text);

Environment-aware attributes

// Runs everywhere except Production.
[UnitFact([EnvironmentType.Production])]
public void Calculates_totals() { /* ... */ }

In-memory repository

var repository = new FakeRepository<Person, long>();   // Person : Entity<long>

var id = repository.Create(new Person { Name = "Ann" }).Data;   // long and int ids start at 1
var person = repository.GetById(id).Data;

Functional web API test

public class ProductsApiTests : WebApiTest<Program>
{
    public ProductsApiTests() : base(EnvironmentType.Local) { }

    [Fact]
    public async Task Creates_a_product()
    {
        await AuthenticateAndAuthorizeAsync(new Credentials("user@test.com", "secret123"), "/auth");

        var response = await Gateway.PostAsync<DataOutput<ProductOutput>>("/products", new { Name = "Widget" });

        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
    }
}

Upgrading

This project is licensed under the MIT License. A copy of the license is available at LICENSE in the repository.


Assertions & Attributes

This page covers CustomAssert and the environment-aware test attributes.

Fakes

This page covers the in-memory test doubles: FakeRepository<T, TKey>, AsyncFakeRepository<T, TKey> and FakeScheduler.

Web API Testing

WebApiTest<T> is a base class for functional web API tests. It hosts your API in memory with WebApplicationFactory<T> and exposes an HttpGateway plus…

Changelog

Notable changes in each release.

Contributing

Building from source, running the tests, branching and releasing.