[英]How do I stop selenium automation if 20% or 1st 20 test cases test methods are failed?
我有 100 个测试用例,如果 20% 或前 20 个测试用例都失败了,我该如何停止执行? 已经有 testng ITestResult 我应该在哪里中断构建?
@Test(retryAnalyzer = ReTryFail.class, dataProvider = "SanityTCTest", dataProviderClass = utility.Xlsdataprovider.class, groups = "Dashboard", alwaysRun = true)
public void Sanity_TC001(LinkedHashMap<String, String> data) throws InterruptedException, SQLException {
Some code
}
@Test(retryAnalyzer = ReTryFail.class, dataProvider = "SanityTCTest", dataProviderClass = utility.Xlsdataprovider.class, groups = "Dashboard", alwaysRun = true)
public void Sanity_TC002(LinkedHashMap<String, String> data) throws InterruptedException, SQLException {
Some code
}
@Test(retryAnalyzer = ReTryFail.class, dataProvider = "SanityTCTest", dataProviderClass = utility.Xlsdataprovider.class, groups = "Dashboard", alwaysRun = true)
public void Sanity_TC003(LinkedHashMap<String, String> data) throws InterruptedException, SQLException {
Some code
}
///////////////////////////////
如果结果,我在哪里可以打破这个套件,“失败超过 20?我需要创建新的 class 还是我可以在下面添加相同的?
@AfterMethod(alwaysRun = true)
public void reporterDataResults(ITestResult Result) throws InterruptedException {
boolean flag = false;
Testfail = TestResultStatus.Testfail;
/*System.out.println("test fail flag in AfterMethod: " + Testfail); */
if (Result.getStatus() == ITestResult.SKIP) {
Resmark.put(Result.getName(), "");
captureScreenShot(Result, "SKIP", getDriver());
Reporter.log(Result.getName() + " is SKIPPED");
Add_Log.info(Result.getName() + " is SKIPPED");
TestResultTL.put(Result.getName(), "SKIP");
if (!(getDriver() == null)) {
closeWebBrowser();
}
} else if (Result.getStatus() == ITestResult.FAILURE) {
Collection<String> values = TestResultStatus.mpaskeysnew.get(Result.getName());
String resultout = String.join(" | ", values);
System.out.println(resultout);
Resmark.put(Result.getName(), resultout);
captureScreenShot(Result, "FAIL", getDriver());
Reporter.log(Result.getName() + " is FAIL");
Add_Log.info(Result.getName() + " is FAIL");
if (!(getDriver() == null)) {
closeWebBrowser();
}
TestResultTL.put(Result.getName(), "FAIL");
} else {
captureScreenShot(Result, "PASS", getDriver());
Resmark.put(Result.getName(), "");
Reporter.log(Result.getName() + " is PASS");
Add_Log.info(Result.getName() + " is PASS");
if (!(getDriver() == null)) {
closeWebBrowser();
}
TestResultTL.put(Result.getName(), "PASS");
}
Testskip = false;
TestResultStatus.Testfail = false;
}
您可以实现ISuiteListener
并在onFinish
方法中访问ISuite
和ISuiteResult
然后你可以做
public void onFinish(ISuite suite) {
final Map<java.lang.String,ISuiteResult> res = suite.getResults();
for (ISuiteResult r : res.values()) {
context = r.getTestContext() ;
failedTestCases =context.getFailedTests().size();
}
}
size()
将为您提供该套件的失败测试数。 一旦您知道该数字,您就可以使用本文中的策略实施以停止执行
如果您的测试用例位于不同的套件中,那么在每次调用onFinish
方法时,您可以计算每个套件的失败测试用例数,并基于该停止执行。
另一种选择是实现ITestListener
。 在onTestFailure
方法中,您可以访问ITestResult
您可以计算onTestFailure
方法被调用的次数并基于该停止执行。 我认为在您的情况下实施ITestListener
更合适和更容易。
在这里,我进行了编辑以解释您将如何实现侦听器
import org.testng.ISuiteListener;
public class listener implements Itestlistener {
public int i = 0;
public void onTestFailure(ITestResult result) {
result.getName();
i++;
//your break logic goes here
if (i ==20){
// do something or call some function to stop execution
}
}
}
您可以在此处阅读有关 testng 听众的更多信息。
对于上面的编辑(如果你想 go 那样)。 尽管我仍然认为您应该实现更清洁的侦听器。 只有在测试失败时才会调用它。
但是做和我在onTestFailure
方法中做的一样的事情,添加一个计数器并在else if
中增加它。
public int i = 0; //do this in your class
然后在你的方法中
else if (Result.getStatus() == ITestResult.FAILURE) {
i++; //increase counter here
Collection<String> values = TestResultStatus.mpaskeysnew.get(Result.getName());
String resultout = String.join(" | ", values);
System.out.println(resultout);
Resmark.put(Result.getName(), resultout);
captureScreenShot(Result, "FAIL", getDriver());
Reporter.log(Result.getName() + " is FAIL");
Add_Log.info(Result.getName() + " is FAIL");
if (!(getDriver() == null)) {
closeWebBrowser();
}
TestResultTL.put(Result.getName(), "FAIL");
if (i==20){
// stop execution here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.