Maven中测试插件(surefire)的相关配置及常用方法

2023-11-13

原创文章,版权所有,允许转载,标明出处:http://blog.csdn.net/wanghantong

1. 在Maven中配置测试插件surefire

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <plugin>  
  2.   <groupId>org.apache.maven.plugins</groupId>  
  3.   <artifactId>maven-surefire-plugin</artifactId>  
  4.   <version>2.17</version>  
  5. </plugin>  

 

2. 默认被执行的测试
   
   默认情况下,surefire会执行文件名以Test开头或结尾的测试用例,或者是以TestCase结尾的测试用例。
[html] view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. "**/Test*.java" - includes all of its subdirectories and all java filenames that start with "Test".  
  2. "**/*Test.java" - includes all of its subdirectories and all java filenames that end with "Test".  
  3. "**/*TestCase.java" - includes all of its subdirectories and all java filenames that end with "TestCase".</span>  
3. 跳过测试   Skipping Tests
[html] view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <build>  
  2.     <plugins>  
  3.       <plugin>  
  4.         <groupId>org.apache.maven.plugins</groupId>  
  5.         <artifactId>maven-surefire-plugin</artifactId>  
  6.         <version>2.17</version>  
  7.        <span style="color:#009900;"> <configuration>  
  8.           <skipTests>true</skipTests>  
  9.         </configuration></span>  
  10.       </plugin>  
  11.     </plugins>  
  12.   </build>  
  13.   
  14. 补充:如果使用Junit或者TestNG也可以直接在当前测试方法上加注解@Ignore忽略,这是加了该注解的也都会被skip  
4. 排除测试 Exclusions  (Junit & TestNG 通用)
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <build>  
  2.     <plugins>  
  3.       <plugin>  
  4.         <groupId>org.apache.maven.plugins</groupId>  
  5.         <artifactId>maven-surefire-plugin</artifactId>  
  6.         <version>2.17</version>  
  7.         <configuration>  
  8.          <span style="color:#009900;"> <excludes>  
  9.             <exclude>**/TestCircle.java</exclude>  
  10.             <exclude>**/TestSquare.java</exclude>  
  11.           </excludes></span>  
  12.         </configuration>  
  13.       </plugin>  
  14.     </plugins>  
  15.   </build>  

5. 仅执行一个/一类测试(repeat)   Running a Single Test   (Junit & TestNG 通用)

 

 

在开发过程中配置命令:
mvn -Dtest=TestCircle test   test表示当前测试方法所在的测试类,不需要扩展名 The value for the test parameter is the name of the test class
mvn -Dtest=TestCi*le test    *表示任何
mvn -Dtest=TestSquare,TestCi*le test    如果测试类没有使用规范的命名,可以显示的直接指定测试方法的名称

 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. Running a set of methods in a Single Test Class  
  2.   
  3. With version 2.7.3, you can run only n tests in a single Test Class.  
  4.  NOTE : it's supported for junit 4.x and TestNG.   
  5. You must use the following syntax  
  6. mvn -Dtest=TestCircle#mytest test  
  7. You can use patterns too  
  8. mvn -Dtest=TestCircle#test* test  
  9. As of surefire 2.12.1, you can select multiple methods (JUnit4X only at this time, patches welcome)  
  10. mvn -Dtest=TestCircle#testOne+testTwo test  


6. 如何使用TestNG

 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <dependency>  
  2.       <groupId>org.testng</groupId>  
  3.       <artifactId>testng</artifactId>  
  4.       <version>6.3.1</version>  
  5.       <scope>test</scope>  
  6.     </dependency>  
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. If  using an older version of TestNG (<= 5.11)  
  2.     <dependency>  
  3.       <groupId>org.testng</groupId>  
  4.       <artifactId>testng</artifactId>  
  5.       <version>5.11</version>  
  6.       <scope>test</scope>  
  7.      <span style="color:#009900;"> <classifier>jdk15</classifier></span>  
  8.     </dependency>  

 

默认会执行的测试用例*Test.java

7. 如何使用TestNG的 suite

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <plugin>  
  2.         <groupId>org.apache.maven.plugins</groupId>  
  3.         <artifactId>maven-surefire-plugin</artifactId>  
  4.         <version>2.17</version>  
  5.         <configuration>  
  6.           <span style="color:#009900;"><suiteXmlFiles>  
  7.                 <suiteXmlFile>testng.xml</suiteXmlFile>  
  8.             </suiteXmlFiles></span>  
  9.         </configuration>  
  10.       </plugin>  

注意:This configuration will override the includes and excludes patterns and run all tests in the suite files.

原创文章,版权所有,允许转载,标明出处:http://blog.csdn.net/wanghantong

8.  执行群组测试 execute one or more specific groups  (Junit & TestNG 通用)

 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <plugin>  
  2.        <groupId>org.apache.maven.plugins</groupId>  
  3.        <artifactId>maven-surefire-plugin</artifactId>  
  4.        <version>2.17</version>  
  5.        <configuration>  
  6.         <span style="color:#009900;"> <groups>functest,perftest</groups></span>  
  7.        </configuration>  
  8.      </plugin>  


9. 多线程的运行测试用例 Running tests in parallel (Junit & TestNG 通用)

 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <plugin>  
  2.         <groupId>org.apache.maven.plugins</groupId>  
  3.         <artifactId>maven-surefire-plugin</artifactId>  
  4.         <version>2.17</version>  
  5.         <configuration>  
  6.           <span style="color:#009900;"><parallel>methods</parallel>  
  7.           <threadCount>10</threadCount></span>  
  8.         </configuration>  
  9.       </plugin>  


10. 如何使用Junit  Using JUnit

 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <dependency>  
  2.       <groupId>junit</groupId>  
  3.       <artifactId>junit</artifactId>  
  4.       <version>4.8.1</version>  
  5.       <scope>test</scope>  
  6.     </dependency>  

11. 如何使用Junit Category    Using JUnit Categories

 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <plugin>  
  2.       <artifactId>maven-surefire-plugin</artifactId>  
  3.       <version>2.11</version>  
  4.       <configuration>  
  5.        <span style="color:#009900;"> <strong><groups>com.mycompany.SlowTests</groups></strong></span>  
  6.       </configuration>  
  7.     </plugin>  

 

 

仅有带该注解的测试 或者 是当前注解 类/接口的 子类 会被执行,
This will execute only those tests annotated with the @Category(com.mycompany.SlowTests.class) annotation and 
those tests annotated with @Category(com.mycompany.SlowerTests.class) if class/interface SlowerTests is subclass of SlowTests:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. public interface SlowTests{}  
  2. public interface SlowerTests extends SlowTests{}  
  3. ------------------------------------------------------------------------------------------------  
  4. ic class AppTest {  
  5.   @Test  
  6.   @Category(com.mycompany.SlowTests.class)  
  7.   public void testSlow() {  
  8.     System.out.println("slow");  
  9.   }  
  10.   
  11.   @Test  
  12.   @Category(com.mycompany.SlowerTests.class)  
  13.   public void testSlower() {  
  14.     System.out.println("slower");  
  15.   }  
  16.   
  17.   @Test  
  18.   @Category(com.cmycompany.FastTests.class)  
  19.   public void testSlow() {  
  20.     System.out.println("fast");  
  21.   }  
  22. }  

参考:

Junit的@Category详解

 

 

12. 如何debug TestCases

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. mvnDebug -DforkCount=0 test   dubug非fork的测试  
  2. 在debug的需求时,还是使用Eclipse最方便  


13. 使用系统属性    Using System Properties

 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <build>  
  2.     <plugins>  
  3.       <plugin>  
  4.         <groupId>org.apache.maven.plugins</groupId>  
  5.         <artifactId>maven-surefire-plugin</artifactId>  
  6.         <version>2.17</version>  
  7.         <configuration>  
  8.           <span style="color:#009900;"><systemPropertyVariables>  
  9.             <propertyName>propertyValue</propertyName>  
  10.             <buildDirectory>${project.build.directory}</buildDirectory>  
  11.             [...]  
  12.           </systemPropertyVariables></span>  
  13.         </configuration>  
  14.       </plugin>  
  15.     </plugins>  
  16.   </build>  

 

 

14. 选择surefire provider
surefire 默认会根据工程的classpath中已有的Junit|TestNG的版本来选择 test-framework provider,我们也可以手动的选择和覆盖当前的provider
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-surefire-plugin</artifactId>  
  4.     <version>2.17</version>  
  5.     <dependencies>  
  6.       <dependency>  
  7.         <groupId>org.apache.maven.surefire</groupId>  
  8.        <span style="color:#009900;"> <artifactId>surefire-junit47</artifactId></span>  
  9.         <version>2.17</version>  
  10.       </dependency>  
  11.     </dependencies>  
  12.   </plugin>  
目前已经提供的provider有surefire-junit3, surefire-junit4, surefire-junit47 and surefire-testng
参考:http://mvnrepository.com/artifact/org.apache.maven.surefire
注意:选择手动指定provider时,不要忘记安装test framework
15. class Loading issues
please refer to:
http://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.html



 

原创文章,版权所有,允许转载,标明出处:http://blog.csdn.net/wanghantong

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Maven中测试插件(surefire)的相关配置及常用方法 的相关文章

随机推荐

  • Android开发:最全面、最易懂的Android屏幕适配解决方案

    前言 Android的屏幕适配一直以来都在折磨着我们Android开发者 本文将结合 Google的官方权威适配文档 郭霖 Android官方提供的支持不同屏幕大小的全部方法 Stormzhang Android 屏幕适配 鸿洋 Andro
  • Wireshark抓包分析交换机工作原理

    实验名称 交换机工作原理 实验目的 1 熟悉Linux虚拟网络环境 2 熟悉Linux中network namespace的基本操作 3 熟悉Linux中虚拟以太网设备Tap和veth pair的基本操作 4 熟悉Linux中Bridge设
  • 【Pytorch】循环神经网络实现手写体识别

    Pytorch 循环神经网络实现手写体识别 1 数据集加载 2 搭建RNN模型 3 训练模型 4 模型保存和加载 模型测试 1 数据集加载 import seaborn as sns sns set font scale 1 5 style
  • 如何在智星云平台发挥GPU的最大性能

    在租用智星云服务器后 如果发现训练速度很慢 可以参考下面的方式进行调优 查看gpu利用率 命令 watch nvidia smi 如果发现 nvidia smi显示gpu利用率很低 可以尝试以下操作 a 如果显存还有非常多空余 尝试增大ba
  • Neural Natural Language Processing for Long Texts: A Survey of the State-of-the-Art

    本文是针对NLP处理长文本的一个综述 针对 Neural Natural Languag Processing for Long Texts A Survey of the State of the Art 的翻译 长文本的神经自然语言处理
  • 【转载】[特征向量在线提取工具Pse-in-One 2.0

    1 Pse In One 简单介绍 机器学习分类算法方法主要依赖于根据蛋白质的结构以及功能特性构建的特征集合 通过构造具有辨别性的特征集合来达到令人满意的分类结果 但是使用一个能够反映序列模式信息并能够保持关键序列信息的离散模型或向量是一个
  • HTTPS、UDP/TCP、三次握手、四次挥手

    HTTPS协议 其实就是加密后的HTTP协议 自定制协议 HTTP协议 https到底是如何进行加密传输的 通过ssl加密实现 非对称加密算法 对称加密算法 签名证书 数据直接在网络中传输 很容易被劫持 有很大的安全隐患 对传输过程进行加密
  • HLA仿真中的多联邦

    经常有人问我HLA仿真中的多联邦问题 本讲对此进行解答 1 HLA标准中的联邦指单个联邦 在HLA标准中 联邦定义如下 简单地说 一个联邦用于完成一定的仿真功能 包含一组仿真成员和一个FOM文件 在HLA1 3中 FOM文件的后缀为 fed
  • 安装Vmware workstations 16 Microsoft Visual C++ 2019 x86 Minimum Runtime报错解决办法

    由于电脑更新到了win11系统 之前的Vmware不在被系统支持 需要重新下载新版本的Vmware 在安装Vmware workstations 16时 系统报错界面如下 我们点击OK 会弹出如下提示框 根据这个提示框 可以判断出是电脑中的
  • oracle 11g rac手册(第2版) 高清,Oracle Database11g RAC手册(第2版)_IT教程网

    资源名称 Oracle Database11g RAC手册 第2版 内容简介 根据Oracle ACE提供的专家指导来管理动态的企业级计算基础设施 戈帕拉克里希南所著的 Oracle Database 11g RAC手册 第2版 进行了全面
  • web前端面试题(全)

    近来看到网上格式各样的web前端求职的面试题 接下来我用我的经验总结了一套在面试过程中高频率问到的面试题 希望能帮助各位求职者在求职的过程中顺利通过 废话不多说 直接说题 一 HTML5部分 1 说一下对css盒模型的理解 答 css盒子模
  • 【总结一】现代密码学

    目录 1 密码学概述 1 1 密码学的基本概念 1 1 1 为什么要学密码学 1 1 2 什么是密码学 1 1 2 密码算法的基本模型 1 1 3 密码算法的分类 1 2 密码分析学 1 3 古典密码算法 1 3 1 置换密码 1 3 2
  • 对表的复杂查询

    1 连接查询 数据库中的各个表中存放着不同的数据 用户往往需要用多个表中的数据来组合 提炼出所需要的信息 如果一个查询需要对多个表进行操作 就称为连接查询 例 对student sno clno sname ssex sage course
  • Windows上安装Hadoop 3.x

    目录 0 安装Java 1 安装Hadoop 1 1 下载Hadoop 1 2 下载winutils 2 配置Hadoop 1 hadoop env cmd 2 创建数据目录 3 core site xml 4 hdfs site xml
  • 解决textarea文字不顶头显示/点击textarea 不是第一行

    问题描述 表单提交后发现内容前多了很多空格 而且每次更新表单提交都会有空格增加 后来发现 每次文字从数据库读到textarea后文字都不居左 在排出样式 转义字符等问题后 baidu google了一会始终没找到答案 后来发现原来问题处在H
  • 网络--正向代理和反向代理

    正向代理的概念 正向代理 也就是传说中的代理 他的工作原理就像一个跳板 简单的说 我是一个用户 我访问不了某网站 但是我能访问一个代理服务器 这个代理服务器呢 他能访问那个我不能访问的网站 于是我先连上代理服务器 告诉他我需要那个无法访问网
  • 如何将VS Code扩展插件迁移出系统盘

    背景 Windows的C盘 系统盘 容量经常不够用 经过排查发现VSCode的扩展插件所在目录占用了很大空间 为了节省系统盘的空间 需要将VSCode扩展插件迁移到D盘 环境 Windows VS Code 全称是Visual Studio
  • MySQL的JSON数据类型介绍以及JSON的解析查询

    文章目录 概述 JSON 数据类型的意义 JSON相关函数 测试 创建测试表 插入数据 查询数据 条件查询 优化JSON查询 解决方案 总结 概述 MySQL从5 7后引入了json数据类型以及json函数 可以有效的访问json格式的数据
  • iOS音视频—FFmepg:iOS平台下集成和应用

    1 在iOS平台下集成和应用FFmpeg Mac配置FFmpeg环境 1 安装homebrew ruby e curl fsSL https raw githubusercontent com Homebrew install master
  • Maven中测试插件(surefire)的相关配置及常用方法

    原创文章 版权所有 允许转载 标明出处 http blog csdn net wanghantong 1 在Maven中配置测试插件surefire html view plain copy