【问题标题】:How do I get the name of the test method that was run in a testng tear down method?如何获取在 testng 拆卸方法中运行的测试方法的名称?
【发布时间】:2011-02-26 11:43:58
【问题描述】:

基本上,我有一个拆卸方法,我想登录到刚刚运行测试的控制台。我将如何获取该字符串?

我可以得到类名,但我想要刚刚执行的实际方法。

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”

但是,我不知道getTestThatJustRanMethodName 究竟应该有什么魔力。

【问题讨论】:

    标签: java unit-testing testng


    【解决方案1】:

    在@AfterMethod 中声明一个 ITestResult 类型的参数,TestNG 将注入它:

    @AfterMethod
    public void afterMethod(ITestResult result) {
      System.out.println("method name:" + result.getMethod().getMethodName());
    }
    

    【讨论】:

    • tks,我使用'Reporter.setCurrentTestResult(result);'将报告流更改为测试方法
    【解决方案2】:

    如果您想在测试执行之前 获取方法名称,您可以使用以下内容:

    import java.lang.reflect.Method;
    
    @BeforeMethod
    public void nameBefore(Method method)
    {
        System.out.println("Test name: " + method.getName());       
    }
    

    【讨论】:

    • 也适用于@AfterMethod ... OP 要求的内容 ;)
    • 帮助了我 ✓
    • 对我没用
    【解决方案3】:

    只需声明一个java.lang.reflect.Method 参数。

     @BeforeMethod
     public void beforeTestMethod(Method testMethod){
        System.out.println("Before Testmethod: " + testMethod.getName());       
     }
    

    但 TestNG 允许您inject 更多;)

    • 任何 @Before 方法或 @Test 方法都可以声明 ITestContext 类型的参数。
    • 任何 @AfterMethod 方法都可以声明 ITestResult 类型的参数,这将反映刚刚运行的测试方法的结果。
    • 任何 @Before@After 方法都可以声明 XmlTest 类型的参数,其中包含当前标记。
    • 任何 @BeforeMethod(和 @AfterMethod)都可以声明 java.lang.reflect.Method 类型的参数。此参数将接收此 @BeforeMethod 完成后(或在为 @AfterMethod 运行的方法之后)将调用的测试方法。
    • 任何 @BeforeMethod 都可以声明 Object[] 类型的参数。此参数将接收即将被提供给即将到来的测试方法的参数列表,这些参数可以由 TestNG 注入,例如 java.lang.reflect.Method 或来自 @DataProvider
    • 任何 @DataProvider 都可以声明 ITestContextjava.lang.reflect.Method 类型的参数。后一个参数将接收即将被调用的测试方法。

    【讨论】:

      【解决方案4】:

      TestNG 支持的另一种(虽然不像Cedric's answer 那样简单)方法是register a listener

      @Listeners({MethodListener.class})
      public class ListenerTest {
      
        @Test
        public void someTest() {
        }
      
      }
      

      侦听器可能如下所示:

      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());
        }
      }
      

      这个特定的侦听器会将方法名称(即someTest)打印到控制台。它会在每个执行的测试之后执行。

      如果您以编程方式生成 testSuite,则可以按如下方式添加侦听器,而不是在每个测试类上添加 @Listeners({MethodListener.class})

          List<String> listeners = new ArrayList<String>();
          listeners.add(MethodListener.class.getName());
          testSuite.setListeners(listeners);
      

      【讨论】:

      • 你可以只使用 testResult.getMethod().getMethodName()。无需获取 IInvokedMethod。
      【解决方案5】:

      在我自己的项目中,我通过 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);
          }
      };
      

      【讨论】:

      • 这个问题是关于 TestNG 而不是 JUnit
      猜你喜欢
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多