文章目錄
配置基礎環境
前提:系統中已經安裝了maven、jdk
配置JDK
配置maven
配置git
安裝maven項目插件
創建一個最基礎的maven任務
1、點擊創建任務,輸入項目名稱,選擇maven項目:
2、管理源碼設置
3、觸發器設置
保存之後,任務就創建好了,這是一個最基礎的任務。
參數化
jenkins中配置參數並傳遞給maven項目
指定用例集
指定測試環境
配置build命令參數
clean test -DsuiteXmlFiles=${suiteXmlFiles} -Denv=${env}
(網上大部分傳參數方式都是 -DtestEnv=%testEnv% ,我的jenkins是2.2版本的,不能識別到參數)
注意:執行執行的用例集要注意路徑配置,路徑不對,會提示下面的錯誤
[ERROR] Suite file /Users/lihaihui/.jenkins/workspace/jenkinsTest/testng.xml is not a valid file
在pom.xml中指定用例集(用jenkins傳過來的參數)
在maven項目的pom.xml配置插件「maven-surefire-plugin」,具體使用可參考官方文檔
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFiles}</suiteXmlFile>
</suiteXmlFiles>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
注意:插件不要放到 中,這個裏面聲明的全都是maven生命週期的插件(參考博客)
在測試用例中使用jenkins傳遞的參數
方法一:在testng.xml中接收參數
在測試用例集的xml文件中添加:
<parameter name="testType" value="${testType}"/>
**方法二:使用「maven-surefire-plugin」的系統參數接收變量
<systemPropertyVariables>
<propertyName>${env}</propertyName>
</systemPropertyVariables>
在測試用例中使用參數
不管是方法一還是方法二,才測試用例中聲明參數的方式是一樣的
jenkins中構建項目查看結果
我測試用的兩個測試用例集
jenkins通過參數構建
查看執行結果
參考文章:
https://blog.csdn.net/weixin_30677073/article/details/97493802
https://www.cnblogs.com/melody-emma/p/5033270.html
https://blog.csdn.net/z69183787/article/details/80605461
http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html
https://blog.csdn.net/qq_37858220/article/details/88210796