IsolatedTests 0.0.6-alpha

This is a prerelease version of IsolatedTests.
dotnet add package IsolatedTests --version 0.0.6-alpha
                    
NuGet\Install-Package IsolatedTests -Version 0.0.6-alpha
                    
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="IsolatedTests" Version="0.0.6-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="IsolatedTests" Version="0.0.6-alpha" />
                    
Directory.Packages.props
<PackageReference Include="IsolatedTests" />
                    
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 IsolatedTests --version 0.0.6-alpha
                    
#r "nuget: IsolatedTests, 0.0.6-alpha"
                    
#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 IsolatedTests@0.0.6-alpha
                    
#: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=IsolatedTests&version=0.0.6-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=IsolatedTests&version=0.0.6-alpha&prerelease
                    
Install as a Cake Tool

IsolatedTests

IsolatedTests is a .NET library that enables test class isolation in your test suites using a simple attribute-based approach. It supports .NET 6, .NET 7, .NET 8 and .NET 9.

Features

  • Isolated Test Classes – Ensure test classes are isolated from each other to avoid shared state issues.
  • Simple Initialization – Minimal setup using a [ModuleInitializer].
  • Seamless Integration – Use [IsolatedTest] to mark classes that should be isolated.

Getting Started

Installation

Add the IsolatedTests package to your test project:

dotnet add package IsolatedTests

Setup

Initialize IsolatedTests once at module startup using the [ModuleInitializer] attribute:

using System.Runtime.CompilerServices;
using IsolatedTests;

public static class TestSetup {
    [ModuleInitializer]
    internal static void ModuleInitializer() {
        if(TestIsolator.ModuleInitializer()) {
            // Do your other one time initializations here...
            // ex: dockerContainer.start();
        }
        
        // Code here will run for each assembly load triggered by [IsolatedTest]
        // ex: MyTestContext.parseFiles();
    }
}

This ensures that isolation is injected before any tests run.

Usage

To isolate a test class, simply decorate it with the [IsolatedTest] attribute:

using IsolatedTests.Attribute;

public class MySharedState {
    private static MySharedState? _instance;
    internal static MySharedState Instance => _instance ??= new MySharedState();

    internal bool IsInitialized {
        get;
        set;
    }
}

[TestClass]
[IsolatedTest]
public class MyIsolatedTest1 {
    [TestMethod]
    public void Test1() {
        lock (MySharedState.Instance) {
            Assert.IsFalse(MySharedState.Instance.IsInitialized);
            MySharedState.Instance.IsInitialized = true;
        }
    }
}

[TestClass]
[IsolatedTest]
public class MyIsolatedTest2 {
    [TestMethod]
    public async Task Test2() {
        await Task.Delay(1000)
            .ContinueWith(t => {
                lock (MySharedState.Instance) {
                    Assert.IsFalse(MySharedState.Instance.IsInitialized);
                    MySharedState.Instance.IsInitialized = true;
                }
            });
    }
}

Only test classes marked with [IsolatedTest] will be isolated. Regular test classes continue to run as usual.

How does it work?

IsolatedTests is powered by UnsafeCLR (thus the non-support for net6.0 ARM64), a runtime method manipulation library.

Here’s what happens under the hood:

  1. UnsafeCLR is used to dynamically edit the implementation of your test methods at runtime. This allows IsolatedTests to intercept calls to test methods.

  2. Once a test method is intercepted, IsolatedTests loads the test assembly (if not previously done for this class) into a completely separate, isolated assembly context.

  3. The intercepted method call is then re-routed to execute inside this new context, providing full isolation from other tests.

This approach ensures that static state, global configuration, or other test-related side effects do not bleed between test classes.

Compatibility

License

See LICENSE

Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you'd like to change.

Feedback

Found an issue or have a feature request? Open an issue.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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 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
0.0.6-alpha 108 4/29/2025
0.0.5-alpha 80 4/25/2025
0.0.4-alpha 134 4/21/2025
0.0.3-alpha 138 4/19/2025
0.0.2-alpha 140 4/18/2025
0.0.1-alpha 156 4/17/2025