【问题标题】:How to print exceptions on console? I am using TestNG and Maven如何在控制台上打印异常?我正在使用 TestNG 和 Maven
【发布时间】:2021-05-17 12:12:33
【问题描述】:

我想在控制台上查看异常。我正在使用带有 Maven 的 TestNG 作为构建工具。我在 Maven surefire 插件中定义了我的 testng.xml。

【问题讨论】:

  • 在哪个控制台?为什么您目前看不到它们?你能添加更多上下文吗?
  • @AlexeyR。在 IDE 控制台上。异常显示在测试失败标签下的 TestNG 选项卡上。
  • @shriv ☝️也设置您的投票。我花了我的。
  • @PDHide 重新打开问题以接受其他答案。
  • @DebanjanB 添加了答案谢谢

标签: java selenium maven testng maven-surefire-plugin


【解决方案1】:

如果您使用任何类型的日志记录框架,您可能应该坚持使用它。 否则,您始终可以使用https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Throwable.html#printStackTrace()(和兄弟姐妹)打印异常,例如

...
} catch (Exception e) {
    e.printStackTrace(System.out);
    e.printStackTRace(System.err);
}
...

【讨论】:

    【解决方案2】:

    https://www.javadoc.io/doc/org.testng/testng/latest/org/testng/reporters/VerboseReporter.html

    你应该使用上面的报告器,但是构造函数需要一个字符串,所以你不能使用 testng.xml 来初始化它(如果有人知道如何在 testng.xml 中将字符串参数传递给监听器,请在此处添加)

    所以解决方法是通过脚本添加监听器并通过java入口文件启动testng。

    public static void main(String[] args) {
    
    
            TestNG testng = new TestNG();
    
            // Create a list of String
            List<String> suitefiles = new ArrayList<String>();
    
            // Add xml file which you have to execute
            suitefiles.add(prop.getProperty("path_to_your_existing_testngxml\testng.xml"));
    
            // now set xml file for execution
            testng.setTestSuites(suitefiles);
            
            testng.addListener(new VerboseReporter("[TestNG] "));
    
            // finally execute the runner using run method
            testng.run();
    
    }
    

    输出:

    注意

    由于此报告器构造函数需要一个字符串,因此您不应在 testng.xml 中提供它,您将收到初始化错误

    【讨论】:

    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2013-09-21
    • 2022-07-05
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    相关资源
    最近更新 更多