Skip to content

Part 2 of 2. Made as part of a solution developed for the final semester project. See readme.

Notifications You must be signed in to change notification settings

MagnusPC/TestResultStorageTool

Repository files navigation

Test Result Storage Tool

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.

Overview

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.

Features

  • 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

Database Schema

The tool populates the following tables:

  • TestAssemblies - Top-level test run metadata (pipeline, branch, commit, timestamps, aggregate stats)
  • TestCollections - xUnit test collections with aggregated statistics
  • TestCases - Individual test methods with results and duration
  • TestFailures - Error messages and stack traces for failed tests
  • TestSkipReasons - Reasons for skipped tests
  • TestTraits - Custom xUnit traits (categories, priorities, etc.)

Usage

Local Development

Minimal usage (with user secrets configured):

dotnet run --project TestResultStorageTool -- path/to/test-results.xml

The 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

CI/CD Environment

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.

Automated Script Integration

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 -Test

The script will:

  1. Clean, restore, and build the solution
  2. Run all xUnit test suites
  3. Generate XML result files
  4. Automatically invoke the storage tool for each test suite
  5. Handle all configuration and path management

Configuration

User Secrets (Recommended for Local Development)

cd TestResultStorageTool
dotnet user-secrets set "ConnectionStrings:TestResultsDb" "Server=localhost\SQLEXPRESS;Database=DatabaseForTestResults;Integrated Security=true;TrustServerCertificate=true;"

Environment Variables

# 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"

Arguments

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

Output

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

Error Handling

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.

Integration with quality-operations.ps1

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 results

The 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

Requirements

  • .NET 6.0 or higher
  • SQL Server (LocalDB, Express, or full edition)
  • xUnit test framework
  • Microsoft.Data.SqlClient
  • Microsoft.Extensions.Configuration (for user secrets)

Database Setup

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/.

About

Part 2 of 2. Made as part of a solution developed for the final semester project. See readme.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published