A .NET console application that parses xUnit XML test results and persists them to a SQL Server database with full metadata tracking and transactional integrity.
Note: The software quality operations script should be used to implement this repository in your solution.
TestResultStorageTool provides automated storage and tracking of xUnit test results, enabling historical analysis, trend monitoring, and CI/CD pipeline integration. The tool parses xUnit XML output files and stores comprehensive test data including assemblies, collections, individual test cases, failures, skip reasons, and traits.
- Comprehensive Test Data Capture: Stores test assemblies, collections, individual test cases, failures, skip reasons, and custom traits
- CI/CD Integration: Automatically detects CI environment variables (GitLab CI) or falls back to local Git metadata
- Duplicate Prevention: Detects and prevents duplicate test result insertion using pipeline ID and commit SHA
- Transactional Safety: ACID-compliant database operations with automatic rollback on errors
- Flexible Configuration: Supports user secrets (local), environment variables (CI), and command-line arguments
- Rich Metadata: Captures branch name, commit SHA, pipeline ID, build number, and timestamps
- Error Handling: Detailed SQL error diagnostics with troubleshooting guidance
- Git Integration: Automatically extracts Git commit SHA and branch name when running locally
The tool populates the following tables:
TestAssemblies- Top-level test run metadata (pipeline, branch, commit, timestamps, aggregate stats)TestCollections- xUnit test collections with aggregated statisticsTestCases- Individual test methods with results and durationTestFailures- Error messages and stack traces for failed testsTestSkipReasons- Reasons for skipped testsTestTraits- Custom xUnit traits (categories, priorities, etc.)
Minimal usage (with user secrets configured):
dotnet run --project TestResultStorageTool -- path/to/test-results.xmlThe tool will automatically:
- Read connection string from user secrets
- Extract Git commit SHA and branch from local repository
- Generate a unique local pipeline ID based on file hash
The tool is set up to automatically detect CI environment variables. Meaning it will store relevant CI environment values instead of the locally set values, when implemented in a CI/CD pipeline. See also Arguments > Fallback.
Use with quality-operations.ps1 for complete workflow automation:
# Setup database connection
.\quality-operations.ps1 -LocalServerName "MY-PC-NAME"
# Run tests and store results automatically
.\quality-operations.ps1 -TestThe script will:
- Clean, restore, and build the solution
- Run all xUnit test suites
- Generate XML result files
- Automatically invoke the storage tool for each test suite
- Handle all configuration and path management
cd TestResultStorageTool
dotnet user-secrets set "ConnectionStrings:TestResultsDb" "Server=localhost\SQLEXPRESS;Database=DatabaseForTestResults;Integrated Security=true;TrustServerCertificate=true;"# Connection string
$env:DB_CONNECTION_STRING = "Server=..."
# CI metadata (automatically set by GitLab CI)
$env:CI_COMMIT_SHA = "abc123..."
$env:CI_COMMIT_BRANCH = "main"
$env:CI_PIPELINE_ID = "12345"
$env:CI_BUILD_NUMBER = "67"| Position | Parameter | Description | Fallback |
|---|---|---|---|
| 0 | XML Path | Path to xUnit XML results file | Required |
| 1 | Connection String | Database connection string | User secrets → Env var → Error |
| 2 | Commit SHA | Git commit hash | CI_COMMIT_SHA → Local Git → Timestamp |
| 3 | Branch | Git branch name | CI_COMMIT_BRANCH → Local Git → "local" |
| 4 | Pipeline ID | Unique pipeline identifier | CI_PIPELINE_ID → File hash |
| 5 | Build Number | Build number (optional) | CI_BUILD_NUMBER → null |
The tool provides detailed console output:
=== Test Results Reporter ===
Source: TestSuite/Results/test-results.xml
Pipeline: local-abc123def456
Branch: feature/new-tests
Commit: abc123def456789
✓ XML parsed successfully
Connecting to database...
✓ Database connected
Processing test assembly...
Assembly timestamp: 2024-01-15 14:30:45
Created test assembly record (ID: 42)
Processed 127 test(s)
=== Test Summary ===
Total: 127
Passed: 125 ✓
Failed: 1 ✗
Skipped: 1 ⊘
Duration: 45.23s
✓ Test results successfully persisted to database
The tool provides specific troubleshooting guidance for common SQL errors:
- Connection failures (Error -1, 53, 258): Server availability and address verification
- Authentication failures (Error 18456): Credential and authentication mode checks
- Duplicate entries (Error 2627, 2601): Pipeline already processed detection
- General errors: Full exception details with stack trace
All database operations use transactions with automatic rollback on failure.
The storage tool is designed to work seamlessly with the quality operations script:
# Complete workflow
.\quality-operations.ps1 -Clone # Clone storage tool repository
.\quality-operations.ps1 -LocalServerName "MY-PC" # Configure database
.\quality-operations.ps1 -Test # Run tests and store resultsThe script handles:
- Locating all xUnit test projects
- Installing required test loggers
- Creating mock configuration files
- Running tests with XML output
- Invoking the storage tool for each test suite
- Managing paths and error handling
- .NET 6.0 or higher
- SQL Server (LocalDB, Express, or full edition)
- xUnit test framework
- Microsoft.Data.SqlClient
- Microsoft.Extensions.Configuration (for user secrets)
Use quality-operations.ps1 -LocalServerName to automatically:
- Create the database
- Initialize schema (tables, views, indexes)
- Configure connection string in user secrets
- Run verification queries
Or manually execute the SQL scripts in SqlScriptsForLocalDatabase/.