Skip to content

TDD Anti-patterns: Local Hero

This series is inspired by a Stack Overflow thread.

You’re creating unit tests in your local environment, everything looks nice and all the tests are green. But, after you check-in the code, in effect, the tests aren’t working elsewhere, only in your machine. They aren’t working in your team members machines and the continuous integration is failing.

What could have gone wrong?

This is a typical symptom of the local hero anti-pattern. It’s usually cause by Hardcoded paths, locally installed libraries and difference in the environment configuration.

Local Hero Code Sample

To illustrate the local hero anti-pattern, take a look at the following unit test:

[Fact]
public void AnalyzeFile_ReturnFileDataAnalyzed()
{
    //Arrange
    var fileLocation = "C:\User\Matheus\My Documents\file.txt";
    var sut = new FileAnalyzer();

    //Act
    var result = sut.AnalyzerTxtFile(fileLocation);

    //Assert
    //...
}

As you can see, in this unit test we’re trying to analyze a text file but, the file is stored on my machine. This unit test will break in other people machine and on the server, because they don’t have the file at that location.

A solution to this problem is to store the file inside the solution and add it to the source control. As a result, they won’t fail in other environments anymore.

Wrap Up

To conclude, look for possible implementations that are linked to your local environment when writing unit tests.

Published inAnti-patternUnit Test

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: