【问题标题】:TestNG run once before running all test suites mentioned in the pom.xmlTestNG 在运行 pom.xml 中提到的所有测试套件之前运行一次
【发布时间】:2021-09-30 05:02:06
【问题描述】:

我在 Surefire 插件中设置了多个 testng.xml 文件,以便我可以run the automation test from command prompt using Maven

现在,我面临一个问题。我如何设置suiteListener 来执行一些任务,例如删除从上次运行中捕获的文件和屏幕截图。 (一次运行包含多个套件文件)

现在发生的是第一个测试套件运行并捕获屏幕截图并创建日志。当第二个套件运行时,它会清除之前捕获的屏幕截图和日志,并为此运行创建一个新的屏幕截图。

有没有一种方法可以让这个方法每次运行运行一次,并且不是在每个测试套件之前运行一次

import java.io.IOException;
import org.testng.ISuite;
import org.testng.ISuiteListener;
import com.company.appium.base.BaseTest;
public class suiteListener extends BaseTest implements ISuiteListener {

    @Override
    public void onStart(ISuite suite) {
        // This method will be executed before Test Suite run
        try {
            deletePreviousScreenShots();
            System.out.println("Inside onStart of suiteListener");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Before starting test suite: " + suite.getName() + " in onStart() method");
    }
    @Override
    public void onFinish(ISuite suite) {
        // This method will be executed at the end of the Test Suite run
        System.out.println("After executing the test suite: " + suite.getName() + " in onFinish() method");
    }
} 

【问题讨论】:

  • 假设你有5个套件文件,你是说上面的代码在5个套件全部完成后只需要运行一次吗?
  • 是的,没错。基本上我要做的是清除上次运行的屏幕截图和测试报告。这样我就不会从所有以前的运行中建立文件。当我在surefire插件中只有一个测试套件时,我现在拥有的代码可以工作。当我说 5 个测试套件时,这个 onstart 方法会在每个套件的开头调用,并清除前一个测试套件创建的文件。我希望所有文件都可用于每次运行,然后当我重新运行前一次运行的文件时被删除。

标签: java maven testng testng.xml


【解决方案1】:

因为我不确定是否为每个套件运行创建了一个新的 suiteListener 实例。因此,只有当通过ISuite suite 参数接收到的套件名称与您提供的“第一个”xml 套件文件匹配时,您才能继续删除。

public class suiteListener extends BaseTest implements ISuiteListener {

    private static final String firstTest = "suite1";
    @Override
    public void onStart(ISuite suite) {
        if(!suite.getName().equals(firstTest)) {
            return;
        }

        // rest of the code
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2020-06-03
    相关资源
    最近更新 更多