Java 如何使此测试用例失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22089243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:32:41  来源:igfitidea点击:

How to Make This Test case to Fail

javaseleniumselenium-webdriver

提问by Santhosh S

I have a test case as shown below . it compares 2 data and returns PASS if both data is identical .

我有一个测试用例,如下所示。它比较 2 个数据,如果两个数据相同,则返回 PASS。

the issue is i want this test to fail if there is data mismatch

问题是如果数据不匹配,我希望这个测试失败

CODE is

代码是

WebElement TxtBoxContent = driver.findElement(By.id(WebelementID));

String Content = TxtBoxContent.getAttribute("value");

String ExcelData = Generic.getXlCellValue(xlpath, sheetName, rownum, cellnum);

Content.equals(ExcelData);

Reporter.log(LocationName+" Data Verification -- PASS",true);

采纳答案by rsakhale

In order to make your test fail, you can put a habit of writing Assertions whenever you have something boolean to check

为了让你的测试失败,你可以养成写断言的习惯,只要你有一些布尔值要检查

Assert.assertTrue(Content.equals(ExcelData));

In case your Test should pass for a False condition, then your Assert would become

如果您的测试应通过 False 条件,则您的 Assert 将变为

Assert.assertFalse(Content.equals(ExcelData));

回答by Rahul

You can do this.

你可以这样做。

if(Content.equals(ExcelData))
{ 
Reporter.log(LocationName+" Data Verification -- PASS",true);
}
else
{
 Reporter.log(LocationName+" Data Verification -- Fail",true);
}

回答by Dinu

if (Content.equals(ExcelData)){
        Reporter.log(LocationName+" Data Verification -- PASS",true);
    }else{
        System.out.println(LocationName+ " Data Verification Failed");
    }

In the above code when the condition is not met then it will show in console that the test case has failed along with the location name.

在上面的代码中,当条件不满足时,它将在控制台中显示测试用例与位置名称一起失败。

回答by pavanraju

If you are on TestNg then to fail test case use below thow

如果您在 TestNg 上,则要使测试用例失败,请在下面使用

Assert.fail();

or

或者

Assert.fail("Write your custom error message");

Reporter.log() is for logging, it will not fail your test case.

Reporter.log() 用于日志记录,它不会使您的测试用例失败。