This is Part 15 of a series on Designing, Building & Packaging A Scalable, Testable .NET Open Source Component.

In our last post, we looked at how to virtualize infrastructure such as database engines (SQL Server and PostgreSQL).

In this post, we will look at how to better organize our tests.

Currently, if you look at the test runner, all the tests look like this:

AllTests

This is a mix of unit tests, integration tests and behaviour tests.

Wouldn’t it be nice if they could be categorized as such?

This is possible using the Trait attribute.

We can decorate each test class as appropriate.

For the unit tests:

[Trait("Type", "Unit")]
public class AesFileEncryptorTests

The behavior tests:

[Trait("Type", "Behaviour")]
public class UploadFileManagerTests

The integration tests:

[Trait("Type", "Integration")]
public class PostgreSQLStorageEngineTests

The test runner will now look like this (depending on your IDE of choice)

TestTraits

This makes it easier to visualize and run the tests. For example, if you want to run only the unit tests, you can right click that node and run them all collectively:

UnitTests

TLDR

Traits allow for the organization of tests to make the testing experience smoother.

The code is in my GitHub.

Happy hacking!