-
Notifications
You must be signed in to change notification settings - Fork 1
Integration NUnit
Example project: A complete working example is available at
Example.Api/tests/Example.Api.Tests.Component.NUnit4/.
This guide walks you through integrating TestTrackingDiagrams with NUnit. After completing this guide, your NUnit tests will automatically generate:
- PlantUML sequence diagrams from HTTP traffic between your service and its dependencies
- HTML reports with embedded diagrams
- YAML specification files
- .NET 10.0 SDK or later
- An ASP.NET Core API project to test (your "Service Under Test")
- Basic familiarity with NUnit
Create a new NUnit test project:
dotnet new nunit -n MyApi.Tests.Componentdotnet add package TestTrackingDiagrams.NUnit4
dotnet add package Microsoft.AspNetCore.Mvc.Testing
dotnet add package Microsoft.NET.Test.Sdk
dotnet add package NUnit
dotnet add package NUnit3TestAdapterYour <ItemGroup> should look like this:
<ItemGroup>
<PackageReference Include="TestTrackingDiagrams.NUnit4" Version="1.23.9" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.12" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>Create a GlobalUsings.cs:
global using NUnit.Framework;NUnit uses a [SetUpFixture] class for global setup and teardown. This must be placed outside of any namespace so that it applies to the whole assembly.
Create a Infrastructure/TestRun.cs:
using TestTrackingDiagrams;
using TestTrackingDiagrams.NUnit4;
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
[SetUpFixture]
public class TestRun : DiagrammedTestRun
{
[OneTimeSetUp]
public static void GlobalSetup()
{
Setup();
// Optional: start any HTTP fakes here
}
[OneTimeTearDown]
public static void GlobalTeardown()
{
EndRunTime = DateTime.UtcNow;
// Generate reports when the test run ends
NUnitReportGenerator.CreateStandardReportsWithDiagrams(
TestContexts,
StartRunTime,
EndRunTime,
new ReportConfigurationOptions
{
SpecificationsTitle = "My API Specifications"
});
// Optional: dispose HTTP fakes here
}
}Critical points:
- The class must not be inside a namespace — NUnit only runs
[SetUpFixture]as a global fixture when it has no namespace. -
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]ensures each test gets a fresh fixture instance (important for accurate tracking). - Call
Setup()in[OneTimeSetUp]— this records theStartRunTime.
Create Infrastructure/BaseFixture.cs:
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using TestTrackingDiagrams.NUnit4;
namespace MyApi.Tests.Component.Infrastructure;
public abstract class BaseFixture : DiagrammedComponentTest, IDisposable
{
private static readonly WebApplicationFactory<Program>? SFactory;
protected HttpClient Client { get; }
private const string ServiceUnderTestName = "My API";
static BaseFixture()
{
SFactory = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
services.TrackDependenciesForDiagrams(new NUnitTestTrackingMessageHandlerOptions
{
CallingServiceName = ServiceUnderTestName,
PortsToServiceNames =
{
{ 80, ServiceUnderTestName },
{ 5001, "Downstream Service A" }
}
});
});
});
}
protected BaseFixture()
{
Client = SFactory!.CreateTestTrackingClient(
new NUnitTestTrackingMessageHandlerOptions
{
FixedNameForReceivingService = ServiceUnderTestName
});
}
public void Dispose() => Client.Dispose();
}Key points:
-
DiagrammedComponentTestprovides a[TearDown]method that enqueuesTestContext.CurrentContextfor report collection after each test. -
NUnitTestTrackingMessageHandlerOptionsuses NUnit'sTestContext.CurrentContextto resolve the current test's identity.
Tests are written as regular NUnit [Test] methods. Use the [Endpoint] and [HappyPath] attributes to add metadata for the report.
using TestTrackingDiagrams.NUnit4;
namespace MyApi.Tests.Component.Scenarios;
[Endpoint("/cake")]
public partial class Cake_Feature
{
[Test]
[HappyPath]
public async Task Calling_Create_Cake_Endpoint_Returns_Cake()
{
await Given_a_valid_post_request_for_the_Cake_endpoint();
await When_the_request_is_sent_to_the_cake_post_endpoint();
await Then_the_response_should_be_successful();
}
[Test]
public async Task Calling_Create_Cake_Endpoint_Without_Eggs_Returns_Bad_Request()
{
await Given_a_valid_post_request_for_the_Cake_endpoint();
await But_the_request_body_is_missing_eggs();
await When_the_request_is_sent_to_the_cake_post_endpoint();
await Then_the_response_http_status_should_be_bad_request();
}
}using System.Net;
using System.Net.Http.Json;
using MyApi.Tests.Component.Infrastructure;
namespace MyApi.Tests.Component.Scenarios;
[TestFixture]
public partial class Cake_Feature : BaseFixture
{
private HttpResponseMessage? _response;
private async Task Given_a_valid_post_request_for_the_Cake_endpoint()
{
// Build your request using Client
}
private async Task But_the_request_body_is_missing_eggs()
{
// Modify request
}
private async Task When_the_request_is_sent_to_the_cake_post_endpoint()
{
_response = await Client.PostAsJsonAsync("cake", /* request */);
}
private async Task Then_the_response_should_be_successful()
{
_response!.StatusCode.Should().Be(HttpStatusCode.OK);
}
private async Task Then_the_response_http_status_should_be_bad_request()
{
_response!.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}
}Key points:
-
[Endpoint("/cake")]— Sets the endpoint label for this feature group in the report. This attribute maps to NUnit'sPropertyAttribute. -
[HappyPath]— Marks a scenario as a happy path (filterable in the HTML report). -
[TestFixture]— Required on the partial class with steps that inherits fromBaseFixture. - Class and method names are converted from underscore-separated to space-separated in reports.
dotnet testAfter the tests complete, check the bin/Debug/net10.0/Reports/ folder:
| File | Description |
|---|---|
ComponentSpecificationsWithExamples.html |
HTML specifications with embedded PlantUML sequence diagrams |
FeaturesReport.html |
HTML test run report with diagrams and execution summary |
ComponentSpecifications.yml |
YAML specifications |
┌─────────────────────────────────┐
│ TestRun │ ← [SetUpFixture] outside any namespace
│ : DiagrammedTestRun │ Generates reports in [OneTimeTearDown]
│ [SetUpFixture] │
└─────────────────────────────────┘
│
▼
┌─────────────────────────────────┐
│ BaseFixture │ ← Creates tracked HttpClient
│ : DiagrammedComponentTest │ Enqueues TestContext on [TearDown]
│ IDisposable │
└─────────────┬───────────────────┘
│ inherited by
▼
┌─────────────────────────────────┐
│ Cake_Feature : BaseFixture │ ← Your test class with [Test] methods
│ [TestFixture] │
└─────────────────────────────────┘
You can customise diagrams within a test using TrackingDiagramOverride:
using TestTrackingDiagrams.NUnit4;
// Insert a delimiter between multiple requests in the diagram
TrackingDiagramOverride.InsertTestDelimiter("Step 1");
// Insert raw PlantUML markup
TrackingDiagramOverride.InsertPlantUml("note over MyApi : Custom note");
// Override the start/end of diagram generation
TrackingDiagramOverride.StartOverride();
TrackingDiagramOverride.EndOverride();
// Explicitly mark the boundary between setup and action phases
TrackingDiagramOverride.StartAction();Setup separation: When
SeparateSetup = trueis set onReportConfigurationOptions, HTTP calls made beforeStartAction()are wrapped in a visual "Setup" partition in the diagram.
| Property | Default | Description |
|---|---|---|
SpecificationsTitle |
"Specifications" |
Title shown at the top of reports |
PlantUmlServerBaseUrl |
"https://www.plantuml.com/plantuml" |
PlantUML server URL |
HtmlSpecificationsFileName |
"ComponentSpecificationsWithExamples" |
Output filename for specs HTML |
HtmlTestRunReportFileName |
"FeaturesReport" |
Output filename for test run HTML |
YamlSpecificationsFileName |
"ComponentSpecifications" |
Output filename for YAML specs |
HtmlSpecificationsCustomStyleSheet |
null |
Custom CSS appended to specs HTML |
ExcludedHeaders |
[] |
HTTP headers to exclude from diagrams |
SeparateSetup |
false |
When true, HTTP calls made before StartAction() are wrapped in a visual "Setup" partition in the diagram |
HighlightSetup |
true |
When true (and SeparateSetup is enabled), the setup partition is rendered with a background colour |
| Property | Description |
|---|---|
CallingServiceName |
Display name for the service making outgoing HTTP calls |
FixedNameForReceivingService |
Display name for the service receiving requests |
PortsToServiceNames |
Dictionary mapping port numbers to friendly service names. Unmapped ports appear as localhost_80, localhost_5001, etc. |
- Ensure
TestRun.GlobalTeardown()callsNUnitReportGenerator.CreateStandardReportsWithDiagrams. - Ensure
TestRunhas no namespace (otherwise NUnit won't treat it as a global[SetUpFixture]). - Ensure your test classes inherit from
BaseFixture(which inherits fromDiagrammedComponentTest).
- Make sure each test class inherits from
DiagrammedComponentTest(directly or viaBaseFixture). The base class's[TearDown]enqueues theTestContextfor collection.
If any test has failed, the specifications files will be blank by design. The FeaturesReport.html will still be generated.
Getting Started
Common Tasks
Integration Guides
- Integration xUnit3
- Integration xUnit2
- Integration NUnit
- Integration MSTest
- Integration TUnit
- Integration BDDfy xUnit3
- Integration LightBDD xUnit2
- Integration LightBDD xUnit3
- Integration LightBDD TUnit
- Integration ReqNRoll xUnit2
- Integration ReqNRoll xUnit3
- Integration ReqNRoll TUnit
Extensions
- Integration AtlasDataApi Extension
- Integration BigQuery Extension
- Integration Bigtable Extension
- Integration BlobStorage Extension
- Integration ClickHouse Extension
- Integration CloudStorage Extension
- Integration CosmosDB Extension
- Integration Dapper Extension
- Integration DynamoDB Extension
- Integration EF Core Relational Extension
- Integration Elasticsearch Extension
- Integration EventBridge Extension
- Integration EventHubs Extension
- Integration Grpc Extension
- Integration Kafka Extension
- Integration MassTransit Extension
- Integration MongoDB Extension
- Integration MySqlConnector Extension
- Integration Npgsql Extension
- Integration Oracle Extension
- Integration PubSub Extension
- Integration Redis Extension
- Integration S3 Extension
- Integration ServiceBus Extension
- Integration SNS Extension
- Integration Spanner Extension
- Integration SqlClient Extension
- Integration Sqlite Extension
- Integration SQS Extension
- Integration StorageQueues Extension
- Integration OpenTelemetry Extension
- Integration DispatchProxy Extension
- Integration MediatR Extension
- Integration PlantUML IKVM
Configuration
- Tracking Dependencies
- Tracking Custom Dependencies
- HTTP Tracking Setup
- Report Configuration
- Diagram Customisation
- Phase-Aware Tracking
- Content Formatting
- PlantUML Server Configuration
Features
- Generated Reports
- Search Syntax
- Component Diagrams
- PlantUML Browser Rendering
- Inline SVG Rendering
- Internal Flow Tracking
- Tags and Attributes
- Excluding Requests
- Excluded Headers
- Multi-Host Test Architectures
- Event-Driven Architecture Testing
- Service Bus Tracking Patterns
- Background Thread Correlation
- Parallel-Safe Background Correlation
- Event & Message Tracking
- Assertion Tracking
- Step Tracking
- Tabular Attributes
- Large Response and Diagram Handling
- Diagnostics and Debugging
- CI Summary Integration
- CI Artifact Upload
- Merging Parallel Reports
Reference