XUnitAssured.Kafka 6.35.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package XUnitAssured.Kafka --version 6.35.0
                    
NuGet\Install-Package XUnitAssured.Kafka -Version 6.35.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XUnitAssured.Kafka" Version="6.35.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XUnitAssured.Kafka" Version="6.35.0" />
                    
Directory.Packages.props
<PackageReference Include="XUnitAssured.Kafka" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add XUnitAssured.Kafka --version 6.35.0
                    
#r "nuget: XUnitAssured.Kafka, 6.35.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package XUnitAssured.Kafka@6.35.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=XUnitAssured.Kafka&version=6.35.0
                    
Install as a Cake Addin
#tool nuget:?package=XUnitAssured.Kafka&version=6.35.0
                    
Install as a Cake Tool

XUnitAssured.Kafka

Apache Kafka integration testing extensions for the XUnitAssured.Net framework. Write expressive Kafka tests using a fluent Given().When().Then() DSL with full support for produce/consume operations, batch messaging, authentication (SASL, SSL, mTLS), and Schema Registry.

Installation

dotnet add package XUnitAssured.Kafka

Quick Start

using XUnitAssured.Kafka.Extensions;
using XUnitAssured.Kafka.Testing;

public class OrderTests : KafkaTestBase<KafkaClassFixture>, IClassFixture<KafkaClassFixture>
{
    public OrderTests(KafkaClassFixture fixture) : base(fixture) { }

    [Fact]
    public async Task Produce_And_Consume_Order()
    {
        var topic = GenerateUniqueTopic("orders");
        var groupId = $"test-{Guid.NewGuid():N}";

        // Produce
        await Given()
            .Topic(topic)
            .Produce(new { id = 1, product = "Laptop", price = 999.99m })
            .ExecuteAsync()
            .AssertSuccess();

        // Consume
        await Given()
            .Topic(topic)
            .Consume()
            .WithGroupId(groupId)
            .ExecuteAsync()
            .AssertSuccess()
            .AssertMessage<string>(msg => msg.ShouldNotBeNullOrEmpty());
    }
}

The assertions chain right after ExecuteAsync(), and the chain is awaited once, at the start. Await it: without await the step still runs, but a failed assertion is never seen. .When() and .Then() still read the same, and are optional. The synchronous Execute() keeps working; it holds the thread until the broker answers.

The methods with an out parameter, such as Extract(out var result), cannot be asynchronous: use them on the builder, after the await — (await ...ExecuteAsync()).Extract(out var result) — or end the chain with GetResult(), which returns the result.

Fluent DSL Reference

Produce

Given()
    .Topic("my-topic")
    .Produce("Hello, Kafka!")                          // Produce string message
    .Produce(new { id = 1, name = "Test" })            // Produce JSON object
    .WithKey("my-key")                                 // Set message key
    .WithHeader("correlation-id", "abc-123")           // Add message header
    .WithBootstrapServers("localhost:9092")             // Override bootstrap servers
    .FromTimestamp(testStart)                          // Only messages written from this moment on
    .WhereKey("order-42")                              // Only a message with this key

Produce Batch

Given()
    .Topic("my-topic")
    .ProduceBatch(new[] { "msg1", "msg2", "msg3" })    // Produce multiple messages

Consume

Given()
    .Topic("my-topic")
    .Consume()                                         // Consume single message
    .WithGroupId("my-group")                           // Set consumer group
    .WithTimeout(30)                                   // Set timeout in seconds
    .WithBootstrapServers("localhost:9092")             // Override bootstrap servers

Consume Batch

Given()
    .Topic("my-topic")
    .ConsumeBatch(5)                                   // Consume up to 5 messages
    .WithGroupId("my-group")

Assertions

await Given().Topic("my-topic").Consume().ExecuteAsync()
    .AssertSuccess()                                   // Assert operation succeeded
    .AssertMessage<string>(msg => msg.ShouldBe("expected"))  // Assert message content
    .AssertBatchCount(5)                               // Assert batch message count

Authentication

SASL/PLAIN

await Given()
    .Topic("my-topic")
    .Produce("message")
    .WithBootstrapServers("localhost:29093")
    .WithAuth(auth => auth.UseSaslPlain("user", "password", useSsl: false))
    .ExecuteAsync()
    .AssertSuccess();

SASL/SCRAM-SHA-256

await Given()
    .Topic("my-topic")
    .Produce("message")
    .WithBootstrapServers("localhost:29094")
    .WithAuth(auth => auth.UseSaslScram256("user", "password"))
    .ExecuteAsync()
    .AssertSuccess();

SASL/SCRAM-SHA-512

await Given()
    .Topic("my-topic")
    .Produce("message")
    .WithBootstrapServers("localhost:29095")
    .WithAuth(auth => auth.UseSaslScram512("user", "password"))
    .ExecuteAsync()
    .AssertSuccess();

SSL (One-Way)

await Given()
    .Topic("my-topic")
    .Produce("message")
    .WithBootstrapServers("localhost:29096")
    .WithAuth(auth => auth.UseSsl("certs/ca-cert.pem"))
    .ExecuteAsync()
    .AssertSuccess();

Mutual TLS (mTLS)

await Given()
    .Topic("my-topic")
    .Produce("message")
    .WithBootstrapServers("localhost:29097")
    .WithAuth(auth => auth.UseMutualTls("client-cert.pem", "client-key.pem", "ca-cert.pem"))
    .ExecuteAsync()
    .AssertSuccess();

Automatic Authentication via testsettings.json

{
  "kafka": {
    "bootstrapServers": "localhost:9092",
    "authentication": {
      "type": "SaslPlain",
      "sasl": {
        "username": "user",
        "password": "password"
      }
    }
  }
}

Batch Operations

// Produce batch
var messages = new[] { "order-1", "order-2", "order-3", "order-4", "order-5" };

await Given()
    .Topic("orders")
    .ProduceBatch(messages)
    .ExecuteAsync()
    .AssertSuccess()
    .AssertBatchCount(5);

// Consume batch
await Given()
    .Topic("orders")
    .ConsumeBatch(5)
    .WithGroupId("batch-consumer")
    .ExecuteAsync()
    .AssertSuccess()
    .AssertBatchCount(5);

Message Keys and Headers

// Produce with key
await Given()
    .Topic("user-events")
    .Produce(new { action = "login", userId = 42 })
    .WithKey("user-42")
    .ExecuteAsync()
    .AssertSuccess();

// Produce with headers
await Given()
    .Topic("user-events")
    .Produce("event-data")
    .WithHeader("correlation-id", "abc-123")
    .WithHeader("source", "test-suite")
    .ExecuteAsync()
    .AssertSuccess();

Round-Trip Example (Produce → Consume)

[Fact]
public async Task RoundTrip_ProduceAndConsume()
{
    var topic = GenerateUniqueTopic("roundtrip");
    var groupId = $"test-{Guid.NewGuid():N}";

    // Produce
    await Given()
        .Topic(topic)
        .Produce("Hello, Kafka!")
        .ExecuteAsync()
        .AssertSuccess();

    // Consume and validate
    await Given()
        .Topic(topic)
        .Consume()
        .WithGroupId(groupId)
        .ExecuteAsync()
        .AssertSuccess()
        .AssertMessage<string>(msg => msg.ShouldBe("Hello, Kafka!"));
}

Consuming on a topic with history

A consume starts at the group's committed offset or at AutoOffsetReset, and returns the first message it finds. On a shared topic that already has messages, that is an old one, not the one the test caused. FromTimestamp starts every partition at the first message written at or after a moment, and WhereKey skips messages with another key until one arrives or the timeout ends:

var testStart = DateTimeOffset.UtcNow;

await Given().Topic("orders").Produce("order-42", order).ExecuteAsync();

await Given()
    .Topic("orders")
    .Consume()
    .FromTimestamp(testStart)
    .WhereKey("order-42")
    .ExecuteAsync()
    .AssertSuccess();

The timestamps are the broker's (CreateTime by default, from the producer's clock), so a moment taken on the test machine works when the producer runs there too. FromTimestamp ignores committed offsets, and both apply to Consume, not to ConsumeBatch.

One sharp edge: Validate with an untyped lambda

In a test project that references both this package and XUnitAssured.Http, a lambda with no parameter type binds to the HTTP overload, because that one accepts any result. The chain is then typed for HTTP, and the failure only shows up at the end, when the assertions are read:

// Binds to the Http overload, and types the chain for HTTP
Given(kafka).Topic("orders").Consume().Validate(r => r.Success.ShouldBeTrue())

// Names the type, and stays a Kafka chain
Given(kafka).Topic("orders").Consume().Validate((KafkaStepResult r) => r.Success.ShouldBeTrue())

ValidateMessage<T>, ValidateOffset, ValidatePartition and the rest are unaffected: no other package declares them, so there is nothing to compete with. Only the bare name Validate is shared.

The error message names both result types and suggests the typed form, so a run that hits this says what to change. Moving the protocol-agnostic overload out of the HTTP package would remove the ambiguity altogether, and that is a breaking change held for the next major.

Supported Frameworks

  • .NET 8
  • .NET 9
  • .NET 10
  • .NET 10

Dependencies

License

Apache-2.0 — see LICENSE.md

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
6.43.0 109 11/7/2026
6.42.0 94 11/6/2026
6.41.0 105 11/6/2026
6.40.0 100 11/6/2026
6.39.0 99 11/6/2026
6.38.0 102 11/5/2026
6.37.1 90 11/5/2026
6.37.0 90 11/5/2026
6.36.1 121 10/29/2026
6.36.0 139 10/18/2026
6.35.0 117 10/18/2026
6.34.0 104 10/18/2026
6.33.0 101 10/18/2026
6.32.0 111 10/17/2026
6.31.5 103 10/17/2026
6.31.4 105 10/17/2026
6.31.3 103 10/17/2026
6.31.2 115 10/17/2026
6.31.1 103 10/17/2026
6.31.0 99 10/16/2026
Loading failed

New package XUnitAssured.Camunda: deploy BPMN, start real Camunda 8.10 process instances on a shared cluster and mock the diagram's job workers per instance, with assertions that wait for the cluster. The other packages are unchanged from 6.19.1. Upgrade guide: https://github.com/andrewBezerra/xunit-assured-net/blob/main/UPGRADING.md