Is it possible to run each test isolated in a separate process? #1796
-
One feature that has been often requested in xUnit or NUnit (I am too lazy to look up/provide the relevant GH issues now) and is somewhat required when you work with an unmanaged component that is hard to isolate and you want to run tests in parallel, is the ability to execute each test in a separate process. Separate threads or AppDomains don't cut it, because a) AppDomains are no longer a thing in recent versions of .NET and b) the unmanaged part lives outside of the AppDomain anyway. Having this feature would be a strong argument for moving to TUnit.
Thanks for your answers in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
It'd be slow spawning new processes - But my first thought would be you could create a custom executor which executes your test project again with a specific filter for that one test. public class Tests
{
[TestExecutor<SeparateProcessExecutor>]
[Test]
public async Task Test()
{
// ...
}
}
public class SeparateProcessExecutor : ITestExecutor
{
public static bool IsSeparateProcess => Environment.GetEnvironmentVariable("IS_SEPARATE_PROCESS") == "true";
public Task ExecuteTest(TestContext context, Func<Task> action)
{
if (IsSeparateProcess)
{
return action();
}
var testDetails = context.TestDetails;
var classInformation = testDetails.TestClass;
return Process.Start(new ProcessStartInfo
{
FileName = "dotnet",
ArgumentList = { "run", "--treenode-filter", $"/{classInformation.Assembly.Name}/{classInformation.Namespace}/{classInformation.Name}/{testDetails.TestMethod.Name}" },
WorkingDirectory = GetProjectDirectory(),
Environment = { ["IS_SEPARATE_PROCESS"] = "true" }
})!.WaitForExitAsync();
}
} Edit:
I don't think so (not easily at least). I've never delved into how debuggers attach to things. You'd need to attach a debugger to the newly spawned process, but I don't know how you'd do that! |
Beta Was this translation helpful? Give feedback.
It'd be slow spawning new processes - But my first thought would be you could create a custom executor which executes your test project again with a specific filter for that one test.