Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Jacoco to calculate server code coverage

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to use Jacoco statistics server code coverage, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

1. Preface

Use Python + Coverage to count the code coverage of test cases

Jacoco is an open source coverage tool for Java, which can be embedded in Maven and Gradle, providing coverage counters of various scales, such as class coverage, line coverage, branch coverage, etc.

This article will talk about the server-side code coverage statistics, taking the Spring Boot project as an example, using Jacoco + junit to calculate the server-side code coverage

two。 Prepare for

First create a Spring Boot project (Maven) using IDEA, based on the project code that previously built RESTFul API

Then, configure the pom.xml file to add the jacoco dependency JAR package for the current project

Org.jacoco

Jacoco-maven-plugin

0.8.5

For the jacoco version, please refer to:

Https://www.eclemma.org/jacoco/index.html

Next, configure the Jacoco plug-in and the associated goal, and use the includes and excludes keywords to set the included or excluded classpath

For example, only the classes under com.xingag.api.service are counted here.

Org.jacoco

Jacoco-maven-plugin

0.8.5

Com/xingag/api/service/*

Prepare-agent

Prepare-agent

Check

Check

Report

Prepare-package

Report

Finally, click Maven synchronization in the upper right corner to download the dependency and configure the project

3. First of all, write a simple code to be tested and return different results / / ScoreServiceImpl.java according to the test results.

Package com.xingag.api.service

/ / Code under test

Public class ScoreServiceImpl {

Public String getScoreLevel (int score) {

String result

If (score > 90) {

Result = "excellent"

} else if (score

< 90 && score >

= 75) {

Result = "good"

} else if (score > = 60) {

Result = "qualified"

} else if (score > 40) {

Result = "unqualified"

} else if (score > = 0) {

Result = "poor"

} else {

Result = "incorrect grade format"

}

Return result

}

}

Then, in the test test folder, write the test class and unit test methods @ RunWith (SpringRunner.class) and @ SpringBootTest to annotate the test class, indicating that the current class handles / / ScoreTests as a test class

/ / Test class

@ RunWith (SpringRunner.class)

@ SpringBootTest

Public class ScoreTests {

...

}

Next, define three unit test methods / / ScoreTests.java in the test class with the @ Test annotation of Junit

/ / define the test method

/ / excellent results

@ Test

Public void testLevelA () {

Assert.assertEquals (RESULT_LEVEL [0], scoreService.getScoreLevel (95))

}

/ / good grades

@ Test

Public void testLevelB () {

Assert.assertEquals (RESULT_LEVEL [1], scoreService.getScoreLevel (80))

}

/ / pass the grade

@ Test

Public void testLevelC () {

Assert.assertEquals (RESULT_LEVEL [2], scoreService.getScoreLevel (70))

} finally, in Terminal, point to the project root directory and enter the mvn test jacoco:report command to generate code coverage report code coverage report directory in:. / target/site/jacoco

By opening the index.html file in the statistical report folder with a browser, you can directly view the unit test coverage and the specific coverage of the corresponding test classes.

After reading the above, have you mastered how to use Jacoco to calculate server code coverage? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report