Nullean.Xunit.Partitions 0.5.0

dotnet add package Nullean.Xunit.Partitions --version 0.5.0
NuGet\Install-Package Nullean.Xunit.Partitions -Version 0.5.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="Nullean.Xunit.Partitions" Version="0.5.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Nullean.Xunit.Partitions --version 0.5.0
#r "nuget: Nullean.Xunit.Partitions, 0.5.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.
// Install Nullean.Xunit.Partitions as a Cake Addin
#addin nuget:?package=Nullean.Xunit.Partitions&version=0.5.0

// Install Nullean.Xunit.Partitions as a Cake Tool
#tool nuget:?package=Nullean.Xunit.Partitions&version=0.5.0

Nullean.Xunit.Partitions

<a href="https://www.nuget.org/packages/Nullean.Xunit.Partitions/"><img src="https://img.shields.io/nuget/v/Nullean.Xunit.Partitions?color=blue&style=plastic" /></a>

<img src="https://github.com/nullean/xunit-partitions/raw/main/nuget-icon.png" align="right" title="Logo " width="220" height="220">

An XunitTestFramework implementation that introduces the concept of "partitions".

A IPartitionFixture{TLifetime} allows tests to inject a long lived object to share.

Only a single partition will run at a time in contrast with xUnit's ICollectionFixture{TFixture} However unlike ICollectionFixture{TFixture} this library's IPartitionFixture{TLifetime} does not mark a concurrency barrier, tests belonging to a single partition still run concurrently

In fact each IPartitionFixture{TLifetime} can declare its own desired concurrency through IPartitionLifetime.MaxConcurrency

If you want to share a few (say 0-20) long running objects over 1000's of tests this library will work for you. If you instead have many test collections each with only a few tests xUnit native collections will suit better.

Because each partititon only calls InitializeAsync and DisposeAsync just before and after its test will run this makes it more appropiate then assembly fixtures which might bootstrap too early and dispose too late.

Setup

Provide the following Assembly level attribute anywhere in your test project.

using Nullean.Xunit.Partitions;
using Xunit;
[assembly: TestFramework(Partition.TestFramework, Partition.Assembly)]

This will ensure xUnit bootstraps the partition test framework shipped with this library.

Providing options

Options to control the parition test framework can be provided similarly through the PartitonOptions Assembly level attribute.

Here you can control filters to only run certain partitions and/or tests.

Setup.cs
using Nullean.Xunit.Partitions;
using My.Tests;
using Xunit;

[assembly: TestFramework(Partition.TestFramework, Partition.Assembly)]
//optional only needed if you want to specify execution options to PartitionTestFramework
[assembly: PartitionOptions(typeof(MyPartitioningOptions))]

namespace My.Tests;

/// <summary> Allows us to control the xunit partitioning test pipeline </summary>
public class MyPartitioningOptions : PartitionOptions
{
	public MyPartitioningOptions()
	{
		PartitionFilterRegex = "LongLivedObject";
		TestFilterRegex = null;
	}
}

Usage

namespace Nullean.Xunit.Partitions.Tests;


public class NoStateClass
{
	[Fact]
	public void SimpleTest() => 1.Should().Be(1);
}

public class LongLivedObject : IPartitionLifetime
{
	private static long _initialized;
	private static long _disposed;

	public long Initialized => _initialized;
	public long Disposed => _disposed;

	public Task InitializeAsync()
	{
		Interlocked.Increment(ref _initialized);
		return Task.CompletedTask;
	}

	public Task DisposeAsync()
	{
		Interlocked.Increment(ref _disposed);
		return Task.CompletedTask;
	}

	public int? MaxConcurrency => null;
}

public class SharedState1Class(LongLivedObject longLivedObject) : IPartitionFixture<LongLivedObject>
{
	[Fact]
	public void StaticInitializedShouldNotIncrease() => longLivedObject.Initialized.Should().Be(1);

	[Fact]
	public void DisposeShouldNotHaveHappened() => longLivedObject.Disposed.Should().Be(0);
}

public class SharedState2Class(LongLivedObject longLivedObject) : IPartitionFixture<LongLivedObject>
{
	[Fact]
	public void StaticInitializedShouldNotIncrease() => longLivedObject.Initialized.Should().Be(1);

	[Fact]
	public void DisposeShouldNotHaveHappened() => longLivedObject.Disposed.Should().Be(0);
}

SharedState1Class and SharedState2Class both depend on LongLivedObject and so will receive a single shared instance AFTER InitializeAsync has run. The tests of each will run concurrently.

DisposeAsync will run before the next partition's state will InitializeAsync

NoStateClass does not belong to any partition. All tests with no partitions are treated as part of an empty partition. These tests will all run concurrently too.

However no partition will ever run concurrently with another by design.

This allows partition's state to run expensive operations (e.g starting docker containers, processes, bootstrap playwright states), in isolation.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Nullean.Xunit.Partitions:

Package Downloads
Elastic.Elasticsearch.Xunit The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Provides an Xunit test framework allowing you to run integration tests against local ephemeral Elasticsearch clusters

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.5.0 728 2/22/2024
0.4.3 236 2/13/2024
0.4.2 112 2/9/2024
0.4.1 84 2/9/2024
0.4.0 98 2/9/2024
0.3.0 106 2/9/2024