本文介绍了在TestNG上检索测试名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否可以像在JUnit中那样检索当前正在运行的测试名称(使用 getName()规则)?

Can I retrieve currently running test name like in JUnit (using getName() or rules)?

@Test
public void fooBar(){
     System.out.println(magic()); //should print "fooBar"
}

P.S.我不想使用一些基于堆栈跟踪的自写工具.

P.S. I don't want use some self-written tool based on stack traces.

推荐答案

根据TestNG文档,网址为: http://testng.org/doc/documentation-main.html 您可以实现可能可以帮助您解决问题的侦听器.

According the to TestNG documentation at: http://testng.org/doc/documentation-main.htmlyou can implement listeners that might be able to help you with your problem.

请参阅第5.16节"TestNG侦听器",尤其是IInvokedMethodListener(javadoc: http://testng.org/javadocs/org/testng/IInvokedMethodListener.html ).您可以连接到beforeInvocation来获取方法名称,将其保留在某个地方,然后在测试中使用它.当然,您可以在侦听器实现中立即使用这些详细信息.

Look at section 5.16 TestNG Listeners, and in particular the IInvokedMethodListener (javadoc: http://testng.org/javadocs/org/testng/IInvokedMethodListener.html). You can hook into the beforeInvocation to grab the method name, hold onto it somewhere, and then use it in your test. You could of course, just use the the details immediately in your listener implementation.

这篇关于在TestNG上检索测试名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 10:00