如何从 ITestListener 访问 Reporter 使用的测试输出目录

How to access test-output directory that Reporter uses from ITestListener

我们正在使用 IReporter 的 generateReport() 方法来创建自定义报告。在运行的开头,如果报告文件存在,我们需要将其删除。

我们需要这样做的原因是因为我们在 ITestListener 的 onStart() 方法中使用 SkipException - 如果出现某些异常,我们需要 fail/stop 测试 运行。如果发生这种情况,则永远不会调用 generateReport(),将现有的自定义报告留在原地,通常会造成混乱。

如何从 ITestListener 访问在 generateReport() 中使用的同一个 outputDirectory?我可以获得 ITestContext 的 outputDirectory,但这会将我带到 Reporter 的 outputDirectory 的子文件夹。

public class CustomTestListener implements ITestListener {

    @Override
    public void onStart(ITestContext ctx) {

        // This returns a child directory of the actual directory we want
        // String outputDir = ctx.getOutputDirectory();

        // This is null, how would i access from ITestContext?
        // XMLReporter x = new XMLReporter();
        // String outputDir = x.getOutputDirectory();

        // method that would delete the file
        String reportFile = outputDir + "/" + GlobalConstants.REPORT_FILE;
        deleteFile (reportFile);
    }
}

我找到了一个解决方案,这对我有用:

String outputDir = ctx.getOutputDirectory() + "/../"