# maven4test **Repository Path**: Protector_hui/maven4test ## Basic Information - **Project Name**: maven4test - **Description**: 通过maven配置变量,以达到用不同的命令执行单元测试/集成测试 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-11-13 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README @[toc] ## 前提 项目中一般会包含两种测试:单元测试 + 集成测试。为了方便管理,我个人偏向于将这两种测试放在不同的文件夹下。 ## 项目结构 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201113155033910.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTkxNTMxNA==,size_16,color_FFFFFF,t_70#pic_center) ## pom.xml ### 添加源文件 ```xml org.codehaus.mojo build-helper-maven-plugin 3.0.0 add-integration-test-sources generate-test-sources add-test-source src/int/java add-integration-test-resources generate-test-resources add-test-resource src/int/resources ``` ## 设置参数 ```xml skipTests true true true unit true false int false true tests false false ``` ### 单元测试 & 集成测试的区分 以`.Test.java`结尾的文件为单元测试,以`.IntTest.java`结尾的文件为集成测试 ```xml org.apache.maven.plugins maven-surefire-plugin ${skipUnitTest} **/*Test.java **/*IntTest.java org.apache.maven.plugins maven-failsafe-plugin ${skipIntTest} **/*IntTest.java ``` ## 运行结果展示 ### 不执行任何测试 ```shell mvn clean install -P skipTests ``` **结果** ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201113155954798.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTkxNTMxNA==,size_16,color_FFFFFF,t_70#pic_center) ### 只执行单元测试 ```shell mvn clean install -P unit ``` **结果** ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201113160116391.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTkxNTMxNA==,size_16,color_FFFFFF,t_70#pic_center) ### 只执行集成测试 ```shell mvn clean install -P int ``` **结果** ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201113160923141.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTkxNTMxNA==,size_16,color_FFFFFF,t_70#pic_center) ### 全都执行 ```shell mvn clean install -P tests ``` **结果** ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201113161040234.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTkxNTMxNA==,size_16,color_FFFFFF,t_70#pic_center) ## 项目代码 [Maven For Test](https://gitee.com/Protector_hui/maven4test)