[英]How to explicitly fail a test step in Extent Report?
Background背景
I am using Extent Report Cucumber Adapter for my Cucumber based test automation framework built using Java which is running on Junit.我正在将 Extent Report Cucumber Adapter 用于我的基于 Cucumber 的测试自动化框架,该框架使用 Java 构建,该框架在 Junit 上运行。 I am using AssertJ assertions for the test conditions.
我在测试条件中使用 AssertJ 断言。
Scenario设想
One of the test scenarios require to test all the links on a web page.其中一种测试场景需要测试网页上的所有链接。 I have written the code for the same and it's working fine.
我已经编写了相同的代码,并且运行良好。 I am using AssertJ assertion for the test condition under a try block and catching the SoftAssertionError so that my test execution doesn't halt because of the exception and continue validating all the remaining links even if it finds any broken link.
我在 try 块下使用 AssertJ 断言作为测试条件并捕获SoftAssertionError以便我的测试执行不会因为异常而停止并继续验证所有剩余的链接,即使它发现任何断开的链接。
The report mentions the links which are found broken.该报告提到了被发现损坏的链接。 However, ideally this step should get failed as the script found some broken links.
但是,理想情况下,这一步应该会失败,因为脚本发现了一些损坏的链接。 But the report marks the overall step as passed and as a result the scenario is also marked as passed.
但报告将整个步骤标记为通过,因此场景也被标记为通过。 Now I am not able to figure out how can I mark the step as failed in my Extent Report provided there are some links found which are broken.
现在我无法弄清楚如何在我的范围报告中将步骤标记为失败,前提是找到了一些损坏的链接。 Kindly suggest a way to do this.
请提出一种方法来做到这一点。 I am providing a small snippet of my code for a better understanding.
我提供了一小段代码以便更好地理解。
public void ValidateAllLinks(String linkURL) {
try
{
URL url = new URL(linkURL);
//Creating url connection and getting the response code
HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
httpURLConnect.setConnectTimeout(5000);
httpURLConnect.connect();
try {
SoftAssertions softly = new SoftAssertions();
softly.assertThat(httpURLConnect.getResponseCode()).as("This is a broken link: " + linkURL).isGreaterThanOrEqualTo(400);
softly.assertAll();
}catch (SoftAssertionError e)
{
e.printStackTrace();
}
if(httpURLConnect.getResponseCode()>=400)
{
System.out.println(linkURL+" - "+httpURLConnect.getResponseMessage()+" is a broken link.");
ExtentCucumberAdapter.addTestStepLog("<b>" + "<font color=" + "red>" + linkURL+" - "+httpURLConnect.getResponseMessage()+" is a broken link." + "</font>" + "</b>");
}
//Fetching and Printing the response code obtained
else{
System.out.println(linkURL+" - "+httpURLConnect.getResponseMessage()+" is working as expected.");
}
}catch (Exception e) {
}
}
Your example is not a good fit for soft assertions as you are testing only one thing.您的示例不适合软断言,因为您只测试一件事。 Soft assertions are meant to assert a bunch of things and once you have asserted all the things you wanted to, you call
assertAll()
.软断言是为了断言一堆东西,一旦你断言了你想要的所有东西,你就调用
assertAll()
。
I don't understand why you test twice httpURLConnect.getResponseCode()
you could do it once, add the test step log and then fail the test with a fail()
method call (either from AssertJ or JUnit)我不明白为什么你测试两次
httpURLConnect.getResponseCode()
你可以做一次,添加测试步骤日志,然后使用fail()
方法调用(来自 AssertJ 或 JUnit)使测试fail()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.