[NotInParallel] Support nested ConstraintKeys: combine class and method constraints #5016
-
IntroductionI have a large number of tests that rely on various unmanaged/shared resources, so I need flexible control over parallelism in TUnit. Default behaviorBy default, [NotInParallel] behaves like an override. Here is an example that works, but requires explicitly specifying all keys on each test: ProblemWith many tests and shared resources, this approach becomes hard to maintain: Constraint definitions are duplicated across tests It’s easy to forget a key and accidentally introduce parallel conflicts Base classes cannot effectively define shared constraints Also, [DependsOn] is not suitable for my scenario. I don’t want to enforce sequential execution between tests — I want tests to run concurrently as long as they use different resources, and only prevent parallel execution when tests share the same resource. Desired behaviorIn my case, it would be much more convenient to combine (union) the keys instead of overriding them. Something like this: This way, I could define shared constraints once (e.g. on a base class) and only add extra ones when needed. My workaroundI found a workaround, but it feels quite hacky: QuestionIs there a more correct or idiomatic way to achieve this kind of behavior in TUnit? If not, I would appreciate any feedback or recommendations regarding my current workaround. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Out of the box - No, there isn't a clean way to do this. Your workaround is a good solution for now - But as you say, it's not ideal, and it's extra work on you having to maintain that attribute. I'd need to build something into TUnit to allow these to be mergeable. I'm thinking something like an [NotInParallel(X)]
public class ATests : BaseA
{
[Test]
public async Task DesiredTestA1() => await Action();
[Test]
public async Task DesiredTestA2() => await Action();
[Test]
[NotInParallel(Y, InheritParentKeys = true)]
public async Task DesiredTestAB() => await Action();
} |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for the quick response! I hope this feature makes it into TUnit. It would be great if something like InheritParentKeys could also be configurable as the default behavior. |
Beta Was this translation helpful? Give feedback.
Out of the box - No, there isn't a clean way to do this.
Your workaround is a good solution for now - But as you say, it's not ideal, and it's extra work on you having to maintain that attribute.
I'd need to build something into TUnit to allow these to be mergeable.
I'm thinking something like an
InheritParentKeysproperty on the attribute or something.