zoukankan      html  css  js  c++  java
  • Maven进行测试Test

    地址:https://blog.csdn.net/yonggang7/article/details/79780487  

    Maven测试
    maven测试为 default 生命周期中的test阶段。
    test阶段与 maven-surefire-plugin 的test目标相绑定了, 这是一个内置的绑定。
    Maven通过插件来执行 JUnit 和 TestNG 的测试用例。

    maven-surefire-plugin 的test目标会自动执行测试源码路径下符合命名模式的测试类。
    默认测试源代码路径: src/test/java/
    测试类命名模式:
    **/Test*.java
    **/*Test.java
    **/*TestCase.java
    按上述模式命名的类, 使用 mvn test 命令就能自动运行他们。

    跳过测试
    下面命名跳过测试:
    mvn install -DskipTests
    也可以在POM文件中配置 maven-surefire-plugin

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
    <skipTests>true</skipTests>
    <!--
    skip 对应命令行参数为 maven.test.skip
    -->
    <skip>false</skip>
    </configuration>
    </plugin>
     
    不推荐这种做法,会让项目长期跳过测试。

    如果想不仅跳过测试运行,还跳过测试代码的编译,使用下面命令:
    mvn package -Dmaven.test.skip=true
    maven.test.skip 控制了 maven-compiler-plugin 和 maven-surefire-plugin 两个插件的行为。

    Maven动态指定测试用例
    maven-surefire-plugin 使用 test 参数指定测试用例, 为测试用例的类名
    mvn test -Dtest=RandomTest
    只执行 RandomTest 这个测试类.
    mvn test -Dtest=RandomTest#myTest
    上面命令,只运行 RandomTest 类的 myTest 方法

    可以指定多个类,逗号分隔
    mvn test -Dtest=RandomTest,Random2Test
    也可以用 * 匹配多个
    mvn test -Dtest=Random*Test
    *和 逗号可以结合使用。

    如果不指定或者找不到测试类则构建失败
    mvn test -Dtest
    failIfNoTests 参数控制没有测试用例不报错
    mvn test -Dtest -DfailIfNoTests=false

    包含测试用例
    将不符合命名模式测试类自动运行测试。
    修改POM文件

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
    <includes>
    <include>**/*Tests.java</include>
    </includes>
    </configuration>
    </plugin>
     
    两个星号 ** 表示匹配任意路径。
    上面表示匹配已 Tests.java 结尾的Java类。

    排除测试用例
    排除测试用例不实用test自动运行
    使用 excludes 节点

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
    <excludes>
    <exclude>**/*ServiceTest.java</exclude>
    </excludes>
    </configuration>
    </plugin>
     
    生成测试报告
    测试报告默认生成 target/surefire-reports 目录下,生成 txt, 和 xml 格式的。

    测试代码重用
    mvn package 会打包项目主代码和资源文件代码,没有包含测试代码。
    如果想一起打包测试用例,供依赖方使用, 使用 maven-jar-plugin 插件

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <executions>
    <execution>
    <goals>
    <goal>test-jar</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
     
    maven-jar-plugin 有两个目标 jar ,test-jar,
    jar 内置绑定在 default 生命周期的 package 阶段。
    test-jar没有内置绑定。

    依赖方引入时 dependency

    <dependency>
    <groupId>org.A</groupId>
    <artifactId>A</artifactId>
    <version>5.0.0</version>
    <type>test-jar</type>
    <scope>test</scope>
    </dependency>
     
    需要设置 type 和 scope。

  • 相关阅读:
    用java简单的实现单链表的基本操作
    Pointcut is not well-formed: expecting 'identifier' at character position 0
    Spark 学习(二)
    Spark 学习
    学习Mahout (四)
    github 入门
    Source Insight 入门设置
    shell chpasswd 命令 修改用户密码
    sed 匹配 换行符
    学习Mahout(三)
  • 原文地址:https://www.cnblogs.com/kelelipeng/p/13230052.html
Copyright © 2011-2022 走看看