为什么我的测试被忽略了用jacoco和maven生成覆盖率报告

46qrfjad  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(668)

我有以下pom文件:

<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>com.manning.junitbook</groupId>
    <artifactId>ch13-continuous</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>ch13-continuous</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <sonar.coverage.jacoco.xmlReportPaths>target\site\jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
        <sonar.junit.reportPaths>target\surefire-reports</sonar.junit.reportPaths>

    </properties>
    <build>
    <plugins>
        <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.5</version>
        <executions>
            <execution>
                <id>report</id>
                <goals>
                    <goal>report</goal>
                </goals>
                <phase>verify</phase>
            </execution>
        </executions>
       </plugin>

        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
        </plugin>
      </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

我做了一些测试。例如,其中一个测试来自passenger类的一些函数。代码如下:

package es.ull.passengers;

import es.ull.flights.Flight;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class PassengerTest {
    Passenger Pasajero = new Passenger("1","Paco","US");
    Flight testing = new Flight("AA9020", 100);

    @Test
    public void test_get_Name(){
        System.out.println("Testing get Name...");
        assertEquals("Paco", Pasajero.getName());
    }

    @Test
    public void test_get_Identifier(){
        System.out.println("Testing get Identifier...");
        assertEquals("1", Pasajero.getIdentifier());
    }

    @Test
    public void test_get_CountryCode(){
        System.out.println("Testing get Country Code...");
        assertEquals("US", Pasajero.getCountryCode());
    }

    @Test
    public void test_Flight(){
        System.out.println("Testing get Flight...");
        Pasajero.setFlight(testing);
        assertEquals(testing, Pasajero.getFlight());
    }

    @Test
    public void test_join_flight(){
        System.out.println("Testing join Flight...");
        Flight testing2 = new Flight("AA9023", 200);
        Pasajero.joinFlight(testing2);
        assertEquals(testing2, Pasajero.getFlight());
    }

    @Test
    public void test_to_string(){
        System.out.println("Testing toString...");
        assertEquals("Passenger Paco with identifier: 1 from US", Pasajero.toString());
    }
}

当我执行“mvn verify”时,它会在target/site上生成index.xml。但是,当我检查它时,它看起来是这样的:

所以,我想知道为什么会这样。为什么不呢´不考虑我的测试??以下是控制台输出:

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------< com.manning.junitbook:ch13-continuous >----------------
[INFO] Building ch13-continuous 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ch13-continuous ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\jesus\Downloads\Airport\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ ch13-continuous ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ch13-continuous ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ ch13-continuous ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ ch13-continuous ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running FlightTest
Testing number of passengers...
Testing removing Passenger...
Testing adding Passenger...
Testing flight number...
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 s - in FlightTest
[INFO] Running PassengerTest
Testing get Flight...
Testing toString...
Testing get Country Code...
Testing get Name...
Testing get Identifier...
Testing join Flight...
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in PassengerTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 10, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ ch13-continuous ---
[INFO] 
[INFO] --- jacoco-maven-plugin:0.8.5:report (report) @ ch13-continuous ---
[INFO] Loading execution data file C:\Users\jesus\Downloads\Airport\target\jacoco.exec
[INFO] Analyzed bundle 'ch13-continuous' with 2 classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.520 s
[INFO] Finished at: 2020-12-12T13:27:22Z
[INFO] ------------------------------------------------------------------------
ohtdti5x

ohtdti5x1#

注解@test是从org.junit.jupiter.api.test包导入的吗?
如果是这样,您需要在pom文件中添加maven plugin maven surefire plugin。
下面是maven junit5插件文档
我认为junit5在执行maven测试时没有maven surefire插件是无法工作的。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M5</version>
</plugin>

相关问题