2013-06-01 4 views
1

Я пытаюсь настроить покрытие кода на моем локальном сервере Jenkins с Maven. Казалось бы, мои тесты не включаются в отчеты о покрытии.Недостаточное покрытие кода Кобертуры

У меня есть следующие в моем pom.xml:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
     <artifactId>sonar-maven-plugin</artifactId> 
     <version>2.0</version> 
</plugin> 
<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
     <artifactId>cobertura-maven-plugin</artifactId> 
     <version>2.5.2</version> 
     <configuration> 
     <check> 
     <haltOnFailure>false</haltOnFailure> 
      <totalLineRate>85</totalLineRate> 
     </check> 
     <formats> 
      <format>xml</format> 
      <format>html</format> 
     </formats> 
     </configuration> 
     <executions> 
      <execution> 
       <id>clean</id> 
       <phase>pre-site</phase> 
       <goals> 
        <goal>clean</goal> 
       </goals> 
      </execution> 
      <execution> 
       <id>instrument</id> 
       <phase>site</phase> 
       <goals> 
        <goal>instrument</goal> 
        <goal>cobertura</goal> 
       </goals> 
      </execution> 
     </executions> 
</plugin> 

и вот где я определяю тесты для запуска

<plugin> 
    <!-- Separates the unit tests from the integration tests. --> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <configuration> 
     <skip>true</skip> 
     <trimStackTrace>false</trimStackTrace> 
     </configuration> 
    <executions> 
     <execution> 
      <id>unit-tests</id> 
      <phase>test</phase> 
      <goals> 
       <goal>test</goal> 
      </goals> 
      <configuration> 
       <skip>false</skip> 
       <includes> 
        <include>**/*Test*.java</include> 
       </includes> 
       <excludes> 
        <exclude>**/*IT.java</exclude> 
       </excludes> 
      </configuration> 
     </execution> 

     <execution> 
      <id>integration-tests</id> 
      <phase>integration-test</phase> 
      <goals> 
       <goal>test</goal> 
      </goals> 
      <configuration> 
       <skip>false</skip> 
       <includes> 
        <include>**/*IT.java</include> 
       </includes> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

И на моем сервере Дженкинс я иметь следующее: Дженкинс Конфигурация: название Sonar Сонар: Сонар локальный URL 3.5.1 сервер: http://local host:9000

Уровень проекта: публиковать Cobertura Coverate Доклад: XML шаблон отчета: **/цель/site.covertura/coverage.xml

Но когда я бегу либо задачу Maven "МВН Cobertura: Cobertura" или просмотреть сонар отчеты в Дженкинсе, весь охват составляет 0%, что заставляет меня думать, что мой модуль и интеграционные тесты не видны.

Это проблема с тем, как мой pom отделяет модульные и интеграционные тесты?

ответ

1

cobertura не может изучить интеграционный тест, чтобы увидеть покрытие кода.

Попробуйте взглянуть на code coverage by integration tests with sonar и на separating integration and unit tests

Чтобы сделать короткий рассказ: вы должны запустить jacoco Maven плагин, который будет установить два свойства, одна для модульных тестов, другой для него испытаний и запуска безошибочный и отказоустойчиво с этими свойствами.

И после этого вы можете показать результат в гидролокаторе в отдельном апплете.

В моем собственном файле пом, вы будете себе часть для включенной jacoco: «**/** мишень»

  <properties> 
       <!-- Coverage and report --> 
       <jacoco.destFile.unit>${project.build.directory}/jacoco-unit.target</jacoco.destFile.unit> 
       <jacoco.destFile.it>${project.build.directory}/jacoco-it.target</jacoco.destFile.it> 
       <coverage.reports.dir>${project.build.directory}/coverage-reports</coverage.reports.dir> 
       <version.surefire>2.12.4</version.surefire> 

       <!-- Sonar --> 
       <sonar.jacoco.itReportPath>${jacoco.destFile.it}</sonar.jacoco.itReportPath> 
       <sonar.jacoco.reportPath>${jacoco.destFile.unit}</sonar.jacoco.reportPath> 
       <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin> 
       <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis> 
      </properties> 


      <build> 
      .... 
      <plugins> 
       <!-- Jacoco configuration --> 
       <plugin> 
        <groupId>org.jacoco</groupId> 
        <artifactId>jacoco-maven-plugin</artifactId> 
        <version>0.6.0.201210061924</version> 
        <executions> 
         <execution> 
          <goals> 
           <goal>prepare-agent</goal> 
          </goals> 
          <configuration> 
           <propertyName>jacoco.argLine.unit</propertyName> 
           <destFile>${jacoco.destFile.unit}</destFile> 
          </configuration> 
         </execution> 
         <execution> 
          <id>pre-integration-test</id> 
          <phase>pre-integration-test</phase> 
          <goals> 
           <goal>prepare-agent</goal> 
          </goals> 
          <configuration> 
           <propertyName>jacoco.argLine.it</propertyName> 
           <destFile>${jacoco.destFile.it}</destFile> 
          </configuration> 
         </execution> 
        </executions> 
       </plugin> 
       <!-- And in the surefire and failsafe plugins you need to enable jacoco like this -> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-surefire-plugin</artifactId> 
        <version>${version.surefire}</version> 
        <dependencies> 
         <dependency> 
          <groupId>org.apache.maven.surefire</groupId> 
          <artifactId>surefire-junit47</artifactId> 
          <version>${version.surefire}</version> 
         </dependency> 
        </dependencies> 
        <configuration> 
         <argLine>${jacoco.argLine.unit} 
          -Dfile.encoding=${project.build.sourceEncoding} -Xmx512m</argLine> 
         <forkMode>always</forkMode> 
         <parallel>classes</parallel> 
        </configuration> 
       </plugin--> 

       <!-- Play the /src/test/java/**IT.java with goal verify --> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-failsafe-plugin</artifactId> 
        <version>${version.surefire}</version> 
        <dependencies> 
         <dependency> 
          <groupId>org.apache.maven.surefire</groupId> 
          <artifactId>surefire-junit47</artifactId> 
          <version>${version.surefire}</version> 
         </dependency> 
        </dependencies> 
        <configuration> 
         <forkMode>pertest</forkMode> 
         <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory> 
         <argLine>${jacoco.argLine.it} 
          -Dfile.encoding=${project.build.sourceEncoding} -Xmx512m</argLine> 
        </configuration> 
        <executions> 
         <execution> 
          <id>integration-test</id> 
          <phase>integration-test</phase> 
          <goals> 
           <goal>integration-test</goal> 
          </goals> 
         </execution> 
         <execution> 
          <id>verify</id> 
          <phase>verify</phase> 
          <goals> 
           <goal>verify</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 

В Дженкинс, вы должны настроить «postbuild» JaCoCo плагинов с для путь к файлам exec

Надеюсь, это поможет вам.

+0

ОК, я заменил использование cobertura с помощью JaCoco (сделав его похожим на образец pom.xml) и попробовал работать с «mvn clean package», но теперь все тесты запускаются, но для Jenkins не создаются отчеты .exec для отображения. – sonoerin

+0

Вы видите отчет в гидролокаторе? ваш плагин jacoco настроен на «\ * \ */\ * \ *. dump» (для путей к файлам exec?) – twillouer

+0

Не уверен, что я сделал, но теперь он работает. Если я посмотрю на Sonar, а не на Jenkins, я могу увидеть количество тестов (unit & integration), которые запускались с охватом кода. Большое вам спасибо за вашу помощь. Вы публиковали больше, чем много учебников, которые я пытался работать! – sonoerin

Смежные вопросы