Use of surefire plug-in

Keywords: Junit Maven Apache xml

Article catalog

Write in front

The surefire plug-in is used in the test phase of maven's life cycle for testing. It provides some mvn operations that are convenient for us to perform the testing tasks we need

  • It is required to match JDK above 1.7
  • More than maven 3 required

Combined with TestNG configuration

In addition to configuring the basic testng dependency, we need to configure the surefire plug-in

This article on the official website introduces how to configure with testng http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html

<plugins>
    [...]
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <!-- Version can be selected by yourself -->
        <version>RELEASE</version>
        <configuration>
            <!-- Specify the running xml File path -->
            <suiteXmlFiles>
                <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles>
            <!-- To set parameters, you can use the @Parameters receive -->
            <systemPropertyVariables>
            	<propertyName>firefox</propertyName>
          </systemPropertyVariables>
          [...]
          <argLine>-Dfile.encoding=UTF-8</argLine>
        </configuration>
    </plugin>
    [...]
</plugins>

In this plug-in, you can even configure TestNG to run in groups and in parallel. There are many other configurations, which are quite powerful. However, I prefer to use the testng.xml To control. Please refer to the official website for more detailed configuration

Combined with Junit configuration

junit low

In addition to configuring basic junit dependencies, we need to configure the surefire plug-in

junit5: http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html

<plugins>
    [...]
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-surefire-plugin</artifactId>
		<version>RELEASE</version>
		<dependencies>
			<dependency>
                <groupId>org.apache.maven.surefire</groupId>
                <artifactId>surefire-junit47</artifactId>
				<version>RELEASE</version>
			</dependency>
		</dependencies>
	</plugin>
	[...]
</plugins>

When there is no configuration version, this shows which version of the logic the surefire plug-in depends on. It is quite intelligent

    if the JUnit 5 Platform Engine is present in the project
        use junit-platform
    if the JUnit version in the project >= 4.7 and the <<<parallel>>> configuration parameter has ANY value
        use junit47 provider
    if JUnit >= 4.0 is present
        use junit4 provider
    else
        use junit3.8.1

junit5

JUnit Jupiter dependency of JUnit 5 includes JUnit Jupiter engine and JUnit platform launcher in JUnit Jupiter engine

Official website introduction: http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html

<plugins>
    [...]
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>RELEASE</version>
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>5.3.2</version>
            </dependency>
        </dependencies>
    </plugin>
    [...]
</plugins>

It's smart to respect this logic when it's not configured

    if the JUnit 5 Platform Engine is present in the project
        use junit-platform
    if the JUnit version in the project >= 4.7 and the <<<parallel>>> configuration parameter has ANY value
        use junit47 provider
    if JUnit >= 4.0 is present
        use junit4 provider
    else
        use junit3.8.1

Please refer to the official website for the rest

Matching mvn command use

It is also introduced on the official website http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

# Test a single test class
mvn -Dtest=TestCircle test
mvn -Dtest=TestCi*le test

# Test specific methods
mvn -Dtest=TestCircle#mytest test
mvn -Dtest=TestCircle#test* test
# Since of Surefire Plugin 2.19 you can select multiple methods (JUnit 4, JUnit 4.7+ and TestNG):
mvn -Dtest=TestCircle#testOne+testTwo test

Even support rerun failed tests and debug tests

advertisement

Pragmatism learning Python (a Python practical case that Xiaobai is easy to use)

Posted by slj90 on Fri, 12 Jun 2020 02:24:24 -0700