Mittwoch, 6. Mai 2015

NEdifis: How we organize our test classes

For the past decade I read a lot of articles about unit testing. There are thousands of best practices, do's, don'ts and especially workarounds. I remember a "trick" of a friend of mine who tried to find a way testing internal classes methods in a separate test project. His idea: he made these methods protected and created a derived class to wrap these methods in another internal method to access these methods from a test class. Yes, it is as complicated as it sounds.

In our project we started to add our unit tests to the same project and kept them together. This gave us the ability to keep things internal which should stay internal. After some iterations we ended up with two conventions:

  • Each test class ends with "_Should" so we can read tests easily
  • The test stays along with the class in the same namespace (folder)

The second point avoids the annoying duplication and maintenance of a folder structure or a huge namespace "Tests" with dozens of tests. In addition, you can see at a glance which classes don't have a test.

For this cross-relation, NEdifis provides two attributes for this relation.
[TestedBy(typeof(TicketAttribute_Should))]
public class TicketAttribute : Attribute
{
}

[TestFixtureFor(typeof(TicketAttribute))]
// ReSharper disable once InconsistentNaming
public class TicketAttribute_Should
{
}
This makes e.g. a navigation between these classes easier using "go to definition" and gives the opportunity for convention tests to check e.g. if each class has a test or a test fixture is properly named.

Keine Kommentare:

Kommentar veröffentlichen