Как в jenkins получить процент покрытия junit тестами?

Пользователь

от steve , в категории: Java , год назад

Как в jenkins получить процент покрытия junit тестами?

Facebook Vk Ok Twitter LinkedIn Telegram Whatsapp

2 ответа

Пользователь

от mona_terry , год назад

@steve 

Jenkins provides several plugins that can be used to get test coverage reports, including the popular JaCoCo plugin. Here's how you can use it to get test coverage information for your JUnit tests:

  1. Install the JaCoCo plugin in Jenkins: Go to Manage Jenkins > Manage Plugins, and then search for "JaCoCo plugin" in the Available Plugins tab.
  2. Configure your build to generate the JaCoCo report: You can either configure your build to run the JaCoCo command line tool or use a build tool plugin such as the Maven plugin. If you are using Maven, add the following to your pom.xml:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.5</version>
      <executions>
        <execution>
          <id>pre-unit-test</id>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
        </execution>
        <execution>
          <id>post-unit-test</id>
          <phase>test</phase>
          <goals>
            <goal>report</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>


  1. Publish the JaCoCo report in Jenkins: Go to your build's configuration page, and then under the "Post-build Actions" section, select "Publish JaCoCo coverage report". Enter the path to the JaCoCo report (e.g. target/site/jacoco/index.html).
  2. Run your build: The JaCoCo report will be generated and published in Jenkins after the build is complete.
  3. View the JaCoCo report: You can view the test coverage report by clicking on the "JaCoCo" link in the build's dashboard. The report will show the percentage of code that is covered by your tests, as well as other information such as the number of lines, branches, and instructions that are covered.

Пользователь

от jazmyne , 4 месяца назад

@steve 

  1. Установите плагин JaCoCo в Jenkins: Перейдите в "Manage Jenkins" > "Manage Plugins", затем используйте функцию поиска для поиска плагина "JaCoCo plugin" во вкладке "Available Plugins".
  2. Настройте вашу сборку для генерации отчета JaCoCo: Вы можете настроить вашу сборку для выполнения команды JaCoCo из командной строки или использовать плагин для сборки, такой как плагин Maven. Если вы используете Maven, добавьте следующий код в ваш pom.xml:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!-- Настройка плагина JaCoCo -->
<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.5</version>
      <executions>
        <execution>
          <id>prepare-agent</id>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
        </execution>
        <execution>
          <id>report</id>
          <phase>test</phase>
          <goals>
            <goal>report</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>


  1. Опубликуйте отчет JaCoCo в Jenkins: Перейдите на страницу конфигурации вашей сборки, и затем в разделе "Post-build Actions" выберите "Publish JaCoCo coverage report". Введите путь к отчету JaCoCo (например, target/site/jacoco/index.html).
  2. Запустите вашу сборку: Отчет JaCoCo будет сгенерирован и опубликован в Jenkins после завершения сборки.
  3. Просмотрите отчет JaCoCo: Вы можете просмотреть отчет о покрытии тестами, нажав на ссылку "JaCoCo" на панели управления сборкой. Отчет покажет процент кода, покрытого вашими тестами, а также другую информацию, например, количество строк, ветвей и инструкций, покрытых тестами.