Workleap.Extensions.Xunit 1.1.1-preview.3

Prefix Reserved
This is a prerelease version of Workleap.Extensions.Xunit.
dotnet add package Workleap.Extensions.Xunit --version 1.1.1-preview.3                
NuGet\Install-Package Workleap.Extensions.Xunit -Version 1.1.1-preview.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Workleap.Extensions.Xunit" Version="1.1.1-preview.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Workleap.Extensions.Xunit --version 1.1.1-preview.3                
#r "nuget: Workleap.Extensions.Xunit, 1.1.1-preview.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Workleap.Extensions.Xunit as a Cake Addin
#addin nuget:?package=Workleap.Extensions.Xunit&version=1.1.1-preview.3&prerelease

// Install Workleap.Extensions.Xunit as a Cake Tool
#tool nuget:?package=Workleap.Extensions.Xunit&version=1.1.1-preview.3&prerelease                

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.

nuget build

Getting started

There are base classes for unit and integration tests. Each test method has its own service collection 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

public class MyUnitTests : BaseUnitTest<MyUnitFixture>
{
    public MyUnitTests(MyUnitFixture fixture, ITestOutputHelper testOutputHelper)
        : base(fixture, testOutputHelper)
    {
    }

    [Fact]
    public void Some_Test_Works()
    {
        var myClass = this.Services.GetRequiredService<MyClass>();
        myClass.DoWork();
    }
}

public class MyUnitFixture : BaseUnitFixture
{
    protected override IConfigurationBuilder ConfigureConfiguration(IConfigurationBuilder builder)
    {
        // Executed once per fixture instance
        return base.ConfigureConfiguration(builder).AddInMemoryCollection(new Dictionary<string, string>
        {
            ["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<MyClass>()
            .AddTransient<IDependency>(new MyFakeDependency());
    }
}

Contribute

Please see CONTRIBUTING

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.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1-preview.3 41 12/18/2024
1.1.1-preview.2 45 11/29/2024
1.1.1-preview.1 39 11/16/2024
1.1.0 1,306 11/5/2024
1.0.3-preview.48 37 11/5/2024
1.0.3-preview.47 42 10/31/2024
1.0.3-preview.46 44 10/30/2024
1.0.3-preview.45 45 10/28/2024
1.0.3-preview.44 40 10/28/2024
1.0.3-preview.43 41 10/28/2024
1.0.3-preview.42 46 10/7/2024
1.0.3-preview.41 49 10/1/2024
1.0.3-preview.40 48 9/30/2024
1.0.3-preview.39 52 9/27/2024
1.0.3-preview.38 56 9/13/2024
1.0.3-preview.37 49 8/27/2024
1.0.3-preview.36 50 8/27/2024
1.0.3-preview.35 50 8/27/2024
1.0.3-preview.34 67 8/16/2024
1.0.3-preview.33 50 7/30/2024
1.0.3-preview.32 52 7/15/2024
1.0.3-preview.31 47 7/15/2024
1.0.3-preview.30 46 7/5/2024
1.0.3-preview.29 63 5/31/2024
1.0.3-preview.28 57 5/31/2024
1.0.3-preview.27 55 5/21/2024
1.0.3-preview.26 64 5/10/2024
1.0.3-preview.25 50 5/3/2024
1.0.3-preview.24 67 4/15/2024
1.0.3-preview.23 69 4/12/2024
1.0.3-preview.22 62 4/3/2024
1.0.3-preview.21 56 3/8/2024
1.0.3-preview.20 57 3/7/2024
1.0.3-preview.19 65 3/4/2024
1.0.3-preview.18 65 3/1/2024
1.0.3-preview.17 64 2/16/2024
1.0.3-preview.16 67 2/6/2024
1.0.3-preview.15 62 2/6/2024
1.0.3-preview.14 58 1/22/2024
1.0.3-preview.13 62 1/12/2024
1.0.3-preview.12 56 1/12/2024
1.0.3-preview.11 56 1/12/2024
1.0.3-preview.10 76 12/18/2023
1.0.3-preview.9 93 11/26/2023
1.0.3-preview.8 74 11/17/2023
1.0.3-preview.7 71 11/7/2023
1.0.3-preview.6 75 10/30/2023
1.0.3-preview.5 80 10/13/2023
1.0.3-preview.4 78 9/26/2023
1.0.3-preview.3 79 9/25/2023
1.0.3-preview.2 70 9/20/2023
1.0.3-preview.1 79 9/20/2023
1.0.2 29,049 7/20/2023
1.0.2-preview.14 87 7/20/2023