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 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
The package targets net10.0 and builds on other ArturRios.* packages
(ArturRios.Util,
ArturRios.Data,
ArturRios.Mediator,
ArturRios.Configuration and
ArturRios.Util.WebApi) plus xunit and
Microsoft.AspNetCore.Mvc.Testing.
What’s inside
| 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> | In-memory ICrudRepository<T> / IRangeRepository<T> implementation |
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 |
Guides
- Assertions & Attributes —
CustomAssertand the environment-aware test attributes. - Fakes —
FakeRepository<T>andFakeScheduler. - Web API Testing — the
WebApiTest<T>base class.
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>();
var id = repository.Create(new Person { Name = "Ann" }); // ids start at 1
var person = repository.GetById(id);
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);
}
}
Versioning
Semantic Versioning (SemVer). Breaking changes result in a new major version. New methods or non-breaking behavior changes increment the minor version; fixes or tweaks increment the patch.
Build, test and publish
Use the official .NET CLI to build, test and publish the project and Git for source control. If you want, optional helper toolsets I built to facilitate these tasks are available:
Legal Details
This project is licensed under the MIT License. A copy of the license is available at LICENSE in the repository.