1. Role of surefire pluginThe surefire plug-in is used to execute a unit test of an application in the test phase of the maven build lifecycle. It generates two different forms of test results report: 1). plain text 2). xml file format by default, these files are generated in the project $ {Basedir}/target/surefire-reports,Directory (basedir refers to the directory where the POM file is located ). It can run any unit tests written by testng, JUnit, and pojo.
2. How to use it?Using this plug-in is simple. Using MVN surefire: test or MVN test can all run unit tests under the project.
3. How to configure
1. Dependency Configuration:1). If you are using testng, you can follow this configuration: Introduce testng dependency in the POM file:
<dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>5.12.1</version><scope>test</scope></dependency>
However, if the version you are using is too low (<= 5.11), you have to configure it as follows:
<dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>5.8</version><scope>test</scope><classifier>jdk15</classifier></dependency>
Many may be unfamiliar with classifier. In fact, the package name of Maven is composed
<Artifactid>-<version>-<classifier>. <packaging>. (This is only for the compiled jar package. If it is sources or javadoc, it will become <artifactid>-<version>-<classifier>-<type>. <packaging>, Here <type> can be replaced with sources or javadoc ). According to this rule, the complete package name of testng In the maven repository should be:
testng-5.8-jdk15.jar
Classifier is mainly used to differentiate different environments or different JDK versions.
2) if you use JUnit, introduce this dependency:
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.8.1</version><scope>test</scope></dependency>
2. Plug-in configuration
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.10</version><configuration><parallel>methods</parallel><threadCount>10</threadCount></configuration></plugin>
This is a simple configuration. In fact, this plug-in version is very old, and it only specifies some basic parameters. Even so, it allows you to run MVN test or MVN surefire: Test on the command line, so that it scans the test class under the test class directory (src/test/Java, as long as the class name conforms to * test. java, then this test class will be run.
However, this configuration does not seem satisfactory, because it is actually not scalable. For example, if I want to use jmockit, I want to start multiple threads to run unit tests, and I want to run some unit tests, how can I eliminate some unit tests? Let's continue.
3. Advanced Configuration
First, configure a good permission project.
Add a plug-in to the parent Pom. xml file:
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.5</version><configuration><testNGArtifactName>com.alibaba.external:test.testng</testNGArtifactName><argLine>-javaagent:"${settings.localRepository}/com/alibaba/external/test.jmockit/0.999.10/test.jmockit-0.999.10.jar"</argLine><useSystemClassLoader>true</useSystemClassLoader><testFailureIgnore>true</testFailureIgnore><parallel>false</parallel><forkMode>once</forkMode><suiteXmlFiles><suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile></suiteXmlFiles></configuration></plugin>
(The above section is my Recommended Configuration)
We can observe that all the more parameters are under configuration. We will separately explain:
TestngartifactnameThis parameter specifies the artifactid of the testng jar package.
ArglineIt is used to configure JVM parameters. It is obvious that we can infer that this is prepared to start a new JVM.
UsesystemclassloaderThis story is not explained.
TestfailureignoreThis determines whether to ignore failed unit tests and continue to run other
AboutForkmodeSpecial Explanation:
When Maven runs the test case, it calls the surefire plug-in of Maven and fork a sub-process to execute the test case. The forkmode attribute specifies whether to create a process for each test or whether all tests are completed in the same process.
Forkmode can be set to "never", "Once", "always", and "pertest ".
Pretest: Create a new process for each test. Creating a New JVM for each test is the most thorough test method, but it is also the slowest. It is not suitable for continuous regression on Hudson.
Once: perform all tests in a process. Once is the default setting. We recommend that you use the default setting for continuous regression on Hudson.
Always: run the script in parallel in a process, which can be used by junit4.7 or later versions. The surefire version must provide this function at 2.6 or later, among which threadcount:Specifies the number of threads that can be allocated during execution. It is valid only when used with the parallel parameter. Default Value: 5. <Forkmode> always </forkmode> <parallel> methods </parallel>
<Threadcount> 4 </threadcount>
SuitexmlfilesThis parameter is used to determine the unit test running rules, which are specified in the file.
The relative path src/test/resources/testng is used. XML, then the file should be in the path of each project, we open a project, such as permission application testng. XML to see what it configures.
Content of the testng. xml file:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="plan-project-test"><test name="plan-project-test"><packages><package name="com.alibaba.lp.permission.dal.cache.test" /><package name="com.alibaba.lp.permission.dal.impl.test" /></packages></test></suite>
This file specifies the unit test under which package to run. In fact, there are many ways and dimensions to configure which tasks need to be run and which do not need to be run.
For example, you can specify the classes to run:
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" ><suite name="My test suite"><test name="testing"><classes><class name="TestNGTest1" /><class name="TestNGTest2" /></classes></test></suite>
You can specify the groups to run:
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" ><suite name="My test suite"><test name="testing"><groups><run><include name="method1"/></run></groups><classes><class name="TestNGTest5_2_0" /></classes></test></suite>
If you are interested in the suite, you can view the corresponding configuration in Google suite.
Is this perfect?
Of course not. There is a big problem. Our plug-in is configured in the parent pom. in XML, which means that each sub-project inherits this configuration, so you need to configure a testng under the corresponding directory. XML file. So what if I don't want to run unit tests on some projects and do not want to configure this file?
Method 1: Add attributes in Configuration:
<configuration><skipTests>true</skipTests></configuration>
However, it is obvious that this configuration only appears in the parent Pom. xml. This configuration will make all Unit Tests unavailable.
This is the same as running MVN install-dskiptests or MVN install-dmaven. Test. Skip = true.
Method 2: add the property to the Pom. xml file that does not need to run the unit test, for example, the web project pom of the permission application:
<parent><groupId>com.alibaba.lp.app</groupId><artifactId>permission.parent</artifactId><version>1.0-SNAPSHOT</version></parent> <artifactId>permission.web.perm</artifactId><name>permission web perm project</name> <properties><maven.test.skip>true</maven.test.skip></properties>
In fact, this configuration can be changed to the following configuration to achieve the same effect:
<properties><skipTests>true</skipTests></properties>
Here, by the way, we will explain the differences between maven. Test. Skip and skiptests.
Maven. Test. Skip is used in many plug-ins, such as surefire, Failsafe, and compiler plugin.
Is supported, skiptests works the same, but the scope of application may be smaller (I prefer to use skiptests because it is relatively short ).
I support the use of file-specific include and exclude methods, but if you do not like this method, you can remove suitexmlfiles and use the following configuration to manage it: <Configuration>
<Shortdes> <Include> **/test. Java </include>
</Shortdes> <Excludes> <Exclude> **/testcase. Java </exclude> </Excludes>
</Configuration>
There are other interesting parameters in surefire. If you are interested, you can access
Http://maven.apache.org/plugins/maven-surefire-plugin/examples/inclusion-exclusion.html to learn more.
4. Quiz:
Run MVN test before the surefire plug-in is configured. You will find that all unit tests that have been run using testng in eclipse are suspended. Follow the preceding configurations before running the job. Run MVN test> result.txt, and then open result.txt to view the running result. Testng will tell you which unit tests are down and the total number of use cases.