如何在@AfterClass 方法中将 ITestNGMethod 作为参数传递?

How to pass ITestNGMethod in @AfterClass method as parameter?

我想知道在@Test(测试方法)、@AfterClass 被调用之后。所以要知道我写了下面的代码来做到这一点:

@AfterClass(alwaysRun = true)
  public synchronized void tearDownClass(final ITestNGMethod itx) {

  final Method m = itx.getConstructorOrMethod().getMethod();

  System.out.println(m.getName());

}

但是 testNG 抛出以下错误: 方法 tearDownClass 需要 1 个参数,但在 @Configuration 注释中提供了 0 个参数。

所以我的问题是如何解决上述问题,或者有没有其他方法可以实现。

提前致谢。

它不能工作,因为 ITestNGMethod 代表一个方法,而一个 class 可以有很多方法。

您可以改为注入 ITestContext 并在其中寻找您的方法。

这没有意义:它是一个 @AfterClass 方法,在 class 的所有测试方法都具有 运行 之后被调用。您的具体场景(想知道调用的最后一个方法)非常具体,因此您应该自己实现它。

例如,您可以创建一个字段 lastMethod 并为每个方法设置其名称,这样当您到达 @AfterClass 时,您就知道最后一个 运行。