IsolatedTests 0.0.6-alpha
dotnet add package IsolatedTests --version 0.0.6-alpha
NuGet\Install-Package IsolatedTests -Version 0.0.6-alpha
<PackageReference Include="IsolatedTests" Version="0.0.6-alpha" />
<PackageVersion Include="IsolatedTests" Version="0.0.6-alpha" />
<PackageReference Include="IsolatedTests" />
paket add IsolatedTests --version 0.0.6-alpha
#r "nuget: IsolatedTests, 0.0.6-alpha"
#:package IsolatedTests@0.0.6-alpha
#addin nuget:?package=IsolatedTests&version=0.0.6-alpha&prerelease
#tool nuget:?package=IsolatedTests&version=0.0.6-alpha&prerelease
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
- Make sure your test project targets
.NET 6+
. - Make sure to disable Optimization or specifically JIT inlining so that test methods are not potentially inlined.
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:
UnsafeCLR
is used to dynamically edit the implementation of your test methods at runtime. This allowsIsolatedTests
to intercept calls to test methods.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.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
- .NET 6 (ARM64 is not supported)
- .NET 7
- .NET 8
- .NET 9
- .NET 10
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 | Versions 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. |
-
net10.0
- NLog (>= 5.4.0)
- NLog.Extensions.Logging (>= 5.4.0)
- UnsafeCLR (>= 0.0.4-alpha)
-
net6.0
- NLog (>= 5.4.0)
- NLog.Extensions.Logging (>= 5.4.0)
- UnsafeCLR (>= 0.0.4-alpha)
-
net7.0
- NLog (>= 5.4.0)
- NLog.Extensions.Logging (>= 5.4.0)
- UnsafeCLR (>= 0.0.4-alpha)
-
net8.0
- NLog (>= 5.4.0)
- NLog.Extensions.Logging (>= 5.4.0)
- UnsafeCLR (>= 0.0.4-alpha)
-
net9.0
- NLog (>= 5.4.0)
- NLog.Extensions.Logging (>= 5.4.0)
- UnsafeCLR (>= 0.0.4-alpha)
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 |