Interface 覆盖TestNG';s getTestName方法

Interface 覆盖TestNG';s getTestName方法,interface,overriding,testng,Interface,Overriding,Testng,我使用数据提供程序执行TestNG测试。 因此,我通过@BeforeMethod设置testName,并重写getTestName() 到目前为止,这是可行的,但TestNG似乎在一开始就调用了测试的getTestName 在它开始之前。在配置过程中引发异常时会发生这种情况,因此不会执行@BeforeMethod,因此我的测试名称为null 是否仍有调用原始方法的方法,如果我没有覆盖它,该方法将被调用:D因为我实现了一个接口,并且没有从另一个类扩展,我不能使用super.getTestName(

我使用数据提供程序执行TestNG测试。 因此,我通过@BeforeMethod设置testName,并重写getTestName()

到目前为止,这是可行的,但TestNG似乎在一开始就调用了测试的getTestName 在它开始之前。在配置过程中引发异常时会发生这种情况,因此不会执行@BeforeMethod,因此我的测试名称为null

是否仍有调用原始方法的方法,如果我没有覆盖它,该方法将被调用:D因为我实现了一个接口,并且没有从另一个类扩展,我不能使用
super.getTestName()

有什么办法可以解决这个问题吗

@Test(groups = {TestGroups.READY}, description = "check help on each tab")
public class HelpTest extends TestControl implements ITest {

    // overriding to return my individual testname, but is null at the beginning
    @Override
    public String getTestName() {
        return TestControl.getCurrentTestName();
    }

    @DataProvider(name = "tabs")
    public Iterator<Object[]> tabs() {
        Set<Object[]> list = new LinkedHashSet<Object[]>();
        for (Tab tab : Tab.values()) {
            list.add(new Object[]{tab});
        }
        return list.iterator();
    }

    // before the test below starts, i set my individual testname
    @BeforeMethod
    public void setTestName(Method method, Object[] testData) {
        TestControl.setCurrentTestName(method.getName() + "_" + StringUtils.capitalize(testData[0].toString().toLowerCase()));
    }

    // executing the test with the given data provider
    @Test(dataProvider = "tabs")
    public void testHelpSites(Tab tab) throws Exception {
        TestActions.goTab(tab).callHelp(tab).checkHelp();
    }
}
@Test(groups={TestGroups.READY},description=“检查每个选项卡上的帮助”)
公共类HelpTest扩展TestControl实现ITest{
//重写以返回我的单个testname,但在开始处为null
@凌驾
公共字符串getTestName(){
返回TestControl.getCurrentTestName();
}
@数据提供者(name=“tabs”)
公共迭代器选项卡(){
Set list=new LinkedHashSet();
对于(选项卡:Tab.values()){
添加(新对象[]{tab});
}
return list.iterator();
}
//在下面的测试开始之前,我设置了我个人的testname
@预处理法
public void setTestName(方法,对象[]testData){
TestControl.setCurrentTestName(method.getName()+“”+StringUtils.capitalize(testData[0].toString().toLowerCase());
}
//使用给定的数据提供程序执行测试
@测试(dataProvider=“tabs”)
public void testHelpSites(选项卡)引发异常{
TestActions.goTab(tab).callHelp(tab.checkHelp();
}
}

我想我已经弄明白了,我还通过
AbstractWebDriverEventListener
ITestListener
使用TestReporter,并在其
onTestStart(ITestResult结果)
上调用测试的名称,这是
@beforeThod
调用之前的调用源


我通过检查
result.getName()
是否为null来解决这个问题,如果实现了
ITest
,则调用测试的
getTestName()
,如果为null,则使用
result.getMethod.getMethodName()
中的原始名称。不漂亮,但很少见:D

我可以使用ITestNGMethod testng类解决这个问题

ITestNGMethod=result.getMethod();//结果是ITestResult对象 方法。getMethodName();//这将返回方法名

My complete method here:
    @Override
    public void onTestSuccess(ITestResult result) {
    ITestNGMethod method = result.getMethod();
    String message = "Test Execution is Successful:"+method.getMethodName();
    }
希望这有帮助