xunit-instrumentation-otel-template

Instrument XUnit tests with OpenTelemetry and export results to Aspire Dashboard.

MIT License

Stars
1
Committers
2

XUnit.Otel.Template

Template with OpenTelemetry test instrumentation for XUnit.

Installation

dotnet new install XUnit.Otel.Template

Usage

Find template:

 dotnet new list xunit-otel
# These templates matched your input: 'xunit-otel'

# Template Name  Short Name  Language  Tags
# -------------  ----------  --------  -------------------------
# XUnit Otel     xunit-otel  [C#]      XUnit/Tests/OpenTelemetry

Verify output:

 dotnet new xunit-otel -o $dev/XUnitOtelExample01 -n XUnitOtelExample --dry-run
# File actions would have been taken:
#   Create: $dev\XUnitOtelExample01\BaseFixture.cs
#   Create: $dev\XUnitOtelExample01\Directory.Packages.props
#   Create: $dev\XUnitOtelExample01\ExampleTests.cs
#   Create: $dev\XUnitOtelExample01\Extensions.cs
#   Create: $dev\XUnitOtelExample01\MonitoringFramework\BaseTraceTestAttribute.cs
#   Create: $dev\XUnitOtelExample01\MonitoringFramework\TestRunSpanProcessor.cs
#   Create: $dev\XUnitOtelExample01\MonitoringFramework\TracePerTestAttribute.cs
#   Create: $dev\XUnitOtelExample01\MonitoringFramework\TracePerTestRunAttribute.cs
#   Create: $dev\XUnitOtelExample01\README.md
#   Create: $dev\XUnitOtelExample01\XUnitOtelExample.csproj
#   Create: $dev\XUnitOtelExample01\xunit.runner.json

Install:

 dotnet new xunit-otel -o $dev/XUnitOtelExample01 -n XUnitOtelExample

Run tests:

 cd $dev/XUnitOtelExample01
 dotnet test
# Restore complete (0.4s)
# You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
#   XUnitOtelExample succeeded (0.3s) → bin\Debug\net8.0\XUnitOtelExample.dll
#   XUnitOtelExample test failed with errors (3.3s)
#     C:\Program Files\dotnet\sdk\9.0.100-preview.2.24157.14\Microsoft.TestPlatform.targets(46,5): error : [xUnit.net 00:00:01.77]     XUnitOtelExample.ExampleTests.WaitRandomTime_Fail [FAIL] [$devXUnitOtelExample01\XUnitOtelExample.csproj]
#     $devXUnitOtelExample01\ExampleTests.cs(51): error VSTEST1: XUnitOtelExample.ExampleTests.<>c.<WaitRandomTime_Fail>b__3_0() Assert.True() Failure [$devXUnitOtelExample01\XUnitOtelExample.csproj]
# $devXUnitOtelExample01\ExampleTests.cs(51): error VSTEST1: Expected: True [$devXUnitOtelExample01\XUnitOtelExample.csproj]
# $devXUnitOtelExample01\ExampleTests.cs(51): error VSTEST1: Actual:   False [$devXUnitOtelExample01\XUnitOtelExample.csproj]

# Build failed with errors in 4.4s
# Test run failed. Total: 3 Failed: 1 Passed: 2 Skipped: 0, Duration: 3.3s

See traces:

Navigate to http://localhost:18888/traces.

Results

Initial trace with warmup trace included:

See the details of test run:

Now let's change one of the tests so we can see how failed tests are displayed:

And the details of the test run:

💡 As you can see, the exception is recorded as Trace Event.

Also, we can see the metrics for overall test runs. These metrics shows of execution time per-test and per-class based on tags.

Badges
Extracted from project README
Build CodeQL NuGet Conventional Commits License
Related Projects