简体   繁体   English

如何在通过尝试捕获块的testng报告中使测试失败?

[英]How to make a test fail in testng report which is pass due try catch block?

After executing the below code testng reports shows as pass due to try-catch block, but I need to fail the test if the control come into catch block. 执行以下代码后,testng报告由于try-catch块而显示为通过,但是如果控件进入catch块,则需要使测试失败。

@Test
public void testIt( Method method )
{
    Webdriver d1= new FirefoxDriver();
    d1.get(http://www.google.com);
    String title=d1.getTitle();
    String var=Gogle;
    System.out.println("start execution!!!");
    try {
        Assert.assertEquals( title,var );
    } catch( Throwable er ) {
        System.out.println("Error in method '" + method.getName() + "'.);}
        System.out.println("Execution End!!!");
    }
}

在catch块中调用Assert.fail(String message,Throwable cause),将您选择的错误消息作为第一个参数,并将catch中的Throwable er作为第二个参数。

Your code is swallowing the exception. 您的代码吞没了异常。 You are handling the AssertError and basically just printing out some text rather than allowing TestNG to handle the exception. 您正在处理AssertError ,基本上只是打印一些文本,而不是允许TestNG处理异常。 To fix it, just get rid of the entire try-catch block in your code and just call the .assertEquals on a single line of code. 要解决此问题,只需除去代码中的整个try-catch块,然后在一行代码中调用.assertEquals。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM