简体   繁体   English

如果不使用TestNGListener抛出@BeforeMethod或@BeforeClass,如何捕获异常

[英]How do I catch exceptions if thrown in @BeforeMethod or @BeforeClass without using a TestNGListener

I have a test class each for a specific functionality and have layers of Test Classes that are considered as BaseClass wherein the driver initialization and test kickoff happens (I avoided using Listener here). 我有一个针对特定功能的测试类,并具有被视为BaseClass的测试类层,其中发生了驱动程序初始化和测试启动(我在这里避免使用Listener)。 Defined an AfterMethod in BaseClass which logs out and also captures the results with the exceptions and stacktrace and logs to a database, if the test fails I am able to capture the exceptions, the exceptions are not caught if the BeforeMethod or BeforeClass fails, all I see is test status which is shown as SKIPPED. 在BaseClass中定义了一个AfterMethod,该方法可以注销并捕获带有异常和stacktrace的结果,并记录到数据库中,如果测试失败,我可以捕获异常,如果BeforeMethod或BeforeClass失败,则不捕获异常,所有看到的是测试状态,显示为“跳过”。 Here is the piece of code that I use in AfterMethod: 这是我在AfterMethod中使用的代码:

public void afterMethodBase(ITestResult result) { 
        logger.debug("TestName : " + result.getTestName());
        logger.info("Method Name : " + result.getMethod().getMethodName());
        ResultLogger.processResult(result);
        Throwable exception = itr.getThrowable(); // this is null when status is skipped
}

How to catch the exceptions raised in BeforeMethod or BeforeClass 如何捕获BeforeMethodBeforeClass引发的异常

You can use a listener with its appropriate method: IConfigurationListener#onConfigurationFailure(ITestResult) 您可以使用侦听器及其适当的方法: IConfigurationListener#onConfigurationFailure(ITestResult)

Check the documentation for more details about listeners. 查看文档以获取有关侦听器的更多详细信息。

If accessing it via the Listeners is not something that you want to do, then why not access them like this ? 如果您不想通过侦听器访问它,那么为什么不这样访问它们呢?

From the ITestResult object that get through your @AfterMethod annotated method: 从通过您的@AfterMethod注释方法的ITestResult对象中:

  • First get hold of all the failed configuration methods via result.getTestContext().getFailedConfigurations().getAllMethods() 首先通过result.getTestContext().getFailedConfigurations().getAllMethods()掌握所有失败的配置方法

  • Then you iterate through the above collection of ITestNGMethod to filter out only @BeforeClass failures by calling org.testng.ITestNGMethod#isBeforeClassConfiguration 然后,通过调用org.testng.ITestNGMethod#isBeforeClassConfiguration遍历上述ITestNGMethod集合,以仅过滤出@BeforeClass故障。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用@BeforeMethod和@BeforeClass批注时,何时将WebDriver驱动程序声明为公共或私有 - When do you declare a WebDriver driver public or private when using @BeforeMethod and @BeforeClass annotations 如何在没有尝试捕获的情况下处理流 map 中引发的异常? - How to handle exceptions thrown in streams map without try catch? 如何使用Java代码捕获可执行文件运行抛出的异常? - How can 1 catch the exceptions thrown by the executable run using Java code? 如何捕获反应堆抛出的异常? - How to catch exceptions thrown from reactor? 如何捕获Spring Security Matchers抛出的异常? - How to catch the exceptions thrown by spring security matchers? ScheduledExecutorService:如何捕获其异常? - ScheduledExecutorService: How do I catch its exceptions? @ControllerAdvice 不捕获抛出的异常 - @ControllerAdvice not catch the thrown exceptions 在Spring Boot应用程序中,我将如何捕获内部抛出的异常,这些异常本来会逃逸到客户端,但不会捕获Spring MVC异常? - In a Spring Boot application, how would I catch internally thrown exceptions that would otherwise escape to the client, but not Spring MVC exceptions? 如何捕获通过读取和写入文件而抛出的所有异常? - How can I catch all the exceptions that will be thrown through reading and writing a file? 如何在Guava重试器中捕获代码引发的异常? - How do I catch the exception thrown by the code inside a Guava retryer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM