如何获取在testng拆卸方法中运行的测试方法的名称? [英] How do I get the name of the test method that was run in a testng tear down method?

查看:630
本文介绍了如何获取在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);
    }
}

...应输出到屏幕:The刚刚运行的测试是: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆