Skip to content

BenjaminMichaelis/TrxLib

Repository files navigation

TrxLib

TrxLib is a .NET library for parsing and analyzing TRX (Test Results XML) files generated by MSTest, VSTest, and other test runners in the .NET ecosystem. It provides a clean, easy-to-use API for extracting test results, metadata, and diagnostics information from TRX files.

Features

  • Parse TRX files from Visual Studio, Azure DevOps, and other test runners
  • Access detailed test results including pass/fail status, durations, and error messages
  • Extract test metadata like namespaces, class names, and method names
  • Process test run metadata including timing information, deployment settings, and run configuration
  • Group test results by outcome (passed, failed, not executed, etc.)

Installation

TrxLib is available as a NuGet package: https://www.nuget.org/packages/TrxLib

Package Manager Console

Install-Package TrxLib

.NET CLI

dotnet add package TrxLib

Quick Start

// Parse a TRX file
TestResultSet results = TrxParser.Parse(new FileInfo("path/to/results.trx"));

// Get summary information
Console.WriteLine($"Total tests: {results.Count}");
Console.WriteLine($"Passed: {results.Passed.Count}");
Console.WriteLine($"Failed: {results.Failed.Count}");
Console.WriteLine($"Not executed: {results.NotExecuted.Count}");

// Get test timing information
Console.WriteLine($"Test run created: {results.CreatedTime}");
Console.WriteLine($"Test run started: {results.StartedTime}");
Console.WriteLine($"Test run completed: {results.CompletedTime}");

// Access individual test results
foreach (TestResult test in results.Failed)
{
    Console.WriteLine($"Failed test: {test.FullyQualifiedTestName}");
    Console.WriteLine($"Error message: {test.ErrorMessage}");
    Console.WriteLine($"Stack trace: {test.StackTrace}");
    Console.WriteLine($"Duration: {test.Duration}");
}

// Access test run metadata
string runId = results.OriginalTestRun?.Id;
string deploymentRoot = results.OriginalTestRun?.TestSettings?.Deployment?.RunDeploymentRoot;

Working with Test Results

Each TestResult object contains detailed information about a single test case:

TestResult test = results.FirstOrDefault(r => r.Outcome == TestOutcome.Failed);

// Basic test information
string testName = test.TestName;                     // The method name of the test
string className = test.ClassName;                   // The class containing the test
string namespace = test.Namespace;                   // The namespace of the test class
string fullName = test.FullyQualifiedTestName;       // The fully qualified test name

// Test outcome and timing
TestOutcome outcome = test.Outcome;                  // Passed, Failed, NotExecuted, etc.
TimeSpan? duration = test.Duration;                  // How long the test took to run
DateTimeOffset? startTime = test.StartTime;          // When the test started
DateTimeOffset? endTime = test.EndTime;              // When the test finished

// For failed tests
string errorMessage = test.ErrorMessage;             // The error message
string stackTrace = test.StackTrace;                 // The stack trace

// Test source information
FileInfo codebase = test.Codebase;                   // Path to the test assembly
DirectoryInfo projectDir = test.TestProjectDirectory; // Directory of the test project

Working with Test Result Sets

The TestResultSet class provides aggregated views of test results:

// Group tests by outcome
IReadOnlyCollection<TestResult> passedTests = results.Passed;
IReadOnlyCollection<TestResult> failedTests = results.Failed;
IReadOnlyCollection<TestResult> notExecutedTests = results.NotExecuted;
IReadOnlyCollection<TestResult> inconclusiveTests = results.Inconclusive;
IReadOnlyCollection<TestResult> timeoutTests = results.Timeout;
IReadOnlyCollection<TestResult> pendingTests = results.Pending;

// Filter and query results (using LINQ)
var longRunningTests = results.Where(r => r.Duration > TimeSpan.FromSeconds(1));
var testsByNamespace = results.GroupBy(r => r.Namespace);
var testsByClassName = results.GroupBy(r => r.ClassName);

// Test run metadata
string testRunName = results.TestRunName;
string testFilePath = results.TestFilePath;

License

MIT License

Credits

Inspired by t-rex.

About

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages