【问题标题】:testNG @BeforeMethod use lake param ITestResult extract with parameters ;testNG @BeforeMethod 使用带有参数的湖参数 ITestResult 提取;
【发布时间】:2016-06-28 19:54:56
【问题描述】:

当我只是提取参数时,测试会正确通过 当我尝试操纵参数时,一切都出错了 RunTimeException! 帮助了解问题所在!

 @BeforeMethod
    public void beforeMethodBlock(ITestResult result){
        ReportGenerator.add(result.getParameters());    
    }
public class ReportGenerator {  
    private static Object[][] testData= new Object[50][2];
    private static Integer index;
    public static void add(Object[] data){
        testData[index++]=data;

    }
}

【问题讨论】:

  • 你能分享异常的完整堆栈跟踪吗?
  • 如果我只是在add方法中打印参数程序失败!!
  • java.lang.RuntimeException
  • 在 org.testng.internal.TestResult.toString(TestResult.java:251) 在 org.testng.internal.TestResult.toString(TestResult.java:236)
  • 如果我尝试通过听众做同样的事情同样的问题

标签: java testng qa


【解决方案1】:

看起来您的index 变量从未初始化,并且当您使用Integer 而不是int(为什么?!)默认值为null

只需使用int 并对其进行初始化。

【讨论】:

  • 问题不在 int 如果我只是 print(""+data[0]) 它出错了我的印象是不可能在类中使用 BeforeMethod 注释和侦听器中编写一些语句!可能是库有问题
  • 根据源码,测试结果没有状态,不应该发生的事情。如果您使用的是 testng 6.9.12,您可以在 github 上打开一个问题吗?
  • int 而不是 Integer 有效!但是如果我写 System.out.println(""+data[0]);出错了。
  • 这是一个 toString 问题。如果你避免它,它可能会起作用。我认为你的根本问题是关于 int/Integer
  • 我的版本旧了!首先尝试使用列表 List 并在 add 方法抛出异常
【解决方案2】:
  • testNG 中的BeforeMethod 可以有ITestContext 的参数
  • After 方法可以有 ItestResult 的参数

如果您必须获取接下来要执行的 TestMethod 的名称,可以通过 Method (java.lang.reflect.Method) 对象来完成。

@BeforeMethod public void beforeMethod(Method m) { System.out.println(m.getName()); }

@BeforeMethod public void beforeMethod(ItestContext testContext) { // Do testContext related processing }

参考:-
How do I get the name of the test method that was run in a testng tear down method?

【讨论】:

    猜你喜欢
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多