Maven笔记11-Maven进行测试

1. maven-surefire-plugin

    该插件的test目标会自动执行测试源码路径,默认为src/test/java下负荷下列命名模式的测试类:

    (1) **/Test*.java:任何目录下所有命名以Test开头的Java类;

    (2) **/*Test.java:任何子目录下所有命名以Test结尾的Java类;

    (3) **/*TestCase.java:热河子目录下所有以TestCase结尾的Java类。

2. 跳过测试

    (1) 命令行

    mvn pacage -DskipTests

    (2) XML配置

    <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-surfire-plugin</artifactId>

        <version>2.5</version>

        <confuguration>

            <skipTests>true</shipTests>

        </configuration>

    </plugin>

3. 跳过测试代码的编译和测试

    (1) 命令行:mvn package -Dmaven.test.skip=true

    (2) XML配置

    <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-compile-plugin</artifactId>

        <version>2.1</version>

        <confuguration>

            <skip>true</ship>

        </configuration>

    </plugin>

    <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-surfire-plugin</artifactId>

        <version>2.5</version>

        <confuguration>

            <skip>true</ship>

        </configuration>

    </plugin>

4. 动态指定要运行的测试类

    mvn test -Dtest=RandomTest

    mvn test -Dtest=Rando*Test

    mvn test -Dtest=Rando*Test, User*Test

    若没有任何测试类,可加上下面的参数概述Maven即使没有测试类也不要报错:

    mvn -test -Dtest -DfailIfNoTests=false

5. 包含于排除测试用例

    (1) 自动运行以Tests结尾的测试类

    <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-surfire-plugin</artifactId>

        <version>2.5</version>

        <confuguration>

            <includes>

                <include>**/*Tests.java</include>

            </includes>

        </configuration>

    </plugin>

 

    (2) 排除运行测试类

    <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-surfire-plugin</artifactId>

        <version>2.5</version>

        <confuguration>

            <excludes>

                <exclude>**/ServiceTest1.java</include>

                <exclude>**/ServiceTest2.java</include>

            </excludes>

        </configuration>

    </plugin>

6. 测试覆盖率报告

    使用下面命令生成简单的报告:

    mvn cobertura:cobertura

    生成的报告位于工程的target/site/cobura/index.html

7. 重用测试代码

    maven-jar-plugin有两个目标:jar和test-jar,其中jar目标内置绑定在default生命周期的package阶段执行,

    对项目主代码进行打包,而test-jar用来打包测试代码,配置为:

    <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-jar-plugin</artifactId>

        <version>2.2</version>

        <executions>

            <execution>

                <goals>test-jar</ goals >

            </ execution >

        </executions>

    </plugin>

    依赖测试包构件:

    <dependency>

        <groupId>org.dmwdp.web</groupId>

        <artifactId>web-test</artifactId>

        <version>1.0.0</version>

        <type>test-jar</type>

        <scope>test</scope>

    </dependency>

你可能感兴趣的:(maven)