1. 程式人生 > >Jenkins構建,執行pytest命令時提示command not found問題解決

Jenkins構建,執行pytest命令時提示command not found問題解決

問題說明

在centos下安裝了jenkins,在構建步驟中選擇執行shell
這裡寫圖片描述
新增命令

py.test /home/test/test_do.py

執行提示

+ py.test /home/tet/test_do.py
/tmp/jenkins6679338483323490176.sh: line 2: py.test: command not found

google找到思路

jenkins找不到命令一般有兩種解決方式(前提是jenkins所在使用者組有該命令的執行許可權):
1、增加環境變數:在待執行的shell指令碼前,執行export命令:export PATH = xxx:$PATH(xxx為找不到命令的所在目錄)
2、指令碼中直接使用命令的絕對路徑:/usr/local/bin/py.test xxxx

使用第2種方法嘗試解決
  • 在linux中使用命令which pytest,找到pytest命令的路徑 (我找到的路徑為/root/anaconda2/bin/pytest)
  • 在jenkins命令中更改為 /root/anaconda2/bin/pytest /home/ling/test_do.py

出現報錯,沒有許可權
+ /root/anaconda2/bin/pytest /home/ling/test_do.py
/tmp/jenkins8129489062926013803.sh: line 2: /root/anaconda2/bin/pytest: Permission denied

如上google找到的思路所說,應該是jenkins所在使用者組沒有該命令的執行許可權( /root 目錄下的許可權)
jenkins預設是使用jenkins賬戶來進行操作的,更改為root賬號
更改jenkins預設使用者的方法,參考文章

修改jenkins預設的使用者

  • 修改成功後,重新啟動jenkins
  • 構建成功

擴充套件 pytest使用

/root/anaconda2/bin/pytest /home/ling/test_do.py -s --reruns 1 --junit-xml=test_result.xml
  • 重試執行cases -reruns NUM
    NUM 為重試次數

  • 顯示print內容 -s
    在jenkins控制檯輸出會顯示print的內容

  • 測試結果輸出到報告 –junit-xml=test_result.xml
    這裡寫圖片描述