-
Notifications
You must be signed in to change notification settings - Fork 759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attribute [CollectionDefinition(DisableParallelization = true)] doesn't prevent parallel execution between class #1999
Comments
You haven't properly defined the collection. The docs discuss how to do it: https://xunit.net/docs/shared-context#collection-fixture My recommendation is that your tests look like this: using System.Threading;
using Xunit;
namespace XUnitTestProject
{
[Collection(nameof(NotThreadSafeResourceCollection))]
public class UnitTest1
{
[Fact]
public void Test1()
{
NotThreadSafeResource.Use();
Thread.Sleep(1000);
NotThreadSafeResource.Release();
}
[Fact]
public void Test3()
{
NotThreadSafeResource.Use();
Thread.Sleep(1000);
NotThreadSafeResource.Release();
}
}
} using System.Threading;
using Xunit;
namespace XUnitTestProject
{
[Collection(nameof(NotThreadSafeResourceCollection))]
public class UnitTest2
{
[Fact]
public void Test2()
{
NotThreadSafeResource.Use();
Thread.Sleep(1000);
NotThreadSafeResource.Release();
}
}
} And you should have an extra class, which is the collection definition class: using Xunit;
namespace XUnitTestProject
{
[CollectionDefinition(nameof(NotThreadSafeResourceCollection), DisableParallelization = true)]
public class NotThreadSafeResourceCollection { }
} |
Thanks you for your help! This fix my problem. |
Is it possible to run them in order? Without disabling the parallel execution for the whole framework? I have found this: In order to run classes in order, I need to create AssemblyInfo.cs and add following lines in AssemblyInfo.cs using Xunit;
//Optional
[assembly: CollectionBehavior(DisableTestParallelization = true)]
//Optional
[assembly: TestCaseOrderer("Xunit.Extensions.Ordering.TestCaseOrderer", "Xunit.Extensions.Ordering")]
//Optional
[assembly: TestCollectionOrderer("Xunit.Extensions.Ordering.CollectionOrderer", "Xunit.Extensions.Ordering")] This causes to run whole framework with only one thread. I would like to accomplish to put classes in order in selected Collection Collection[("sequentialRun"),Order(1)]
Class A{
[Fact]
void test1(){}
[Fact]
void test2(){}
}
Collection[("sequentialRun"),Order(2)]
Class B{
[Fact]
void test1(){}
[Fact]
void test2(){}
}
Class C{
[Fact]
void test1(){}
[Fact]
void test2(){}
} When I trigger the whole test script in my framework I would like to achieve that A and C starts running together and when A is done then B can start. Is there anyway to accomplish this?? Thank you so much!! |
I can't speak to specifically how that extension works, since I didn't write it and haven't ever used it. What I can speak to is how parallelization works in general: https://xunit.net/docs/running-tests-in-parallel This line is disabling parallelization: [assembly: CollectionBehavior(DisableTestParallelization = true)] Ordering should be orthogonal to parallelization. |
I have a set of tests in some test classes that use a non-thread-safe resource. When I run all the tests in my solution, some of these tests fail because XUnit tests run in parallel when the tests are in a separate class.
I have tried this attribute on the class I want to disable parallelization and it does not matter.
[CollectionDefinition("EndpointTests", DisableParallelization = true)]
I'm using the XUnit and XUnit.runner.visualstudio at version 2.4.1.
I created a simple project that reproduce my problem when you run all tests in the solution.
XUnitTestProject.zip
The text was updated successfully, but these errors were encountered: