surefire插件的使用

在這裏插入圖片描述

寫在前面

surefire 插件使用在 maven 生命週期的 test 階段,是用來做測試使用的,他提供了一些 mvn 操作可以很方便的讓我們執行我們需要的測試任務

  • 要求搭配 jdk 1.7 以上
  • 要求搭配 maven 3 以上

結合 TestNG 配置

除了配置基礎的 testng 依賴之外,我們需要配置 surefire 插件

這篇官網文章專門介紹如何結合 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>RELEASE</version>
        <configuration>
            <!-- 指定運行的 xml 文件路徑 -->
            <suiteXmlFiles>
                <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles>
            <!-- 設置傳參,可以用 @Parameters 接收 -->
            <systemPropertyVariables>
            	<propertyName>firefox</propertyName>
          </systemPropertyVariables>
          [...]
          <argLine>-Dfile.encoding=UTF-8</argLine>
        </configuration>
    </plugin>
    [...]
</plugins>

此插件中甚至可以配置 testng 的分組運行和並行運行,還有其他等多種配置,還是蠻強大的,只不過這部分我比較喜歡通過 testng.xml 中設置來控制。更加具體細節的配置請參照官網介紹使用

結合 Junit 配置

低版本 junit

除了配置基礎的 junit 依賴之外,我們需要配置 surefire 插件

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>

當沒有配置版本時候,這裏說明了 surefire 插件選用哪個版本依賴的邏輯,挺智能的

    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

junit5 的 junit-jupiter 依賴包含了 junit-jupiter-engine 然後 junit-jupiter-engine 中又包含了 junit-platform-launcher

官網介紹使用: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>

當沒有配置時候尊崇這樣的邏輯,還是很智能的

    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

其餘的使用請參見官網介紹

配套 mvn 命令使用

官網也有介紹使用 http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

# 測試單個測試類
mvn -Dtest=TestCircle test
mvn -Dtest=TestCi*le test

# 測試具體的方法
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

甚至支持重跑失敗的測試和 debug 測試


廣告

實用主義學Python(小白也容易上手的Python實用案例)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章