Skip to content

Integration NUnit

aryehcitron@gmail.com edited this page Nov 2, 2026 · 24 revisions

Example project: A complete working example is available at examples/Example.Api/tests/Example.Api.Tests.Component.NUnit4/.


Overview

This guide walks you through integrating Kronikol 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

Prerequisites

  • .NET 10.0 SDK or later
  • An ASP.NET Core API project to test (your "Service Under Test")
  • Basic familiarity with NUnit

Step 1: Create the Test Project

Create a new NUnit test project:

dotnet new nunit -n MyApi.Tests.Component

Step 2: Install NuGet Packages

dotnet add package Kronikol.NUnit4
dotnet add package Microsoft.AspNetCore.Mvc.Testing
dotnet add package Microsoft.NET.Test.Sdk
dotnet add package NUnit
dotnet add package NUnit3TestAdapter

Your <ItemGroup> should look like this:

<ItemGroup>
    <PackageReference Include="Kronikol.NUnit4" Version="5.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.7" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
    <PackageReference Include="NUnit" Version="4.6.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="6.2.0" />
    <PackageReference Include="NUnit.Analyzers" Version="4.12.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>

NUnit versions: Kronikol.NUnit4 asks for NUnit 4.6.0 or later, the first release whose internal result tree it reads as it does. Its own suite runs on 4.6.0, and the floor search passed on 4.6.1, the newest when 5.27.10 shipped.


Step 3: Add a Global Usings File

Create a GlobalUsings.cs:

global using NUnit.Framework;

Step 4: Create the Test Run Setup/Teardown

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 Kronikol;
using Kronikol.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 the StartRunTime.

Step 5: Create the Base Fixture

Create Infrastructure/BaseFixture.cs:

using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Kronikol.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
                {
                    CallerName = ServiceUnderTestName,
                    PortsToServiceNames =
                    {
                        { 80, ServiceUnderTestName },
                        { 5001, "Downstream Service A" }
                    }
                });
            });
        });
    }

    protected BaseFixture()
    {
        Client = SFactory!.CreateTestTrackingClient(
            new NUnitTestTrackingMessageHandlerOptions
            {
                FixedNameForReceivingService = ServiceUnderTestName
            });
    }

    public void Dispose() => Client.Dispose();
}

Key points:

  • DiagrammedComponentTest provides a [TearDown] method that enqueues TestContext.CurrentContext for report collection after each test.
  • Only a failed scenario carries an error message and a stack trace. The reason given to Assert.Pass, Assert.Ignore or Assert.Inconclusive is not drawn as an error (it was before 4.13.3). A test that calls Assert.Warn is reported as passed and keeps the warning's text, the only place the report has for it.
  • A test ignored by attribute, and the tests of a fixture whose constructor or [OneTimeSetUp] threw, never reach [TearDown]; since 4.14.4 the report has them too, from the run's result tree, which NUnit holds in the set-up fixture's [OneTimeTearDown] where NUnitReportGenerator writes the report. Only tests of fixtures that derive from DiagrammedComponentTest are added, and only when the report is written with the run's whole DiagrammedTestRun.TestContexts, as Step 4 and the templates do: since 4.14.11 a report written for part of the run holds only the tests passed to it. An [Explicit] test the run does not select stays out of the report (since 4.14.11; from 4.14.4 it was reported as skipped).
  • NUnitTestTrackingMessageHandlerOptions uses NUnit's TestContext.CurrentContext to resolve the current test's identity. Outside a test (a [SetUpFixture], a fixture's constructor, [OneTimeSetUp] or [OneTimeTearDown], or a thread the test's execution context did not reach) that context names the fixture or an ad hoc test of NUnit's, not a test, so the fetcher throws there (since 4.13.1) and such calls go to a TestIdentityScope, the global fallback or the background.

Step 6: Write Test Scenarios

Tests are written as regular NUnit [Test] methods. Use the [Endpoint] and [HappyPath] attributes to add metadata for the report.

Scenarios/Cake_Feature.cs:

using Kronikol.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();
    }
}

Scenarios/Cake_Feature.steps.cs:

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's PropertyAttribute.
  • [HappyPath] — Marks a scenario as a happy path (filterable in the HTML report).
  • [Category("smoke")] on the test gives the scenario the tag smoke, and on the fixture, a [SetUpFixture] above it or the assembly gives it to the feature (5.0.0; see Tags and Attributes › Tags).
  • [TestFixture] — Required on the partial class with steps that inherits from BaseFixture.
  • Class and method names are converted from underscore-separated to space-separated in reports. A method's name reads as a sentence, a word written in capitals kept (5.16.5; see Step Tracking › Method Name Humanization), and a TestName is shown as written; their stable ids are as they were.

Step 7: Run the Tests

dotnet test

After the tests complete, check the bin/Debug/net10.0/Reports/ folder:

File Description
Specifications.html HTML specifications with embedded PlantUML sequence diagrams
TestRunReport.html HTML test run report with diagrams and execution summary
Specifications.yml YAML specifications

Architecture Summary

┌─────────────────────────────────┐
│           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]                  │
└─────────────────────────────────┘

Using PlantUML Overrides

You can customise diagrams within a test using TrackingDiagramOverride:

using Kronikol.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 beginning of the setup phase (produces a visual separator in the diagram)
TrackingDiagramOverride.StartSetup();

// Explicitly mark the boundary between setup and action phases
TrackingDiagramOverride.StartAction();

Setup separation: When SeparateSetup = true is set on ReportConfigurationOptions, HTTP calls made before StartAction() are wrapped in a visual "Setup" partition in the diagram. Call StartSetup() to explicitly mark the beginning of the setup phase.


CurrentTestInfo

The Kronikol.NUnit4 package provides a CurrentTestInfo static class whose Fetcher property returns a Func<(string Name, string Id)> delegate that resolves the current test's identity. This is used by all extension CurrentTestInfoFetcher options:

using Kronikol.NUnit4;

options.CurrentTestInfoFetcher = CurrentTestInfo.Fetcher;

Customisation Options

ReportConfigurationOptions

Property Default Description
SpecificationsTitle "Service Specifications" Title shown at the top of reports
PlantUmlServerBaseUrl "https://plantuml.com/plantuml" PlantUML server URL
HtmlSpecificationsFileName "Specifications" Output filename for specs HTML
HtmlTestRunReportFileName "TestRunReport" Output filename for test run HTML
YamlSpecificationsFileName "Specifications" Output filename for YAML specs
HtmlSpecificationsCustomStyleSheet Stylesheets.VioletThemeStyleSheet 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

NUnitTestTrackingMessageHandlerOptions

Property Description
CallerName 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. A call no map names appears as its host and port (payments.example.com:443), or localhost:{port} when it goes to this machine.

Faking Downstream Dependencies (Correctly)

When your SUT calls downstream HTTP services, those calls must flow through TestTrackingMessageHandler to produce proper HTTP-style diagram arrows (with method, status code, headers, body). Do not mock service client interfaces and use MessageTracker to manually log HTTP interactions — this produces event-style arrows that are misleading.

Recommended approaches:

See Tracking Dependencies › Faking Dependencies: Getting Proper HTTP Tracking for detailed examples of each approach.


Troubleshooting

Reports folder is empty

  • Ensure TestRun.GlobalTeardown() calls NUnitReportGenerator.CreateStandardReportsWithDiagrams.
  • Ensure TestRun has no namespace (otherwise NUnit won't treat it as a global [SetUpFixture]).
  • Ensure your test classes inherit from BaseFixture (which inherits from DiagrammedComponentTest).

Tests are not showing in the report

  • Make sure each test class inherits from DiagrammedComponentTest (directly or via BaseFixture). The base class's [TearDown] enqueues the TestContext for collection.

Empty specifications HTML / YAML

If any test has failed, the specifications files will be blank by design. The TestRunReport.html will still be generated.

Home · 🔍 Search this wiki Full-text search across every page. The “Pages” list above matches page and heading names, not page content.


Demo


Getting Started

Common Tasks

Integration Guides

Uninstrumentable / polyglot backends

Extensions

Configuration

Features

Reference

Clone this wiki locally