Can Fixture.InitializeAsync Code execute parallelly with another test collection? #2929
-
We have a suite of Integration tests that requires a huge set of data to be setup in the DB. We are employing XUnit Shared context using Fixtures and have collection fixtures applied for different test collections. Fixture /context setup takes around 20s. The data required for my test context is setup using
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Given this .csproj: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>12.0</LangVersion>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="xunit" Version="2.4.2" />
</ItemGroup>
</Project> And this xunit.runner.json: {
"$schema": "https://xunit.net/schema/v2.8.1/xunit.runner.schema.json",
"diagnosticMessages": true,
"parallelizeTestCollections": false
} And this .cs: using System;
using Xunit;
namespace Empty;
public sealed class Fixture1 : IDisposable
{
public Fixture1() => Console.WriteLine("Fixture1.ctor");
public void Dispose() => Console.WriteLine("Fixture1.Dispose");
}
[CollectionDefinition(nameof(Collection1))]
public class Collection1 : ICollectionFixture<Fixture1> { }
[Collection(nameof(Collection1))]
public class TestClass1a
{
[Fact]
public void TestMethod() => Console.WriteLine("TestClass1a.TestMethod");
}
[Collection(nameof(Collection1))]
public class TestClass1b
{
[Fact]
public void TestMethod() => Console.WriteLine("TestClass1b.TestMethod");
}
public sealed class Fixture2 : IDisposable
{
public Fixture2() => Console.WriteLine("Fixture2.ctor");
public void Dispose() => Console.WriteLine("Fixture2.Dispose");
}
[CollectionDefinition(nameof(Collection2))]
public class Collection2 : ICollectionFixture<Fixture2> { }
[Collection(nameof(Collection2))]
public class TestClass2a
{
[Fact]
public void TestMethod() => Console.WriteLine("TestClass2a.TestMethod");
}
[Collection(nameof(Collection2))]
public class TestClass2b
{
[Fact]
public void TestMethod() => Console.WriteLine("TestClass2b.TestMethod");
} When running the tests, this is the output I see: The fixture for the first collection is created once, then all the tests in the first collection are run, then the fixture is cleaned up. Then the fixture for the second collection is created once, then all the tests in the second collection are run, then the fixture is cleaned up. Unless I've misunderstood the question, this is exactly the behavior you want, right? Can you provide a repro project? |
Beta Was this translation helpful? Give feedback.
-
Thank you for the answer, and the full repro of my question, in my case, the second collection fixture was throwing an error (Some data duplication, which I don't find a reason to occur unless for parallelism). I have managed to solve my blocker by ignoring duplicates, But I will be looking deeper to my issue if this is indeed arising from a parallelism. But thanks for this pointers, I will add some more observability into the Init and Dispose so that I can make myself absolutely certain of the behavior. |
Beta Was this translation helpful? Give feedback.
Sorry, I just noticed you mentioned
InitializeAsync
. The sample changes, but the behavior is still the same: