,我有一个带有dataprovider的@Test方法,我有一个Listener.class,我的目标是当我的@Test方法成功时,testrail中case的状态在onTestSuccess Listener方法中自动设置为“Passed” class,这个过程没问题,但是当我为@Test方法使用dataprovider时,这会导致问题
我希望同样的方法必须工作(让我们说)三次,因为数据提供者和不同的case id数据必须从@Test方法的每次迭代发送到onTestSuccess方法
My Listener.class
package com.myproject.test.listeners;
import java.lang.reflect.Method;
import org.testng.IClass;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ISuite;
import org.testng.ISuiteListener;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class Listener implements ITestListener, ISuiteListener, IInvokedMethodListener {
...
public void onTestStart(ITestResult result) {
}
public void onTestSuccess(ITestResult result) {
try {
Program pr = new Program();
System.out.println("onTestSuccess Method for :" + result.getName());
String TestID = null;
String TestRunID = null;
for (Method testMethod : result.getTestClass().getRealClass().getMethods()) {
if (testMethod.getName().equals(result.getName()) && testMethod.isAnnotationPresent(UseAsTestRailId.class)) {
UseAsTestRailId useAsTestName = testMethod.getAnnotation(UseAsTestRailId.class);
TestID = Integer.toString(useAsTestName.testRailCaseId());
TestRunID = Integer.toString(useAsTestName.testRailRunId());
System.out.println("Case ID---> " + TestID + " Run ID--> " + TestRunID);
// 1 = Passed
pr.enterTestResult(TestRunID, TestID, 1);
}
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
...
}
我的测试类(SettingsTests.java)包括我的@Test方法(checkCurrentPasswordFormatIsValidatedTest)
@Listeners(com.test.listeners.Listener.class)
//listener annotation row is written in BaseTest class
public class SettingsTests extends BaseTest {
...
/**
* Test Case - C5001275 - Check that "Please enter at least 8 characters."
* message is displayed when entered value into "Current Password" field in
* wrong format This case will run two times!
*
* @param currentPasswordValue
*/
@Test(dataProvider = "currentPasswordTestWithWrongValue")
@UseAsTestRailId(testRailCaseId = 5001275,testRailRunId = 56662)
// aim is that to send different case id for each iteration,now even if method works twice , only one testRailCaseId is sent
public void checkCurrentPasswordFormatIsValidatedTest(String currentPasswordValue) {
logger.trace("STARTING TEST: checkCurrentPasswordFormatisValidatedTest");
logger.trace("Test Step : Enter current password in wrong format");
settingsPageObject.enterCurrentPassword(currentPasswordValue);
logger.trace("Test Step : Click on the button 'UPDATE' ");
settingsPageObject.clickOnUpdateButton();
logger.trace("Expected Result: The message 'Please enter at least 8 characters.' is displayed on screen.");
Assert.assertEquals(settingsPageObject.getCurrentPasswordWrongText(), "Please enter at least 8 characters.");
}
@DataProvider(name = "currentPasswordTestWithWrongValue")
public static Object[][] validateTestWithCurrentPasswordInWrongFormat() {
return new Object[][] { { RandomStringUtils.randomAlphabetic(7) }, { RandomStringUtils.randomAlphabetic(1) } };
}
...
}
我的注释类(UseAsTestRailId.java)
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface UseAsTestRailId
{
int testRailCaseId() default 0;
int testRailRunId() default 0;
String[] tags() default "";
}
一些@Test方法设置两个案例状态,一些@Test方法在testRail中设置三个案例状态,因此需要更多dataprovider数据集需要更多案例id,它必须是动态的
答案 0 :(得分:3)
您可以使用testresult对象中的setattribute值来设置自定义值。从Reporter:Reporter.getCurrentTestresult获取currentresult,然后获取setAttribute(" TC_id",sasdf)并在结果对象上使用getAttribute(" TC_id")在ontestsuccess中使用它。