Basic test-class implementations and abstractions that simplify the most common test cases for xUnit testing.
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
ILoggerandILogger<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
For standard testing:
dotnet add package ErikForwerk.TestAbstractionsFor Windows STA (Single-Threaded Apartment) testing:
dotnet add package ErikForwerk.TestAbstractions.STAOr via NuGet Package Manager:
Install-Package ErikForwerk.TestAbstractions
Install-Package ErikForwerk.TestAbstractions.STA
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>"
}
}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);
}
}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);
}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>();
}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
}
}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
}
}- .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
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.