Don’t comment out your tests, there are better alternatives!

You’ve either done it yourself or you’ve seen others done it. Uncommented a whole test, a test fixture or maybe an entire file.

I know, it’s so easy to press Ctrl+A, Ctrl+K+C to uncomment everything. You’re probably changing an interface. Most likely, the methods that are tested don’t exist anymore in the form in which the tests calls them. You probably just click and drag the mouse and comment out a block. But please, be careful. I’ve seen a lot of examples where an entire file ended up commented out for months. Only to turn out to be crucial, because it tested a very important scenario.

First and foremost, if you are refactoring, please look at the tests as if it’s production code. You are best off getting the tests to compile, even if that takes extra time and you’d rather pull the main code back on its feet before you worry about the tests. Be aware that the “it takes extra time…” is a fallacy. It just takes time. It’s part of your job. It’s not less important than the rest of the code. Probably the contrary. You’re better off keeping the tests compiling all the time.

But if you must, please, don’t comment out a whole test or whole fixture. If you must, then do it like this, instead:

    public void SomeMethod_SomeCondition_ShouldDoSomething()
        // Arrange
        // SystemUnderTest sut = new SystemUnderTest();
        // sut.SetupSomeData();

        // Act
        // sut.DoesNotCompile();

        // Assert
        // Assert.IsTrue(sut.SomePredicateDoesNotCompile());
        Assert.Fail("TODO: Fix this test");

That way, at least your test suite will alert you that there are a couple of tests you still need to fix. And you won’t have a colleague pointing out to you that that commit you did two months ago effectively deleted some really important tests…

0 Replies to “Don’t comment out your tests, there are better alternatives!”