本地操作步骤
构建工程及启动服务
$ gradle clean build
$ java -javaagent:/home/rmeng/Application/jacoco/lib/jacocoagent.jar=output=tcpserver -jar ./build/libs/p2p-service.jar
NEW TERMINAL
$ java -jar /home/rmeng/Application/jacoco/lib/jacococli.jar dump --destfile ./build/jacoco.exec
$ java -jar /home/rmeng/Application/jacoco/lib/jacococli.jar report ./build/jacoco.exec --classfiles ./build/classes/java/main/ --html ./build/html --sourcefiles ./src/main/java/
TEST
$ ./update_openapi_client.sh
$ cd tests
$ rm -rf ./allurereport
$ pytest -s -q p2pService --p2p_host=http://127.0.0.1:7600 --alluredir=./allurereport
正式启动服务
$ java -javaagent:/home/rmeng/Application/jacoco/lib/jacocoagent.jar=output=tcpserver -jar ./build/libs/p2p-service.jar --spring.config.location=yitu.yml &
$ java -javaagent:/home/rmeng/Application/jacoco/lib/jacocoagent.jar=output=tcpserver -jar ./build/libs/p2p-service.jar --spring.config.location=yitu2.yml &
$ java -javaagent:/home/rmeng/Application/jacoco/lib/jacocoagent.jar=output=tcpserver -jar ./build/libs/p2p-service.jar --spring.config.location=WXEG0091.yml &
jenkins上面的配置
配置要求:
jenkins需要安装jacoco plugin(CI团队已经安装)
需要源代码和classes文件
(1)配置jacoco安装包的位置:
jacoco
parameters {
string(name: 'source_pack', defaultValue: 'testGeneric/camp101/p2p-service-source.tar.gz', description: 'source code包')
}
(2)下载及解压jacoco
jacoco
stage('prepare jacoco') {
steps {
script{
echo "start prepare jacoco stage..."
sh """
rm -rf jacoco/
mkdir jacoco
"""
echo "start download jacoco_package..."
artifactoryUtils.download( "testGeneric/camp101/p2p-service-source.tar.gz","jacoco/jacoco-0.8.4.tar.gz", "true","fp-rt-server")
echo "download success"
sh """
cd jacoco/
tar xf jacoco-0.8.4.tar.gz
"""
}
// TODO
echo "jacoco is ready"
}
}
(3)启动jar包并启动jacoco在线插桩服务
jacoco
stage('deploy') {
steps {
echo "start deploy..."
script {
sh """
...
#../p2p_package/p2p_service/shared/java/bin/java -javaagent:.../jacoco/jacoco-0.8.4/lib/jacocoagent.jar=output=tcpserver -jar ../*.jar &
"""
}
}
}
(4)测试完成后收集jacoco报告
jacoco
stage('collect jacoco report') {
steps {
script {
sh """
java -jar ../jacoco/jacoco-0.8.4/lib/jacococli.jar dump --destfile **/**.exec
"""
}
jacoco()
echo 'jacoco report has been collected.'
}
}
(5)页面数据展示
com.atlassian.confluence.content.render.xhtml.XhtmlException: Missing required attribute: {http://atlassian.com/resource/identifier}valuecom.atlassian.confluence.content.render.xhtml.XhtmlException: Missing required attribute: {http://atlassian.com/resource/identifier}value
具体覆盖率及源代码染色展示
Why does the coverage report not show highlighted source code?
Make sure the following prerequisites are fulfilled to get source code highlighting in JaCoCo coverage reports:
Class files must be compiled with debug information to contain line numbers.
Source files must be properly supplied at report generation time. I.e. specified source folders must be the direct parent of the folders that define the Java packages.
Can I collect coverage information without stopping the JVM?
Yes, there are three possible ways: The JaCoCo agent can be configured for remote control via TCP/IP sockets. This allows to collect execution data at any point in time from a running JVM. The dump Ant task, the dump Maven goal and the command line interface can be used to request dumps. The remote control feature also allows you to reset execution data.
Alternatively the JaCoCo agent can be configured to expose some functionality via JMX (jmx=true). The bean org.jacoco:type=Runtime provides operations to dump and reset execution data at any point in time.
In addition JaCoCo provides a Java API to directly access the runtime within the JVM executing the tests.