Details

Recommendation type: Files created

Configurable: Yes

Filter category: Unstable test environment

Description

Tests should not have side effects on the test environment. Failing to delete files created on the file system can cause subsequent test runs or other tests to fail. Created files should be removed by the time the test has completed.

Example

In the following example, the created files are removed after the test is executed. Failing to remove the files will generate this recommendation.

public class MyTest {
    private File _testFile;
    private static final String PATH = "C:/newFile";
     
    @Before
    public void setUp() throws IOException {
        // Prepare test environment
        _testFile = new File(PATH);
        _testFile.createNewFile();
    }
 
    @After
    public void tearDown() {
        // Remove test related files
        _testFile.delete();
    }
}
  • No labels