如何获取在 testng 拆卸方法中运行的测试方法的名称? [英] How do I get the name of the test method that was run in a testng tear down method?
本文介绍了如何获取在 testng 拆卸方法中运行的测试方法的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上,我有一个拆卸方法,我想登录到刚刚运行测试的控制台.我将如何获取该字符串?
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);
}
}
...应该输出到屏幕:刚刚运行的测试是:testCase"
...should output to the screen: "The test that just ran was: testCase"
然而,我不知道 getTestThatJustRanMethodName
实际上应该有什么魔力.
However, I don't know the magic that getTestThatJustRanMethodName
should actually be.
推荐答案
在你的@AfterMethod 中声明一个 ITestResult 类型的参数,TestNG 将注入它:
Declare a parameter of type ITestResult in your @AfterMethod and TestNG will inject it:
@AfterMethod
public void afterMethod(ITestResult result) {
System.out.println("method name:" + result.getMethod().getMethodName());
}
这篇关于如何获取在 testng 拆卸方法中运行的测试方法的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文