行为驱动:Cucumber + Selenium + Java(二) - extentreports 测试报告+jenkins持续集成

时间:2024-01-24 10:54:15

1、extentreports 测试报告

pom文件

 <dependency>
            <groupId>com.vimalselvam</groupId>
            <artifactId>cucumber-extentsreport</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>3.0.6</version>
        </dependency>
        <dependency>
            <groupId>com.relevantcodes</groupId>
            <artifactId>extentreports</artifactId>
            <version>2.40.2</version>
        </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
     <artifactId>spring-test</artifactId>
    <version>5.0.2.RELEASE</version>
    </dependency>

cucumber入口类

  • CucumberOptions中加入插件的属性
  • @BeforeClass注解方法中,可以使用setReportPath方法指定插件的报告生成位置
  • @AfterClass注解方法中,可以使用loadXMLConfig方法指定报告配置文件的位置
package com.cucumber.demo;

import com.aventstack.extentreports.ResourceCDN;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.cucumber.listener.Reporter;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

import java.io.File;
//加入注释语句位置,不能运行所有用例集合
//@RunWith(Cucumber.class)
@ContextConfiguration("classpath:cucumber.xml")
@CucumberOptions(
        plugin = {"com.cucumber.listener.ExtentCucumberFormatter:target/extent-report/report.html"},
        format = {"pretty", "html:target/cucumber", "json:target/cucumber.json"},
        features = {"src/test/resources/feature/"},
        glue = {"com.cucumber.demo","com.po.demo"},
        monochrome = true)
public class RunCukesTest extends AbstractTestNGCucumberTests {

    @BeforeClass
    public static void setup() {
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("target/extent-report/report.html");
        htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);

    }

    @AfterClass
    public static void tearDown() {
        Reporter.loadXMLConfig(new File("src/test/resources/extent-config.xml"));//1
        Reporter.setSystemInfo("user", System.getProperty("user.name"));
        Reporter.setSystemInfo("os", "Windows");
        Reporter.setTestRunnerOutput("Sample test runner output message");
    }

}

extent-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<extentreports>
    <configuration>
        <theme>dark</theme>
        <encoding>UTF-8</encoding>
        <documentTitle>Cucumber Extent Reports</documentTitle>
        <reportName>Cucumber Extent Reports</reportName>
        <!--<reportHeadline> - v1.0.0</reportHeadline>-->
        <protocol>https</protocol>
        <dateFormat>yyyy-MM-dd</dateFormat>
        <timeFormat>HH:mm:ss</timeFormat>
        <scripts>
            <![CDATA[
                $(document).ready(function() {

                });
            ]]>
        </scripts>
        <!-- custom styles -->
        <styles>
            <![CDATA[

            ]]>
        </styles>
    </configuration>
</extentreports>

2、jenkins持续集成

2.1、在Jenkins中安装cucumber插件

需要安装的插件如下:

2.2、发布Cucumber测试结果报告

2.3、发布HTML格式的报告

2.4、发布Cucumber结果报告

2.5、点击应用保存,并构建

2.6、构建完成后,效果如下

构建完之后,会多生成这两个链接,点击Cucumber Reports后就可以看到好看的测试报告了,下面是部分截图:

 

 

extentreport

 

注意:cucumber的run文件不要使用标签@runwith,批量执行多个feature文件时

参考文章:

https://www.cnblogs.com/rechin/p/9411669.html

https://blog.csdn.net/qq_33320515/article/details/80648425

https://blog.csdn.net/qq_33320515/article/details/80648425