简体   繁体   English

如何在范围报告中明确地使测试步骤失败?

[英]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.

相关问题 如何将 Extent 或 Junit 报告报告和集成到“For循环”测试场景并添加断言报告通过和失败 - How to report and integrate Extent or Junit report to “For loop” test scenarios and add assertion report Pass and Fail 如何在范围报告中捕获所有测试用例? - How to capture all test cases in extent report? 如何在 JUnit 中显式或手动失败测试用例 - How to explicitly or manually fail a test case in JUnit 范围报告report.endTest(test)方法? - Extent report report.endTest(test) method? 在范围报告中添加测试用例失败原因 - add test cases failure reason in extent report 范围报告测试状态记录失败 - Extent report test status recording for failure Cucumber 范围报告 - 如何将失败案例的屏幕截图添加到范围报告 - Cucumber extent report - how to add screenshot of failed case to the extent report 如何在测试结果列 Extent HTML 报告中将 TestNg“dependsOnMethods”显示为单独的节点 - How to display the TestNg "dependsOnMethods" as separate node in the test result column Extent HTML report 如何使用 selenium 4 和 java 编写通用的 base64 截图方法并将其附加到测试失败的范围报告中? - How to write a generic base64 screenshot method using selenium 4 with java and attach it to extent report on test failure? 如何在 for 循环测试中使用 UI 验证来自 Excel 的匹配值并在范围报告中显示结果 - how to validate matching values from Excel with UI, inside the for loop test and show the result in extent report
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM