Assertions & Attributes
This page covers CustomAssert and the environment-aware test attributes.
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.
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.
| Component | Role |
|---|---|
CustomAssert | Extra xUnit assertions for null/empty checks on collections and strings |
UnitFactAttribute, UnitTheoryAttribute, FunctionalFactAttribute, FunctionalTheoryAttribute | Test 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() |
FakeScheduler | Simulates a delayed command/query dispatch through a CommandQueryMediator |
WebApiTest<T> | Base class for functional web API tests using an in-memory host |
TestException | Exception raised by the utilities when a test-support operation fails |
CustomAssert and the environment-aware test attributes.FakeRepository<T, TKey>, AsyncFakeRepository<T, TKey> and FakeScheduler.WebApiTest<T> base class.CustomAssert.NullOrEmpty(collection); // passes for null or empty
CustomAssert.NotNullOrEmpty(collection); // passes only for a non-empty collection
CustomAssert.NullOrWhiteSpace(text);
CustomAssert.NotNullOrWhiteSpace(text);
// Runs everywhere except Production.
[UnitFact([EnvironmentType.Production])]
public void Calculates_totals() { /* ... */ }
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;
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);
}
}
This project is licensed under the MIT License. A copy of the license is available at LICENSE in the repository.
This page covers CustomAssert and the environment-aware test attributes.
This page covers the in-memory test doubles: FakeRepository<T, TKey>, AsyncFakeRepository<T, TKey> and FakeScheduler.
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…
Notable changes in each release.
Building from source, running the tests, branching and releasing.