Cobertura检查没有失败的构建

问题描述 投票:1回答:1

我创建了一个基本的maven包。 src / main / java目录包含:

public class Blah {
    public int blah(){
        return 1;
    }

    public int bluh(){
        return 2;
    }
}

src / test / java目录包含:

import static org.junit.Assert.assertEquals;


import org.junit.Test;


public class BlahTest {
    @Test
    public void blahTest() {
        Blah b = new Blah();
        assertEquals(1, b.blah());
    }
}

而pom如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cob_test</groupId>
    <artifactId>cob_test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <check>
                        <haltOnFailure>true</haltOnFailure>
                        <branchRate>75</branchRate>
                        <lineRate>85</lineRate>
                        <totalBranchRate>75</totalBranchRate>
                        <totalLineRate>85</totalLineRate>
                        <packageLineRate>75</packageLineRate>
                        <packageBranchRate>85</packageBranchRate>
                    </check>
                </configuration>
            </plugin>
        </plugins>
    </reporting>

</project>

运行mvn install时,不验证检查部分中的各种参数。由于有2个功能,我预计覆盖率为50%,预期覆盖率最小值高于此值。因此,构建应该失败。此外,是否有一种方法可以在命令行上构建之后立即显示包级别覆盖率数字,而不是在html文件中显示它们。

详细拆分很有帮助,但我还希望在违反最小覆盖限制时失败。

java maven code-coverage cobertura maven-cobertura-plugin
1个回答
1
投票

你必须执行

mvn verify

运行cobertura,因为它在验证生命周期阶段默认运行。

如果您想要更改它,您可以按如下方式修改插件配置(将验证阶段更改为您喜欢的):

<project>
  <build>
     <plugins>
       <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <check>
                    <haltOnFailure>true</haltOnFailure>
                    <branchRate>75</branchRate>
                    <lineRate>85</lineRate>
                    <totalBranchRate>75</totalBranchRate>
                    <totalLineRate>85</totalLineRate>
                    <packageLineRate>75</packageLineRate>
                    <packageBranchRate>85</packageBranchRate>
                </check>
            </configuration>
            <executions>
              <execution>
                <phase>verify</phase>
                  <goals>
                    <goal>check</goal>
                  </goals>
              </execution>
            </executions>
        </plugin>
     </plugins>
  </build>
</project>
© www.soinside.com 2019 - 2024. All rights reserved.