Baubit.xUnit 2025.23.1

Additional Details

This project is in process of being ported to a new - simpler test framework for Baubit apps.

dotnet add package Baubit.xUnit --version 2025.23.1
                    
NuGet\Install-Package Baubit.xUnit -Version 2025.23.1
                    
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="Baubit.xUnit" Version="2025.23.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Baubit.xUnit" Version="2025.23.1" />
                    
Directory.Packages.props
<PackageReference Include="Baubit.xUnit" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Baubit.xUnit --version 2025.23.1
                    
#r "nuget: Baubit.xUnit, 2025.23.1"
                    
#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.
#:package Baubit.xUnit@2025.23.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Baubit.xUnit&version=2025.23.1
                    
Install as a Cake Addin
#tool nuget:?package=Baubit.xUnit&version=2025.23.1
                    
Install as a Cake Tool

Baubit.xUnit

NuGet

Baubit.xUnit is an extension for Baubit that integrates with xUnit.net, facilitating the development of modular unit tests for .NET applications. This integration allows for structured and maintainable testing of individual components within a modular architecture.

Features

  • Modular Testing: Enables unit testing of individual modules within a Baubit-based application.
  • Seamless xUnit Integration: Leverages xUnit's Fixtures framework to inject testable components into the test classes.
  • Configuration Flexibility: Supports configuration-driven test setups - allowing scenario design using configuration files.

Getting Started

Installation

To install Baubit.xUnit, add the NuGet package to your test project:

dotnet add package Baubit.xUnit

Usage

Below is a step-by-step guide to setting up and using Baubit.xUnit in your test project.

1. Define Your Component
public class MyComponent
{
    public string SomeString { get; set; }

    public MyComponent(string someStr)
    {
        SomeString = someStr;
    }
}
2. Add it to the applications IoC container using a Baubit module.
3. Create a Test Context

A test context is a custom class that provides access to all testable components registered in one or more modules. This enables validating component behavior in isolation.

[Source(EmbeddedJsonResources = ["MyLib.Test;context.json"])]
public class Context : IContext
{
    public MyComponent MyComponent { get; set; }

    public Context(MyComponent myComponent)
    {
        MyComponent = myComponent;
    }
}

A [Source] attribute MUST be defined for the Context. This allows the Baubit.xUnit framework to load modules relevant for testing

4. Configure Embedded Resource

Make sure context.json is marked as an embedded resource in your test project. Example content:

{
  "modules": [
    {
      "type": "MyLib.MyModule, MyLib",
      "configuration": {
        "myStringProperty": "some string value"
      }
    }
  ]
}
5. Write the Unit Test
public class MyComponentTests : AClassFixture<Context>
{
    public MyComponentTests(Fixture<Context> fixture,
                            ITestOutputHelper testOutputHelper,
                            IMessageSink diagnosticMessageSink = null)
        : base(fixture, testOutputHelper, diagnosticMessageSink)
    {
    }

    [Fact]
    public void MyComponent_Should_Not_Be_Null()
    {
        Assert.NotNull(Context);
        Assert.NotNull(Context.MyComponent);
        Assert.NotNull(Context.MyComponent.SomeString);
    }
}

The test class uses Fixture<Context> to bootstrap the module and expose configured services. The Context property provides access to the test context instance, through which you can access and validate components, behaviors, and configurations.

Scenario Variance

Baubit.xUnit supports scenario variance to test multiple scenarios using the same test method

public class MyScenario : IScenario<Context>
{
    public string ScenarioSpecificData { get; }

    public Result Run(Context context)
    {
        context.MyComponent.DoSomething(ScenarioSpecificData);
        return Result.OkIf(context.MyComponent.State == States.MySpecificState, new Error("Invalid component state after doing something"));
    }

    public Result Run() => throw new NotImplementedException();

    public Task<Result> RunAsync(Context context) => throw new NotImplementedException();

    public Task<Result> RunAsync() => throw new NotImplementedException();
}
public class MyComponentTests : AClassFixture<Context>
{
    public MyComponentTests(Fixture<Context> fixture,
                            ITestOutputHelper testOutputHelper,
                            IMessageSink diagnosticMessageSink = null)
        : base(fixture, testOutputHelper, diagnosticMessageSink)
    {
    }

    [Theory]
    [InlineData("MyLib.Test;Scenarios.scenario1.json")]
    [InlineData("MyLib.Test;Scenarios.scenario2.json")]
    [InlineData("MyLib.Test;Scenarios.scenario3.json")]
    public void MyComponent_ShouldBeInMySpecificStateAfterDoingSomething()
    {
        var result = ExecuteScenario<MyScenario>(embeddedJsonResource);
        var reasons = new List<IReason>();
        result.UnwrapReasons(reasons);
        var reasonsString = string.Join(Environment.NewLine, reasons);
        Assert.True(result.IsSuccess, reasonsString);
    }
}

You assertions will just not check if the scenario was successful, but also tell you the exact reasons behind the failure (as long as your components are capturing reasons in the call stack).

Sample scenario json files

//scenario1.json
{
    "scenarioSpecificData": "<specific data 1>"
}
//scenario2.json
{
    "scenarioSpecificData": "<specific data 2>"
}
//scenario3.json
{
    "scenarioSpecificData": "<specific data 3>"
}

Resources

Contributing

Contributions are welcome! Fork the repo and submit a pull request.

License

This project is licensed under the Apache 2.0 License.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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.