Test Lint, your free "Resharper for unit testing"

May 23, 2010 19:56

Unit tests are not so different from other parts of the code.

You write a test once but expect people to read it many times. And you know what? There are only few reasons for revisiting a test:

  • it started to fail
  • it needs a refactor
  • you want to learn the system under test.

Readability is the key in all cases.

And - golly! - I came across a free tool which helps writing readable and maintainable tests. Test Lint is the name. This post is a quick sum-up of a quick evaluation I did the other day.

Here's Send HTML 
emails with ASP.NET MVC
a small VS2010 project (only VS2010 is supported) which shows some warnings and displays how to write a custom rule.


How it works.

Test Lint works like Resharper. It analyzes your code on the fly, letting you instantly see problems and inefficiences of your unit tests.

Interestingly, its warnings have links to online explanations (the idea employed in one R# plugin) yet I feel those explanations would have been more powerful if they were community content.

Code sample.

Imagine we have a barman and need to test that he can pour any kind of beer.

A "quick" way would be to iterate over all kinds in a test and verify that they all pass ok. However, logic in tests is not good, so Test Link yields a warning:

Fortunately, with NUnit it's easily to create a parametrized test that makes Test Lint happy:

Creating your own rules.

Neat thing is that Test Lint is extensible and you can create your own validation rules (the process is described here).

An even nicer thing is that Typemock guys made it extremely easy to unit tests those rules. It's so encouraging so I even created a small rule taken from TDD antipatterns list:

Sometimes a unit test may catch all exceptions and swallow the stack trace, replacing it with a less informative failure message (or even just logging and letting the test pass).


You can download the project sample here, and below is just the code for the rule. Once you have it, Test Lint will scream on the tests that swallow exceptions:

public class Reviewer : BaseTestReviewer
    public override object VisitCatchClause(CatchClause catchClause, object data)
        if (IsInsideTestMethod) {
            var exception = catchClause.TypeReference.Type;
            if (exception == typeof(Exception).Name) {
                var info = ExpressionHelpers.MakeReviewFromExpression(
                                    ruleUrl: "http://blog.james-carr.org/2006/11/03/tdd-anti-patterns/",
                                    text: "You shouldn't swallow exceptions in tests",
                                    title: "Greedy catch found.");
        return base.VisitCatchClause(catchClause, data);

(One thing to note, I probably should have used the full type name (with namespace), but  Test Lint supports only the short name.)


I know guys from Typemock are reading this, so my "general advice" would be, look at Resharper and do even better. Marker bar, ability to tweak warning levels, solution-wide analysis, quick fixes etc would be super cool.

Also it would be neat if Test Lint could identify my testing (and my mocking) framework and give more specific advices. If it can find out that I use NUnit, its suggestions probably could be more helpful then.

In general, the idea looks really inspiring, and while there are some bits and pieces to clean up, I'm definitely recommending Test Lint to anyone who is looking for a tool that facilitates unit testing.

On second thought... would it be difficult to write a R# plugin with similar functonality? :)


Comments are closed