TestFixture attribute – just because R# doesn’t care, doesn’t mean you shouldn’t

Recently I noticed ReSharper’s built-in test runner doesn’t require you to decorate your fixtures as TestFixture — it’s smart enough to locate Test methods regardless. My team noticed this too, and as a result we’ve started to omit TestFixture entirely.

Turns out this is a big mistake. NUnit itself requires them — in our case, we discovered our build server (which calls NUnit directly) was ignoring a large number of tests (including some that were failing), and was reporting much lower code coverage results than expected.

So, somehow we need to enforce that the TestFixture attribute is applied everywhere it should be, so us lazy ReSharper developers don’t miss anything. Not to worry… we can write a test for that!

public class MissingTestFixtureAttributeTests
    public void All_test_fixtures_must_have_test_fixture_attribute()
        IEnumerable<Type> badFixtures =
            from t in Assembly.GetExecutingAssembly().GetTypes()
            where HasTests(t)
            where IsMissingTestFixtureAttribute(t)
            select t;


    static bool HasTests(Type type)
        var testMethods =
            from m in type.GetMethods()
            from a in m.GetCustomAttributes(typeof (TestAttribute), true)
            select m;

        return testMethods.Any();

    static bool IsMissingTestFixtureAttribute(Type type)
        var attributes =
            from a in type.GetCustomAttributes(typeof (TestFixtureAttribute), true)
            select a;

        return !attributes.Any();

    private static void ErrorIfAny(IEnumerable<Type> fixtures)
        if (!fixtures.Any())

        var sb = new StringBuilder("The following types are missing [TestFixture] attributes:");
        foreach(Type type in fixtures)

        throw new Exception(sb.ToString());

2 thoughts on “TestFixture attribute – just because R# doesn’t care, doesn’t mean you shouldn’t

Comments are closed.