我准备了一个Listener
类来检查End中方法的状态(如果通过或失败)。它工作正常,但在执行Method之后被调用了两次。
我详细说明了我的Listener
班级,TestCase
班级和
输出我低于。
Listener
上课:
package com.qtpselenium.Listener;
import java.util.List;
import org.apache.log4j.Logger;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
import org.testng.internal.Utils;
public class CustomerListner extends TestListenerAdapter implements IInvokedMethodListener{
// Listener class must be part of testng.xml
Logger app_logs = Logger.getLogger("DEBUG");
public void onTestFailure(ITestResult tr) {
app_logs.debug(tr.getName() + " : test case has been Failed ");
System.out.println( tr.getName() + " : test case has been Failed ");
}
public void onTestSkipped(ITestResult tr) {
app_logs.debug(tr.getName() + " : test case has been Skipped ");
System.out.println(tr.getName() + " : test case has been Skipped ");
}
public void onTestSuccess(ITestResult result) {
app_logs.debug(result.getName() + " : test case has been Passed ");
System.out.println(result.getName() + " : test case has been Passed ");
}
public void afterInvocation(IInvokedMethod method, ITestResult result) {
Reporter.setCurrentTestResult(result);
System.out.println("*************TestsListenerAdapter******************");
if (method.isTestMethod()) {
List<Throwable> verificationFailures = ErrorUtil.getVerificationFailures();
//if there are verification failures...
if (verificationFailures.size() != 0) {
//set the test to failed
result.setStatus(ITestResult.FAILURE);
//if there is an assertion failure add it to verificationFailures
if (result.getThrowable() != null) {
verificationFailures.add(result.getThrowable());
}
int size = verificationFailures.size();
//if there's only one failure just set that
if (size == 1) {
result.setThrowable(verificationFailures.get(0));
} else {
//create a failure message with all failures and stack traces (except last failure)
StringBuffer failureMessage = new StringBuffer("Multiple failures (").append(size).append("):nn");
for (int i = 0; i < size-1; i++) {
failureMessage.append("Failure ").append(i+1).append(" of ").append(size).append(":n");
Throwable t = verificationFailures.get(i);
String fullStackTrace = Utils.stackTrace(t, false)[1];
failureMessage.append(fullStackTrace).append("nn");
}
//final failure
Throwable last = verificationFailures.get(size-1);
failureMessage.append("Failure ").append(size).append(" of ").append(size).append(":n");
failureMessage.append(last.toString());
//set merged throwable
Throwable merged = new Throwable(failureMessage.toString());
merged.setStackTrace(last.getStackTrace());
result.setThrowable(merged);
}
}
}
}
public void beforeInvocation(IInvokedMethod arg0, ITestResult arg1) {}
}
***********************
`TestCase` class:
package com.qtpselenium.suiteA;
import org.testng.SkipException;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.qtpselenium.util.TestUtil;
public class TestCaseA1 extends TestSuiteBase{
String runmode[] = null;
int count=-1;
//Checking runMode of the Test case in Suite
@BeforeTest
public void checkTestcaseskip(){
//this.getclass().getSimpleName() method returns the name of the class
App_Logs.debug("checking run mode of " +this.getClass().getSimpleName() + " testcase");
if(!TestUtil.IsTestCaseRunnable(suiteAxlsx, this.getClass().getSimpleName())){
App_Logs.debug("Run mode of testcase " + this.getClass().getSimpleName() + " is N");
throw new SkipException("Run mode of testcase " + this.getClass().getSimpleName() + " is N");
}else
App_Logs.debug("Run mode of testcase " + this.getClass().getSimpleName() + " is Y");
runmode = TestUtil.getDataSetRunmode(suiteAxlsx, this.getClass().getSimpleName());
}
@Test(dataProvider="getTestData")
//No of arguments passed should be equal to no of dataelement in Testcase1 and type should be matched
public void TestcaseA1(
String col1,
String col2,
String col3,
String col4
){
count++;
if(runmode[count].equalsIgnoreCase("N")){
throw new SkipException("The runmode for Testdata is N for row number " + count );
}else{
App_Logs.debug("Test data of testcase : " + this.getClass().getSimpleName());
App_Logs.debug(col1+"--"+col2+"--"+col3+"--"+col4);
}
}
//Data provide to TestcaseA1
@DataProvider
public Object[][] getTestData(){
return TestUtil.getdata(suiteAxlsx, this.getClass().getSimpleName());
}
}
*****************Testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestNG Dadadriver suite" allow-return-values="true" >
<!-- suite name="Suite Name" AND Please remember all the tag names should be lowered case otherwise the sytem will throw an error -->
<listeners>
<!-- packagename.customerlistner_classname -->
<listener class-name="com.qtpselenium.Listener.CustomListner"/>
</listeners>
<suite-files>
<suite-file path="./suiteA.xml" />
<suite-file path="./suiteB.xml" />
<suite-file path="./suiteC.xml" />
</suite-files>
</suite>
*******************
output:
uite read from excel suiteA Suite passed by calling method suiteA
Run mode is NforsuiteA Test case
*************TestsListenerAdapter******************
Data is Returned for Test caseTestCaseA1
*************TestsListenerAdapter******************
TestcaseA1 : test case has been Skipped
TestcaseA1 : test case has been Skipped
*************TestsListenerAdapter******************
TestcaseA1 : test case has been Skipped
TestcaseA1 : test case has been Skipped
*************TestsListenerAdapter******************
TestcaseA1 : test case has been Skipped
TestcaseA1 : test case has been Skipped
*************TestsListenerAdapter******************
TestcaseA1 : test case has been Skipped
TestcaseA1 : test case has been Skipped
I have 4 data set but it's writing 8 times that test case has been skipped.
答案 0 :(得分:0)
我遇到了与testng最新版本6.8相同的问题,通过将pom.xml中的依赖版本更改为6.10解决了问题。
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
</dependency>
答案 1 :(得分:0)
我也遇到过同样的问题,但是我发现了以下解决方案-
在类级别(@Test所在的位置)添加@Listeners批注
例如
@Listeners(listeners.ReportListener.class)
public class Test1
{
@Test
public void test1()
{
System.out.println("in Test1 method");
}
}
但是在套件标签下的testNg.xml中不要包含 listeners 标签。从testng.xml中删除
仅在测试类上添加@listeners批注。它将起作用。