Subject attribute in Machine.Specifications, how to categorize Your tests?

In today post I want to say something about tagging/categorizing tests. Which, depends on a framework. Each of one implement it in a different way.

February 14, 2017 - 2 minute read -
Tests TDD C#

Hi,

In today post I want to say something about tagging/categorizing tests. Which, depends on a framework. Each of one implement it in a different way.

I want to focus on tagging tests using Machine.Specifications (through the Subject attribute) and Mspec.Runner as a test runner. As a continous integration system I use TeamCity. So altogether I would like to answer the question whether categorization/tagging is worthy of our attention.

Tagging tests. Should we do it or not? I assume that as elsewhere, where there are two possibilities, there are also two schools. One says that tagging is bad because the whole test in the output window does not looks like a ‘poetry’, because it’s difficult to call ‘poetry’ something like this:

Definitely better in this case looks simply:

On the other hand we have the information at the beginning of test what class/functionality is tested and we have a possibility to group tests somehow. That information about what class/function test leads me to ‘school’ n2.

So how to tag correctly tests in Machine.Specifications? We should add the attribute “Subject” on the test class that looks like this:

Thanks to that in Mspec.Runner or test report (on TeamCity) we could see a beatiful information about which test was run and whether it passed. This format is as follows:

Are there any problems that tagging tests solves?

Suppose that we have situation like this. We have two classes that have different tasks (functional) in the same folder. The behavior of these classes for an empty string as an input is the same, both should return 0.

So the code for these two classes looks as follows:

The code looks okay, resharper test runner tells us that tests are green, great! We commit our changes to our version control system, we look at our CI system (in this case temcity) and we see a single test. One test? But we have two tests, Resharper shows green badges, everything should be okay! What is the problem? Mspec.Runner interprets these tests as the “same” due to the fact that they have the same “name”, although it fires them, but they are grouped in a single test. Therefore, on TeamCity the test will have a note

2 runs

How to fix it? By adding a subject attribute to the test classes!

But if there are any cons while tagging tests? Well, yes, when you make a mistake mspec runner will not improve for us tagged test and the result for the above mentioned error will be the same.

In summary, I think the option of tagging testing is very helpful and allows you to add another level in the case of grouping tests, especially when the number goes in the number of hundreds/thousands. In addition it allows to more easily locate the possible source of “problems” of red tests.

Thank you for reading :)