mvn clean
mvn package
mvn clean package
mvn test
<project> [...] <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> </plugin> </plugins> </pluginManagement> </build> [...] </project>
<dependencies> [...] <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.8</version> <scope>test</scope> </dependency> [...] </dependencies>
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > <suite name="testSuite"> <test name="test"> <classes> <class name=“ATest" /> </classes> </test> </suite>
import org.testng.Assert; import org.testng.annotations.Test; public class ATest { @Test public void testIsTrue(){ A a=new A(); Assert.assertTrue(a.returnTrue()); } }
<project> [...] <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </pluginManagement> </build> […] </project>
mvn package -DskipTests
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> [...] </project>
mvn package -Dmaven.test.skip=true
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> [...] </project>
mvn -Dtest=ATest test
”maven-surefire-plugin“插件还支持使用星号匹配测试类名的方式以指定运行特定的测试类,星号表示匹配零个或多个字符。例如只执行”A“开头的测试类:
mvn -Dtest=A* test
除了使用星号匹配,还能够使用逗号指定多个测试类:
mvn test -Dtest=ATest,BTest
同时匹配类名以A开头的测试类和类名为BTest的测试类:
mvn -Dtest=A*,BTest test
当test命令经过参数匹配不到任何测试类时,项目将会构建失败。配置参数DfailIfNoTests=false能够在匹配不到测试类时依旧构建成功:
mvn -Dtest=AATest -DfailIfNoTests=false test
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <includes> <include>**/*Tests.java</include> </includes> </configuration> </plugin> </plugins> </build> [...] </project>
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <excludes> <exclude>**/CTest.java</exclude> <exclude>**/DTest.java</exclude> </excludes> </configuration> </plugin> </plugins> </build> [...] </project>
参考资料:java
Maven文档:http://maven.apache.org/guides/apache
maven-surefire-plugin 文档:http://maven.apache.org/surefire/maven-surefire-plugin/框架
=====================================================================maven