Java 如何获取在 testng 拆卸方法中运行的测试方法的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2952202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the name of the test method that was run in a testng tear down method?
提问by Zee Spencer
Basically, I have a teardown method that I want to log to the console which test was just run. How would I go about getting that string?
基本上,我有一个拆卸方法,我想将其登录到刚刚运行测试的控制台。我将如何获得该字符串?
I can get the class name, but I want the actual method that was just executed.
我可以获得类名,但我想要刚刚执行的实际方法。
public class TestSomething {
@AfterMethod
public void tearDown() {
System.out.println("The test that just ran was: " + getTestThatJustRanMethodName());
}
@Test
public void testCase() {
assertTrue(1 == 1);
}
}
...should output to the screen: "The test that just ran was: testCase"
...应该输出到屏幕:“刚刚运行的测试是:testCase”
However, I don't know the magic that getTestThatJustRanMethodName
should actually be.
但是,我不知道getTestThatJustRanMethodName
实际上应该是什么魔法。
采纳答案by Cedric Beust
Declare a parameter of type ITestResult in your @AfterMethod and TestNG will inject it:
在 @AfterMethod 中声明一个 ITestResult 类型的参数,TestNG 将注入它:
@AfterMethod
public void afterMethod(ITestResult result) {
System.out.println("method name:" + result.getMethod().getMethodName());
}
回答by JacekM
If you want to get the method name beforethe test is executed you can use the following:
如果要在执行测试之前获取方法名称,可以使用以下命令:
import java.lang.reflect.Method;
@BeforeMethod
public void nameBefore(Method method)
{
System.out.println("Test name: " + method.getName());
}
回答by René Link
Just declare a java.lang.reflect.Method
parameter.
只需声明一个java.lang.reflect.Method
参数。
@BeforeMethod
public void beforeTestMethod(Method testMethod){
System.out.println("Before Testmethod: " + testMethod.getName());
}
But TestNG allows you to injecta lot more ;)
但是 TestNG 允许您注入更多;)
- Any @Beforemethod or @Testmethod can declare a parameter of type
ITestContext
.- Any @AfterMethodmethod can declare a parameter of type
ITestResult
, which will reflect the result of the test method that was just run.- Any @Beforeand @Aftermethods can declare a parameter of type
XmlTest
, which contain the current tag.- Any @BeforeMethod(and @AfterMethod) can declare a parameter of type
java.lang.reflect.Method
. This parameter will receive the test method that will be called once this @BeforeMethod finishes (or after the method as run for @AfterMethod).- Any @BeforeMethodcan declare a parameter of type
Object[]
. This parameter will receive the list of parameters that are about to be fed to the upcoming test method, which could be either injected by TestNG, such asjava.lang.reflect.Method
or come from a@DataProvider
.- Any @DataProvidercan declare a parameter of type
ITestContext
orjava.lang.reflect.Method
. The latter parameter will receive the test method that is about to be invoked.
- 任何@Before方法或@Test方法都可以声明类型为 的参数
ITestContext
。- 任何@AfterMethod方法都可以声明一个 type 参数
ITestResult
,它将反映刚刚运行的测试方法的结果。- 任何@Before和@After方法都可以声明一个类型为 的参数
XmlTest
,其中包含当前标签。- 任何@BeforeMethod(和@AfterMethod)都可以声明一个类型为 的参数
java.lang.reflect.Method
。此参数将接收将在此@BeforeMethod 完成后(或在为@AfterMethod 运行的方法之后)调用的测试方法。- 任何@BeforeMethod都可以声明一个类型为 的参数
Object[]
。此参数将接收即将提供给即将到来的测试方法的参数列表,这些参数可以由 TestNG 注入,例如java.lang.reflect.Method
或来自@DataProvider
.- 任何@DataProvider都可以声明类型为
ITestContext
或的参数java.lang.reflect.Method
。后一个参数将接收即将被调用的测试方法。
回答by Magnilex
Another (although not as simple as Cedric's answer) way that TestNG supports this is to register a listener:
TestNG 支持的另一种(虽然不像Cedric 的回答那么简单)方式是注册一个监听器:
@Listeners({MethodListener.class})
public class ListenerTest {
@Test
public void someTest() {
}
}
Where the listener could look like this:
听众可能看起来像这样:
public class MethodListener implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
System.out.println(method.getTestMethod().getMethodName());
}
}
This particular listener would print the method name (i.e. someTest
) to the console. It would be executed after everyexecuted test.
这个特定的侦听器会将方法名称(即someTest
)打印到控制台。它将在每次执行测试后执行。
If you are generating the testSuite programmatically then you can add the listener as follows instead of adding @Listeners({MethodListener.class})
over each test class
如果您以编程方式生成 testSuite,则可以按如下方式添加侦听器,而不是添加@Listeners({MethodListener.class})
每个测试类
List<String> listeners = new ArrayList<String>();
listeners.add(MethodListener.class.getName());
testSuite.setListeners(listeners);
回答by Y-B Cause
In my own project I access this data thanks to a JUnit @Rule
.
在我自己的项目中,我通过 JUnit 访问这些数据@Rule
。
String testName;
String className;
@Rule
public TestWatcher watcher = new TestWatcher() {
public void starting(Description description) {
testName = description.getMethodName();
className = description.getClassName();
logger.info("Starting test " + testName + " in class " + className);
}
};