XUnit – Part 4: Parallelism and Custom Test Collections

As multi core processor and computers gain prevalence, the topic of Parallelism become more important. It allows us to use the computing resource we have available to the fullest. So by release of xUnit 2, we have the ability to run the tests in parallel. In this version, the tests are gathered into collection, and they ran in parallel. But if for some reason we don’t want to run our tests in parallel, we can do that by using Custom Test Collections. So In this post I’m going to explain how the tests run with xUnit and how we can control this aspect of test execution.

Parallelism in Testing Frameworks

xUnit run the test in parallel in default if they are in a different collections. This is different from test frameworks such as NUnit which test do not run in parallel by default.

Running Tests in Parallel With Test Collections

In xUnit framework, by default every test class is considered as a collection and these collections of test are ran in parallel. But tests within these collection/classes are ran in a non parallel way. Let me illustrate by an example.

For example the tests in this class take 8 seconds to run, because they do not run in parallel. But if we separate them into two different classes, hence two different collections like so.

The test are going to take 5 second, because now we have two different collections per class. That’s why it’s a good idea to break our tests into cohesive classes when we use xUnit framework. The parallelism also can be controlled through configuration files, you can find more detail about this here.

Running Tests Sequentially Through Custom Test Collections

Now let’s say we have two different classes for our test. But for whatever reason we do not want to run these tests in parallel. Maybe one of them are dependent on the other. In these situations we can use the Collection attribute. This allows us to put two classes, which normally create two collections, into one.

Now our tests are going to run Sequentially and not in parallel. This means it takes 8 second for these tests to run.

Summary

In this post, we saw how tests are organized and ran with using xUnit. We also saw how we can control the how these tests use our resources. Then we used Custom Test Collections to stop the framework from running our tests in a parallel way.

Share...
 

Hamid Mosalla

Hi, I'm Hamid ("Arman"). I'm a software developer with 8+ years of experience in C#, .NET Core, Software Architecture and Web Development. I enjoy creating dev tools, contributing to open-source projects, and sharing insights on my blog. Outside of tech, I’m into indie cinema, classical music and abstract art.

 

One thought on “XUnit – Part 4: Parallelism and Custom Test Collections

  1. Hi.
    I kind of want to have it the other way round.
    I want to have all classes be executed sequentially, but all tests in the class in parallel. Is this somehow possible?

Leave a Reply

Your email address will not be published. Required fields are marked *