欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

gitlab readme 测试覆盖率添加

程序员文章站 2022-03-04 09:34:29
...

第一步:添加一个子模块:contract-coverage,然后在这个空的模块里只要发一个pom.xml即可,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.ivy.contract-management</groupId>
        <artifactId>parent</artifactId>
        <version>1.1-RELEASE</version>
        <relativePath>../</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>contract-coverage</artifactId>

    <properties>
        <build.directory.contract-master>../contract-master/target</build.directory.contract-master>
        <build.directory.contract-element>../contract-element/target</build.directory.contract-element>

        <classes.directory.contract-master>../contract-master/target/classes</classes.directory.contract-master>
        <classes.directory.contract-element>../contract-element/target/classes</classes.directory.contract-element>

        <sources.directory.contract-master>../contract-master/src/main/java</sources.directory.contract-master>
        <sources.directory.contract-element>../contract-element/src/main/java</sources.directory.contract-element>

        <generated-sources.directory.contract-master>../contract-master/target/generated-sources/annotations
        </generated-sources.directory.contract-master>
        <generated-sources.directory.contract-element>../contract-element/target/generated-sources/annotations
        </generated-sources.directory.contract-element>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <!-- Copy the ant tasks jar. Needed for ts.jacoco.report-ant . -->
                    <execution>
                        <id>jacoco-dependency-ant</id>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <phase>process-test-resources</phase>
                        <inherited>false</inherited>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.jacoco</groupId>
                                    <artifactId>org.jacoco.ant</artifactId>
                                    <version>${jacoco.version}</version>
                                </artifactItem>
                            </artifactItems>
                            <stripVersion>true</stripVersion>
                            <outputDirectory>${basedir}/target/jacoco-jars</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <!-- Execute an ant task within maven -->
                                <echo message="Generating JaCoCo Reports"/>
                                <taskdef name="report" classname="org.jacoco.ant.ReportTask">
                                    <classpath path="${basedir}/target/jacoco-jars/org.jacoco.ant.jar"/>
                                </taskdef>
                                <mkdir dir="${basedir}/target/coverage-report"/>
                                <report>
                                    <executiondata>
                                        <fileset dir="${build.directory.contract-master}">
                                            <include name="jacoco.exec"/>
                                        </fileset>
                                        <fileset dir="${build.directory.contract-element}">
                                            <include name="jacoco.exec"/>
                                        </fileset>
                                    </executiondata>
                                    <structure name="jacoco-multi Coverage Project">
                                        <group name="jacoco-multi">
                                            <classfiles>
                                                <fileset dir="${classes.directory.contract-master}"/>
                                                <fileset dir="${classes.directory.contract-element}"/>
                                            </classfiles>
                                            <sourcefiles encoding="UTF-8">
                                                <fileset dir="${sources.directory.contract-master}"/>
                                                <fileset dir="${sources.directory.contract-element}"/>
                                            </sourcefiles>
                                        </group>
                                    </structure>
                                    <html destdir="${basedir}/target/coverage-report/html"/>
                                    <xml destfile="${basedir}/target/coverage-report/coverage-report.xml"/>
                                    <csv destfile="${basedir}/target/coverage-report/coverage-report.csv"/>
                                </report>
                            </target>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.jacoco</groupId>
                        <artifactId>org.jacoco.ant</artifactId>
                        <version>${jacoco.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>


</project>

 

第二步: 在README.md里添加地址

     [![pipeline status](http://10.138.61.72/src/msa/contract-management/badges/develop/pipeline.svg)](http://10.138.61.72/src/msa/contract-management/commits/develop)

[![coverage report](http://10.138.61.72/src/msa/contract-management/badges/develop/coverage.svg)](http://10.138.61.72/src/msa/contract-management/commits/develop)

 

第四步:gitlab-ci.yml 里test stage里添加命令

unit-testing:
  stage: test
  tags: ["fund_04"]
  only:
    - master
    - ci
    - develop
  script:
    - mvn clean test
    - mvn clean verify
    - mvn clean verify -f contract-coverage/pom.xml
    - cat contract-coverage/target/coverage-report/html/index.html

 

第五步:在gitlab上,Settings -》CI/CD -》General pipelines settings 下 -》 Test coverage parsing 填上匹配规则: Total.*?([0-9]{1,3})%

 

第六步:

本地执行脚本就能跑出测试覆盖率报告

    - mvn clean test
    - mvn clean verify
    - mvn clean verify -f contract-coverage/pom.xml
    - cat contract-coverage/target/coverage-report/html/index.html

sit环境的话,合并代码时gitlab自动会跑,然后在readme.md 文件里就能看到测试覆盖率