https://github.com/workleap/wl-extensions-xunit
An opinionated library that provides base unit test and fixture classes based on the Microsoft.Extensions.* packages used by modern .NET applications.
https://github.com/workleap/wl-extensions-xunit
Last synced: about 1 year ago
JSON representation
An opinionated library that provides base unit test and fixture classes based on the Microsoft.Extensions.* packages used by modern .NET applications.
- Host: GitHub
- URL: https://github.com/workleap/wl-extensions-xunit
- Owner: workleap
- License: apache-2.0
- Created: 2022-06-07T16:46:54.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-03-31T16:18:24.000Z (about 1 year ago)
- Last Synced: 2025-03-31T17:29:51.472Z (about 1 year ago)
- Language: C#
- Homepage:
- Size: 118 KB
- Stars: 5
- Watchers: 31
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
# Workleap.Extensions.Xunit
An opinionated library that provides base unit test and fixture classes based on the `Microsoft.Extensions.*` packages used by modern .NET applications.
[](https://www.nuget.org/packages/Workleap.Extensions.Xunit/)
[](https://github.com/gsoft-inc/wl-extensions-xunit/actions/workflows/publish.yml)
## Getting started
There are base classes for **unit** and **integration tests**. Each test method has its own [service collection](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-6.0) configured through a class fixture.
### Unit tests
Create a test class that extends `BaseUnitTest<>` and accepts a class fixture that extends `BaseUnitFixture`.
In the fixture class, you can:
* Override `ConfigureServices(services)` to add, remove or update dependencies for each test method.
* Override `ConfigureConfiguration(builder)` to makes changes to the generated shared `IConfiguration`.
* Access the generated `IConfiguration` through `this.Configuration`.
In the test class, you can:
* Access the fixture through `this.Fixture`.
* Access the .NET logger through `this.Logger` - it is connected to the Xunit's `ITestOutputHelper`.
* Access the `IServiceProvider` which has been configured by the fixture through `this.Services`.
By default, unit tests come with an xunit-connected `ILogger` and an empty `IConfiguration`.
### Integration tests
Create a test class that extends `BaseIntegrationTest<>` and accepts a class fixture that extends `BaseIntegrationFixture`.
They both inherit from their respective `BaseUnit*` class.
* `BaseIntegrationFixture` adds a default `IHostEnvironment` where its environment name is:
* `Local` by default,
* `Test` on CI environments,
* overrideable by defining a `DOTNET_ENVIRONMENT` environment variable, such as in .NET modern applications.
* `BaseIntegrationFixture` adds `appsettings.json` and `appsettings.{environment}.json` optional configuration providers and also an environment variable configuration provider.
### Example
```csharp
public class MyUnitTests : BaseUnitTest
{
public MyUnitTests(MyUnitFixture fixture, ITestOutputHelper testOutputHelper)
: base(fixture, testOutputHelper)
{
}
[Fact]
public void Some_Test_Works()
{
var myClass = this.Services.GetRequiredService();
myClass.DoWork();
}
}
public class MyUnitFixture : BaseUnitFixture
{
protected override IConfigurationBuilder ConfigureConfiguration(IConfigurationBuilder builder)
{
// Executed once per fixture instance
return base.ConfigureConfiguration(builder).AddInMemoryCollection(new Dictionary
{
["My:Config:Variable"] = "foo",
});
// In an integration fixture, you could add concrete configuration providers, such as:
// builder.AddAzureKeyVault(...);
}
public override IServiceCollection ConfigureServices(IServiceCollection services)
{
// Executed for each test method
return base.ConfigureServices(services)
.AddTransient()
.AddTransient(new MyFakeDependency());
}
}
```
## Contribute
Please see [CONTRIBUTING](https://github.com/gsoft-inc/wl-extensions-xunit/blob/main/CONTRIBUTING.md)
## License
Copyright © 2022, Workleap. This code is licensed under the Apache License, Version 2.0. You may obtain a copy of this license at https://github.com/gsoft-inc/gsoft-license/blob/master/LICENSE.