详细信息

建议类型:创建的文件

是否可在首选项配置:是

过滤器分类:不稳定的测试环境

说明

如果新建的文件在测试完成时未能删除,UTA 将显示此建议。未能删除在文件系统中创建的文件可能会导致后续测试运行或其他测试失败。

(info) 如果要创建的文件在运行测试时已经存在,则不会显示此建议。

示例

在以下示例中将创建一个新文件,但不会删除。

public class MyTest {
    private File _testFile;
    private static final String PATH = "newFile";
   
    public void setUp() throws IOException {
        // Prepare test environment
        _testFile = new File(PATH);
        _testFile.createNewFile();
    }
} 

修复

执行测试后删除创建的文件。

public class MyTest {
    private File _testFile;
    private static final String PATH = "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();
    }
}