maven打包会自动执行test方法,如何skipTests关闭maven自动执行test
现象启动时执行@test方法解决方案关闭自动测试
·
现象
对maven项目进行package打包时,发现自动执行了test单元测试,可能到导致执行了意向不到的结果(比如测试修改数据库)。
分析
maven中的maven-surefire-plugin包,在package时会对项目中test/java目录下查找符合规则的Test类进行执行,进行自动化测试。参考完档:http://maven.apache.org/guides/getting-started/index.html
Test类名匹配规则有:
- **/*Test
- **/*TestCase
- **/*Tests (文档中没有)
- **/Test*
Test类名忽略规则有:
**/Abstract*Test.java (文档中,测试没用)
**/Abstract*TestCase.java (文档中,测试没用)
验证
类名
执行结果
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ parent-tester ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.lizz.LizzTests
@@@@@LizzTests@@@@@
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.043 s - in com.lizz.LizzTests
[INFO] Running com.lizz.LizzTest
@@@@@LizzTest@@@@@
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in com.lizz.LizzTest
[INFO] Running com.lizz.Abstract8Test
@@@@@Abstract8Test@@@@@
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 s - in com.lizz.Abstract8Test
[INFO] Running com.lizz.LizzTestCase
@@@@@LizzTestCase@@@@@
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in com.lizz.LizzTestCase
[INFO] Running com.lizz.TestAbstract
@@@@@TestAbstract@@@@@
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in com.lizz.TestAbstract
[INFO] Running com.lizz.AbstractLizzTest
@@@@@AbstractLizzTest@@@@@
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in com.lizz.AbstractLizzTest
[INFO] Running com.lizz.AbstractLizzTests
@@@@@AbstractLizzTests@@@@@
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 s - in com.lizz.AbstractLizzTests
[INFO] Running com.lizz.AbstractLizzTestCase
@@@@@AbstractLizzTestCase@@@@@
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in com.lizz.AbstractLizzTestCase
[INFO] Running com.lizz.Test1
@@@@@Test1@@@@@
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in com.lizz.Test1
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 9, Failures: 0, Errors: 0, Skipped: 0
解决方案
方案1
使用spring-boot-maven-plugin方式打包,可以增加参数 <skipTests>true</skipTests>
<properties>
<skipTests>true</skipTests>
</properties>
方案2
使用maven-surefire-plugin方式打包,可以增加配置<skip>true</skip>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
方案3
idea中设置关闭
方案4
在执行命令中增加 -DskipTests=true,跳过test。
/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/bin/java .... -DskipTests=true package
[INFO] Scanning for projects...
[INFO]
....
效果
收获
合理使用单元测试可以减少手动测试的时间,在持续集成过程中加快开发测速度。
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献7条内容
所有评论(0)