Xunit - disable parallelism in few tests of full set
Asked Answered
T

2

17

I have about 100 selenium tests to run, but 2 of them cannot be run in parallel

Is it possible to disable parallelism only for those 2 tests, using xUnit?

(those 2 test cannot be parallel, because they need to simulate keyboard clicking -> so I would lose input focus using parallel execution)

Best scenario that I am looking for:

Add some attribute to 2 tests that will disable parallelism for them. Then in tests executions 98 tests will be running with 16 threads, and two remaining tests will be executed at the end using 1 thread.

I know that one of the solutions can be something like this:

  • add 'Parallel' and 'NonParallel' categories for tests
  • with xunit.console run only 'Parallel' category with parameter maxthread=16.
  • after that run 'NonParallel' category with parameter maxthread=1
  • And after all of that merge xunit reports into one.

But is not fitting my needs, and I wonder if I can run tests in a scenario like I describe in "best scenario"

P.S. If the is no solution for that, can I find something like that in nunit?

Trivet answered 7/2, 2018 at 7:48 Comment(1)
If you don't find what you are looking for in xUnit, NUnit allows you to specify tests to run in parallel or not in parallel at any level of the test hierarchy from the assembly down to the test methods including classes and namespaces. See github.com/nunit/docs/wiki/Parallelizable-AttributeUsury
G
17

While Diver answer is correct it doesn't show how exactly to achieve this.

  1. Create new special class to establish test collection, something like this:
[CollectionDefinition(nameof(SystemTestCollectionDefinition), DisableParallelization = true)]
public class SystemTestCollectionDefinition { }
  1. Now you can assign the same Collection name to all tests needed to disable parallelization. In my case I just added an attribute on the class which is base for all system tests:
[Collection(nameof(SystemTestCollectionDefinition))]
public class BaseSystemTest { ... }

Now all tests within Collection will be executed in sequence.

Source: https://github.com/xunit/xunit/issues/1999

Gupton answered 3/2, 2021 at 21:27 Comment(0)
K
7

If you have xunit >= 2.3 try [CollectionDefinition(DisableParallelization = true)].

It ability to disable cross-collection parallelization for individual test collections, via the test collection definition. Parallel-capable test collections will be run first (in parallel), followed by parallel-disabled test collections (run sequentially).

Kevakevan answered 7/2, 2018 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.