xUnit uses the same AppDomain
for the whole test assembly, this is problematic as I'm testing a UI library and need to create a new Application
instance for each individual test.
It works when I run a single test, but when I Run All
the first test passed, but all subsequent tests fail with Cannot create more than one System.Windows.Application instance in the same AppDomain
at the line where I create a new Application
object.
xUnit.net creates a new instance of the test class for every test that is run, so any code which is placed into the constructor of the test class will be run for every single test.
Set to true to run the test assemblies in parallel against one other; set to false to run them sequentially. The default value is false . Set to true to run the test collections in parallel against one other; set to false to run them sequentially. The default value is true .
AppDomains are created by the . Net runtime when a managed application is initialised. When you start ABC. EXE, it gets an application domain.
Testing a positive case This method is decorated with the Fact attribute, which tells xUnit that this is a test.
Perhaps you could try by making your test class
like this:
public class DomainIsolatedTests : IDisposable
{
private static int index = 0;
private readonly AppDomain testDomain;
public DomainIsolatedTests()
{
var name= string.Concat("TestDomain #", ++index);
testDomain = AppDomain.CreateDomain(name, AppDomain.CurrentDomain.Evidence, AppDomain.CurrentDomain.SetupInformation);
// Trace.WriteLine(string.Format("[{0}] Created.", testDomain.FriendlyName));
}
public void Dispose()
{
if (testDomain != null)
{
// Trace.WriteLine(string.Format("[{0}] Unloading.", testDomain.FriendlyName));
AppDomain.Unload(testDomain);
}
}
[Fact]
public void Test1()
{
testDomain.DoCallBack(() => {
var app = new System.Windows.Application();
...
// assert
});
}
[Fact]
public void Test2()
{
testDomain.DoCallBack(() => {
var app = new System.Windows.Application();
...
// assert
});
}
[Fact]
public void Test3()
{
testDomain.DoCallBack(() => {
var app = new System.Windows.Application();
...
// assert
});
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With