ErikForwerk.TestAbstractions.STA.v3 1.0.5

dotnet add package ErikForwerk.TestAbstractions.STA.v3 --version 1.0.5
                    
NuGet\Install-Package ErikForwerk.TestAbstractions.STA.v3 -Version 1.0.5
                    
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="ErikForwerk.TestAbstractions.STA.v3" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ErikForwerk.TestAbstractions.STA.v3" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="ErikForwerk.TestAbstractions.STA.v3" />
                    
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 ErikForwerk.TestAbstractions.STA.v3 --version 1.0.5
                    
#r "nuget: ErikForwerk.TestAbstractions.STA.v3, 1.0.5"
                    
#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 ErikForwerk.TestAbstractions.STA.v3@1.0.5
                    
#: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=ErikForwerk.TestAbstractions.STA.v3&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=ErikForwerk.TestAbstractions.STA.v3&version=1.0.5
                    
Install as a Cake Tool

Test Abstractions

Basic test-class implementations and abstractions that simplify the most common test cases for xUnit testing.

Current Status

Codecov Test Coverage WakaTime Tracking

Current Repository Status NuGet NuGet STA

Current Repository Status NuGet NuGet STA

Features

This library provides a collection of utilities and abstractions to simplify unit testing with xUnit:

  • TestBase Class: Base class with common test utilities and helper methods
  • Test Loggers: Implementation of ILogger and ILogger<T> for testing logging scenarios
  • Property Comparison: Deep property comparison utilities using reflection
  • Auto Properties: Automatic property generation with random values for testing
  • File Cleanup: Automatic test file cleanup on test completion
  • String Extensions: Test-specific string manipulation utilities
  • STA Test Support: Support for Single-Threaded Apartment mode tests (Windows only)
  • Fail Test Helpers: Convenient methods to fail tests with custom messages

Installation

NuGet Package

For standard testing:

dotnet add package ErikForwerk.TestAbstractions

For Windows STA (Single-Threaded Apartment) testing:

dotnet add package ErikForwerk.TestAbstractions.STA

Or via NuGet Package Manager:

Install-Package ErikForwerk.TestAbstractions
Install-Package ErikForwerk.TestAbstractions.STA

Usage

TestBase Class

Inherit from TestBase to access common test utilities:

using ErikForwerk.TestAbstractions.Models;
using Xunit;
using Xunit.Abstractions;

public class MyTests : TestBase
{
    public MyTests(ITestOutputHelper output) : base(output)
    {
    }

    [Fact]
    public void TestExample()
    {
        // Use TestConsole for output
        TestConsole.WriteLine("Test output");
        
        // Use B() method for bracketed string representation
        var result = B("value"); // Returns "[value]"
        var nullValue = B(null); // Returns "<null>"
    }
}

Test Loggers

Use TestLogger to capture and verify log messages:

using ErikForwerk.TestAbstractions.Models;
using Microsoft.Extensions.Logging;

public class MyServiceTests : TestBase
{
    public MyServiceTests(ITestOutputHelper output) : base(output)
    {
    }

    [Fact]
    public void TestLogging()
    {
        var logger = GetTestLogger();
        var service = new MyService(logger);
        
        service.DoSomething();
        
        // Verify logged messages
        Assert.Contains("Expected message", logger.LogMessages);
    }
}

Property Comparison

Use CompareHelper to deeply compare object properties:

using ErikForwerk.TestAbstractions.Tools;

[Fact]
public void TestObjectComparison()
{
    var expected = new MyClass { Property1 = "value1", Property2 = 42 };
    var actual = new MyClass { Property1 = "value1", Property2 = 42 };
    
    // Assert all properties are equal
    CompareHelper.AssertEqual(expected, actual, TestConsole);
    
    // Or assert all properties are different
    var different = new MyClass { Property1 = "other", Property2 = 99 };
    CompareHelper.AssertCompletelyUnequal(expected, different, TestConsole);
}

Auto Properties

Generate random property values for testing:

using ErikForwerk.TestAbstractions.Tools;

[Fact]
public void TestWithRandomData()
{
    var autoProps = new AutoProperties();
    
    // Generate random GUID
    var guid = autoProps.GenerateGuid();
    
    // Generate random string
    var randomString = autoProps.GenerateString();
    
    // Get random enum value
    var randomStatus = autoProps.GetRandomEnum<MyStatus>();
}

File Cleanup

Automatically clean up test files after test execution:

[Fact]
public void TestWithFileCleanup()
{
    var testFile = Path.GetTempFileName();
    
    using (CreateTestFileCleanUp(testFile))
    {
        // Work with the file
        File.WriteAllText(testFile, "test data");
        
        // File will be automatically deleted when disposed
    }
}

STA Test Support

For Windows applications requiring Single-Threaded Apartment mode:

using ErikForwerk.TestAbstractions.STA.Models;

public class MyStaTests : StaTestBase
{
    public MyStaTests(ITestOutputHelper output) : base(output)
    {
    }

    [Fact]
    public void TestInStaMode()
    {
        // Test code that requires STA thread
    }
}

Requirements

  • .NET 9.0 or higher
  • xUnit 2.9.3 or higher
  • Microsoft.Extensions.Logging.Abstractions 9.0.10 or higher

For STA package:

  • Windows operating system
  • .NET 9.0-windows or higher

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Product Compatible and additional computed target framework versions.
.NET net9.0-windows7.0 is compatible.  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.

Version Downloads Last Updated
1.0.5 85 8/7/2026