java-sdk接口测试覆盖率统计实践

一、背景

接口覆盖率统计在Java SDK开发中具有重要的意义。它衡量了代码中接口被测试用例覆盖的程度,即测试用例对接口的执行情况进行了多少次验证。

接口覆盖率统计的意义包括:

  1. 质量保证:接口覆盖率统计可以帮助开发团队评估测试的全面性和质量,确保代码的正确性和稳定性。高覆盖率意味着更多的代码路径被测试覆盖,从而减少潜在的错误和缺陷。

  2. 代码可读性:接口覆盖率统计可以作为代码质量指标之一,鼓励开发人员编写可测试、可读性更高的代码。为了提高覆盖率,开发人员通常需要编写更全面的测试用例,这促使他们编写更模块化、可重用和可测试的代码。

  3. 重构支持:接口覆盖率统计还可以帮助开发人员确定哪些代码块需要重构。低覆盖率的接口可能意味着某些功能没有得到充分测试,从而增加了重构时引入错误的风险。

在实践中,可以使用各种工具和框架(如JUnit、Jacoco等)来进行接口覆盖率统计,并根据统计结果进行优化和改进测试策略。

 

二、实践步骤

  1、梳理流水线步骤:1)下载待测代码  2)编译待测工程  3)下载测试代码 4)编译执行testng测试代码打jar包 5)执行jar包(执行自动化case)6)等待**分钟(因为执行服务中执行完case后hold住了进程,因此需要手动预估case执行完时间,人为设置等待时间-sleep **)6)jacoco接口测试统计 7)jacoco接口测试覆盖率统计  8)结束

  2、执行容器里下载jacoco.jar

    3、改动点梳理

   (1)测试代码改动:

    1)改造成用main函数驱动执行testcasepublic class MainTest {

    private static Logger logger = LoggerFactory.getLogger(TestConfigUtil.getClassName());

    public static void main(String[] args) {
////        System.out.println("Welcome to autoTest for JimKV JDK!");
        logger.info("Welcome to autoTest for JimKV JDK!");
        TestNG testNG = new TestNG();
        testNG.setTestClasses(new Class[] {HDelTest.class,HGetAllTest.class,HIncrByTest.class});
        testNG.setParallel(XmlSuite.ParallelMode.METHODS);
        testNG.setThreadCount(1);
        testNG.addListener(new TestNGWithMainClassDemoListener());
        testNG.run();

        // 启动本地服务,然后hold住本地服务
        synchronized (MainTest.class) {
            while (true) {
                try {
                    MainTest.class.wait();
                } catch (InterruptedException e) {
                }
            }
        }
    }

  2)pom.xml的build部分改动:

<build>
        <finalName>jimkv-test</finalName>
        <resources>
            <resource>
                <directory>testNG</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>

            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                    <archive>
                        <manifest>
                            <mainClass>MainTest</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <!--<configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>MainTest1</mainClass>
                        </manifest>
                    </archive>
                </configuration>-->
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
                    </suiteXmlFiles>

                    <systemPropertyVariables>
                        <masterAddress>${jimkv.test.masterAddress}</masterAddress>
                        <jimUrl>${jimkv.test.jimUrl}</jimUrl>

                        <testEnvironment>${jimkv.test.environment}</testEnvironment>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

  3)在src/main下添加assembly

  4) jvm.properties内容:

-Xms1024M -Xmx1024M -Xmn512K -server -XX:+PrintGCDetails -Xloggc:/export/Logs/jimkv-test/gc.log -Xss256K -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:+PrintGCDateStamps -DIGNITE_LOG_DIR=/export/Logs/jimkv-test -DJM.LOG.PATH=/export/Logs/jst_home/jimkv-test -Dhttp.header.validate=false -Dio.netty.leakDetectionLevel=advanced -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/export/dump.hprof

  

  5) assembly.xml
<assembly>
	<id>assembly</id>
	<formats>
		<format>dir</format>
		<format>zip</format>
	</formats>
	<includeBaseDirectory>false</includeBaseDirectory>
	<fileSets>
		<!-- -->
		<fileSet>
			<directory>src/main/resources</directory>
			<outputDirectory>conf</outputDirectory>
			<filtered>true</filtered>
			<fileMode>0644</fileMode>
		</fileSet>
		<fileSet>
			<directory>src/main/assembly/bin</directory>
			<outputDirectory>bin</outputDirectory>
			<includes>
				<include>start.sh</include>
				<include>stop.sh</include>
			</includes>
			<fileMode>0755</fileMode>
		</fileSet>
		<fileSet>
			<directory>src/main/assembly/bin</directory>
			<outputDirectory>bin</outputDirectory>
			<includes>
				<include>jvm.properties</include>
			</includes>
			<filtered>true</filtered>
			<fileMode>0644</fileMode>
		</fileSet>
	</fileSets>
	<dependencySets>
		<dependencySet>
			<outputDirectory>lib</outputDirectory>
		</dependencySet>
	</dependencySets>
</assembly>

  

    (2)流水线该懂点
       1)第(4)步中执行测试代码时执行命令
java -javaagent:/export/servers/tools/jacoco-0.8.5/lib/jacocoagent.jar=includes=*,output=tcpserver,append=true,port=2014,address=127.0.0.1 -jar jimkv-test.jar & 
       2)jacoco接口代码覆盖率统计

    

三、遇到的问题/注意事项

1、统计出来的覆盖绿总是0%

排查步骤:1)用jacoco命令查看覆盖目录,然后手工与源码路径比对,确定是否能对上

在测试代码所在机器上执行jacoco命令查看覆盖目录:java -jar jacococli.jar dump --address 127.0.0.1 --port 2014 --destfile jacoco.exec

posted @ 2023-08-22 17:28  飘飘雪  阅读(197)  评论(0编辑  收藏  举报