gitlab项目需要统计code coverage ,一个同事半年多了没有实质的进展,后面说不知道怎么弄(此处省略几千字)。网络这么发达,这个问题又是很普遍的事情,怎么可能没解决方案呢?
选择jacoco代码覆盖率统计,可能两种比较普遍的:
这里的以spring boot的gs-spring-boot项目 complete为例。
添加一个service文件夹,创建类:
public class MessageBuilder {
public String getMessage(String name) {
StringBuilder result = new StringBuilder();
if (name == null || name.trim().length() == 0) {
result.append("Please provide a name!");
} else {
result.append("Hello " + name);
}
return result.toString();
}
}
同时创建一个测试类:
class MessageBuilderTest {
@Test
void getMessageNull() {
MessageBuilder obj = new MessageBuilder();
assertEquals("Please provide a name!",obj.getMessage(null));
}
@Test
public void testNameMkyong() {
MessageBuilder obj = new MessageBuilder();
assertEquals("Hello kielin", obj.getMessage("kielin"));
}
}
pom文件中加入
>
>
>
>org.springframework.boot >
>spring-boot-maven-plugin >
>
>
>org.jacoco >
>jacoco-maven-plugin >
>0.8.1 >
>
>target/coverage-reports/jacoco-unit.exec >
>target/coverage-reports/jacoco-unit.exec >
>
>**/service/** >
>**/controller/** >
>**/*Tests.java >
>**/*Test.java >
<!--**/service/impl/*.class -->
BUNDLE
METHOD
COVEREDRATIO
0.50
BRANCH
COVEREDRATIO
0.50
CLASS
MISSEDCOUNT
0
>
>
>
>jacoco-initialize >
>
>prepare-agent >
>
>
<!--这个check:对代码进行检测,控制项目构建成功还是失败-->
jacoco-site
test
report
>
>
>
>
好了,项目的内容就这么多。
接下来要让项目本身能获取到覆盖率的数据,需求对项目做一些设置。
image: maven:latest
variables:
MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
cache:
paths:
- .m2/repository/
build:
stage: build
script:
- mvn $MAVEN_CLI_OPTS compile
test:
stage: test
script:
- mvn $MAVEN_CLI_OPTS test
- mvn clean test && cat target/site/jacoco/index.html
artifacts:
paths:
- target/site/jacoco/
pages:
stage: deploy
dependencies:
- test
script:
- mkdir public
- mv target/site/jacoco/index.html public
artifacts:
paths:
- public
deploy:
stage: deploy
script:
- mvn $MAVEN_CLI_OPTS verify
only:
- master
上传.gitlab-ci.yml文件以后,该项目的pipeline应该是可用的。否则的话会显示让先enable pipeline。 有了pipeline后,执行里面的job需要设置runner,如果没有runner, job的状态会一直是stuck. 对于runner,分三个步骤:
1. MAC OS 安装 runner: https://docs.gitlab.com/runner/install/osx.html
2. Mac OS 注册 runner: https://docs.gitlab.com/runner/register/
3. 指定哪种类型的executor,这里是shell: https://docs.gitlab.com/runner/executors/shell.html
以上步骤第1步按文档安装。
第2,3步注册稍微补充一下,当安装完成runner后。 可以通过gitlab-runner status 查看runner运行状态。当看到已运行的时候,在http://git.yourdomain.com/user/complete/settings/ci_cd的runner中拷贝以下要填写的信息: url && token,
在Terminal里执行 gitlab-runner register来注册:
[MacBook-Pro.local]: gitlab-runner register
Runtime platform arch=amd64 os=darwin pid=20975 revision=ece86343 version=13.5.0
WARNING: Running in user-mode.
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://git.yourdomain.com/
Please enter the gitlab-ci token for this runner:
hdpeA85H1******udaY94
Please enter the gitlab-ci description for this runner:
[MacBook-Pro.local]: covtestmax
Please enter the gitlab-ci tags for this runner (comma separated):
covtest;
Registering runner... succeeded runner=hdpeA85H
Please enter the executor: virtualbox, docker+machine, docker-ssh+machine, docker, docker-ssh, parallels, shell, ssh, custom, kubernetes:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
当运行完成后可以在settings–>ci/cd中的runner中看到刚刚注册的这个runner.
当设置完成特定的runner后,pipeline就可以正常运行了。 在project/ci/cd下看到所有job完成状况,像下图这样的表示已经全部正常完成。
那么如何在些项目中展示这次的结果呢?通过文档我们知道有三种方式,选择其中一种方式,在项目的readme.md文件中展示:
[![coverage report](http://git.yourdomain.com/user/complete/badges/master/coverage.svg)](http://git.yourdomain.com/user/complete/commits/master)
我所遇到的几个问题:
>
>nexus-name >
>* >
>http://maven.yourdomain.com/nexus/content/groups/public >
>
到此,我们用了三个步骤实现code coverage收集展现:
对于技术问题,google能解决90%以上的问题,但是这个问题能不能解决取决于你想不想解决。 工作的态度就是你生活的态度,态度有时会决定结果。我们做事情最好是能积极地、认真的对待,共勉