Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

java - Extract test coverage from the docker container in GitLab-CI

I'm using GitLab-CI and running the java application that is built with help of Gradle.

There is an application running inside the Docker container. In parallel, the job automation runs the ApiTestSuite test suite along with the java application.

The question is: How can I get the test coverage from the running application after the ApiTestSuite suite ends?

Here is the simplified version of the GitLab job:

automation:
  stage: validate
  tags:
    - dind
  variables:
    PROFILE: some-profile
  services:
    - docker:stable-dind
    - redis:5-alpine
    - webcenter/activemq
  before_script:
    - docker run --detach --name MY_APPLICATION
  script:
    - ./gradlew ApiTestSuite

I can fetch the code coverage while running unit test cases but have some problems understanding how it can be implemented for the example above.

Thank you in advance :)

question from:https://stackoverflow.com/questions/65923115/extract-test-coverage-from-the-docker-container-in-gitlab-ci

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Tell java to use JaCoCo Agent while running the application inside the container

-javaagent:/home/java/jacoco.jar=destfile=folder_inside_contrainer/jacoco-it.exec,append=true

  1. Configure Agent to write the results into folder_inside_contrainer

replace *folder_inside_contrainer* with the one you will share with the host later on

  1. Share folder folder_inside_contrainer between the container and the host (in this case GitLab Job)

docker run -d -v gitlab_folder:folder_inside_contrainer

  1. Export results that are in .exec format into .html format via JaCoCo-cli.jar

java -jar jacococli.jar report gitlab_folder/jacoco-it.exec --classfiles path_to_app_compiled_classes --html coverage/api-coverage-html

  1. Grab the coverage from HTML and display it for that job
api-coverage:
  stage: release
  script:
    - 'apt-get install -y unzip'
    - 'curl -L "https://search.maven.org/remotecontent?filepath=org/jacoco/jacoco/0.8.6/jacoco-0.8.6.zip" -o jacoco.zip'
    - 'unzip jacoco'
    - 'java -jar lib/jacococli.jar report gitlab_folder/jacoco-it.exec --classfiles path_to_app_compiled_classes --html coverage/api-coverage-html'
    - grep -oP "Total.*?([0-9]{1,3})%" coverage/api-coverage-html/index.html
  coverage: "/Total.*?([0-9]{1,3})%/"
  artifacts:
    paths:
      - coverage/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...