thomhurst / TUnit

A modern, fast and flexible .NET testing framework
MIT License
2.42k stars 38 forks source link

Grouped Tests #1149

Closed gshukov98 closed 3 weeks ago

gshukov98 commented 3 weeks ago

Can I achieve the following scenario with the current version of TUnit or if I can't it will be good to be added.

I have ten unit tests. Five are related to the same feature, and the other five are related to other features. By default, TUnit runs all tests in parallel, but I have an issue when I run all tests in parallel because I have static configurations that are different for the different features. So I want to achieve a parallel execution of the test by group.

Summary: I want to run 5 of my tests in parallel and when they finish I want to run the other group in parallel. Currently, I achieved this behavior with [NotInParallel] on all tests that are from the second group. However, this is not optimal because all tests from group two are parallel and slow.

thomhurst commented 3 weeks ago

Yep achievable by splitting into two classes and using the DependsOnAttribute.

public class Group1
{
    ...
}

[DependsOn<Group1>]
public class Group2
{
    ...
}
gshukov98 commented 3 weeks ago

Ok, but this with the ten tests was just an example. I have a lot more tests that are in different classes and with this approach, it will be hell to configure it.

thomhurst commented 3 weeks ago

Handling specific concurrency of lots of tests is always going to be difficult surely? How would you expect to configure it?

gshukov98 commented 3 weeks ago

I am thinking about something like this:

[Group(Name = "Group1")]
public class Group1Tests
{
    public void Test1()
    {
        Console.WriteLine("Test1");
    }

    public void Test2()
    {
        Console.WriteLine("Test1");
    }

    public void Test3()
    {
        Console.WriteLine("Test1");
    }
}

[Group(Name = "Group2")]
public class Group2Tests
{
    public void Test1()
    {
        Console.WriteLine("Test1");
    }

    public void Test2()
    {
        Console.WriteLine("Test1");
    }

    public void Test3()
    {
        Console.WriteLine("Test1");
    }
}

[Group(Name = "Group1")]
public class Group3Tests
{
    public void Test1()
    {
        Console.WriteLine("Test1");
    }

    public void Test2()
    {
        Console.WriteLine("Test1");
    }

    public void Test3()
    {
        Console.WriteLine("Test1");
    }
}

I think that this feature will be very useful because if you have a lot of tests you will be able to group them and run them in parallel in groups which will give you a lot of flexibility.

We are testing TUnit in Cronus.DomainModeling If you look in the tests you will see that we have static configuration for URN Test example and because the configuration for 'Urn.UseCaseSensitiveUrns' is static and global we have issue when we run the tests in parallel because this setting affects the whole Test project.

thomhurst commented 3 weeks ago

Okay, yeah this sounds like it'd be useful.

I'd go with the naming [ParallelGroup] so it's clearer what the group does.

Leave this with me and I'll try and get it added.

gshukov98 commented 3 weeks ago

Thank you!

thomhurst commented 3 weeks ago

@gshukov98 give v0.2.191 a go and let me know how it is.

gshukov98 commented 3 weeks ago

Tomorrow I will test it and I will give you feedback.

gshukov98 commented 2 weeks ago

I tested it and it solved my problem.

Thank you for the fast response!