A lightweight C# library for programmatically creating test result files in TRX and JUnit formats.
- β¨ Simple API - Intuitive and easy-to-use object model
- π― Type-Safe - Strongly-typed C# classes for test results
- πͺΆ Lightweight - Minimal external dependencies
- π Multi-Target - Supports .NET 8, 9, and 10
- π¦ NuGet Ready - Easy integration via NuGet package
- π Multiple Formats - Supports both TRX and JUnit XML formats
- β Compatible - Works with Visual Studio, Azure DevOps, and CI/CD systems
Install via NuGet Package Manager:
dotnet add package DemaConsulting.TestResultsOr via Package Manager Console:
Install-Package DemaConsulting.TestResultsThe following code-snippet shows how to create test result files in both TRX and JUnit XML formats:
using DemaConsulting.TestResults;
using DemaConsulting.TestResults.IO;
// Create a TestResults instance
var results = new TestResults { Name = "SomeTests" };
// Add some results
results.Results.Add(
new TestResult
{
Name = "Test1",
ClassName = "SomeTestClass",
CodeBase = "MyTestAssembly",
Outcome = TestOutcome.Passed,
Duration = TimeSpan.FromSeconds(1.5),
StartTime = DateTime.UtcNow
});
results.Results.Add(
new TestResult
{
Name = "Test2",
ClassName = "SomeTestClass",
CodeBase = "MyTestAssembly",
Outcome = TestOutcome.Failed,
ErrorMessage = "Expected value to be 42 but was 0",
ErrorStackTrace = "at SomeTestClass.Test2() in Test.cs:line 15"
});
// Save the results to a TRX file (Visual Studio format)
File.WriteAllText("results.trx", TrxSerializer.Serialize(results));
// Save the results to a JUnit XML file
File.WriteAllText("results.xml", JUnitSerializer.Serialize(results));The library can automatically detect the format of test result files:
using DemaConsulting.TestResults.IO;
// Automatically detect and deserialize any supported format
var testResultsXml = File.ReadAllText("test-results.xml");
var results = Serializer.Deserialize(testResultsXml);
// Or identify the format without deserializing
var format = Serializer.Identify(testResultsXml);
if (format == TestResultFormat.Trx)
{
Console.WriteLine("This is a TRX file");
}
else if (format == TestResultFormat.JUnit)
{
Console.WriteLine("This is a JUnit XML file");
}The library supports reading and converting between TRX and JUnit formats:
using DemaConsulting.TestResults.IO;
// Automatic format detection and conversion
var testResultsXml = File.ReadAllText("test-results.xml");
var results = Serializer.Deserialize(testResultsXml); // Works with TRX or JUnit
var trxXml = TrxSerializer.Serialize(results);
File.WriteAllText("converted.trx", trxXml);
// Or use specific deserializers if format is known
var junitXml = File.ReadAllText("junit-results.xml");
var results2 = JUnitSerializer.Deserialize(junitXml);
var trxXml2 = TrxSerializer.Serialize(results2);
File.WriteAllText("converted-from-junit.trx", trxXml2);var result = new TestResult
{
Name = "TestWithOutput",
ClassName = "MyTests",
CodeBase = "MyAssembly",
Outcome = TestOutcome.Passed,
SystemOutput = "Debug information\nTest completed successfully"
};var failedResult = new TestResult
{
Name = "FailingTest",
ClassName = "MyTests",
CodeBase = "MyAssembly",
Outcome = TestOutcome.Failed,
ErrorMessage = "Assertion failed: Expected 100, got 50",
ErrorStackTrace = "at MyTests.FailingTest() in Tests.cs:line 42",
SystemError = "Additional error details"
};The library supports the following test outcomes:
Successful Outcomes:
Passed- Test passed successfullyPassedButRunAborted- Test passed but the run was abortedWarning- Test passed with warningsCompleted- Test completed successfully
Failure Outcomes:
Failed- Test failedError- Test encountered an errorTimeout- Test exceeded timeout limitAborted- Test was aborted
Skipped/Not Run Outcomes:
NotExecuted- Test was not executedNotRunnable- Test is not runnablePending- Test is pending execution
Other Outcomes:
Inconclusive- Test result was inconclusiveDisconnected- Test was disconnectedInProgress- Test is currently in progress
The TestOutcome enum also provides helper extension methods:
IsPassed()- Returns true for passed outcomes (Passed, PassedButRunAborted, Warning)IsFailed()- Returns true for failed outcomes (Failed, Error, Timeout, Aborted)IsExecuted()- Returns true if the test was executed
This library is useful when you need to:
- Generate TRX or JUnit XML files from custom test runners
- Convert test results between formats (TRX β JUnit)
- Create test reports programmatically
- Aggregate test results from multiple sources
- Build custom testing tools that integrate with Visual Studio, Azure DevOps, or CI/CD systems
- Architecture - Learn about the library's architecture and design
- Contributing - Guidelines for contributing to the project
- Code of Conduct - Our code of conduct for contributors
# Clone the repository
git clone https://github.com/demaconsulting/TestResults.git
cd TestResults
# Restore tools
dotnet tool restore
# Restore dependencies
dotnet restore
# Build
dotnet build
# Run tests
dotnet testFor convenience, the repository includes helper scripts to streamline development:
Windows:
# Build and test the project
build.bat
# Run code formatting, spelling, and markdown checks
lint.batLinux/macOS:
# Build and test the project
./build.sh
# Run code formatting, spelling, and markdown checks
./lint.shVisual Studio Code:
If you're using VS Code, preconfigured tasks are available via Ctrl+Shift+B (Windows/Linux) or Cmd+Shift+B (macOS):
build- Build the solution (default build task)test- Run all tests (default test task)clean- Clean build artifactsrestore- Restore NuGet packageslint- Check code formattingformat- Auto-format code
- .NET 8.0, 9.0, or 10.0
We welcome contributions! Please see our Contributing Guide for details.
This project is licensed under the MIT License - see the LICENSE file for details.
- π Report a Bug
- π‘ Request a Feature
- π¬ Ask a Question
Developed and maintained by DEMA Consulting.