用Python寫好的測試程式報告如何與Jenkins DashBoard整合?

用Python寫好的測試程式報告如何與Jenkins DashBoard整合?

Jenkins 被廣泛的運用在持續建構與整合的軟體開發週期中。

如果我們有測試程式用Python完成,如何將測試結果在 Jenkins中呈現呢?

這篇文章主要介紹一個小工具,讓我們可以很容易將 Python測試結果與 Jenkins的環境整合。

 

Jenkins只支援JUnit測試結果?

使用 Jenkins的UI就會發現Post-build Actions > Publish JUnit test result report

選單的選項是 “Junit Test Result report”,那是不是說明只能夠將 Junit 測試結果由Jenkins輸出?

其實不是的! 正確的來說只要測試結果報告是 XML 格式,都可以利用 “Publish JUnit test result report”在Jenkins呈現測試結果。

因此,我們只需要將 Python 測試結果轉換為 XML格式,也可以將Python unitTest的結果在 Jenkins上呈現。

 

Jenkins configuration

因此,只要測試報告結果為 XML 格式,就可以在 Jenkins呈現測試結果報告。與使用的程式語言 Java or Python無關。

Jenkins report

 工具主角 NoseXUnit

NoseXUnit http://nosexunit.sourceforge.net/

我們使用 nose來執行 Python unittest的測試個案

Nose有一個plug-in 可以讓測試結果輸出為 XML,這個工具就是 “NoseXUnit” 或是 “NoseXML”

當然首先要安裝 nose為前提,再安裝  NoseXML

 執行步驟

  • 0. pip install nose
  • 1. pip install nosexunit 或是 easy_install NoseXUnit 或是 pip install nosexml
  • 2. nosetests  tests.py  –with-xunit
  • 3. 測試結果產生於 /NoseXUnit/core/TEST-tests.xml 或是當前工作路徑,檔名為 “nosetests.xml”
  • 4. 設定 Jenkins Jenkins
  •      Post-build Actions > Publish JUnit test result report > target/NoseXUnit/core/*.xml

 

替代方案 XMLrunner

Pip     install    xmlrunner

https://pypi.python.org/pypi/xmlrunner/1.7.4

產生的測試報告會在 \test-reports

另外一個可以將測試結果輸出為 XML的工具。

如果是使用 noseTest執行測試個案 python unitTest,筆者建議直接使用 noseXunit即可。

因為程式完全都不用修改,僅需在執行時多一個參數。

  • nosetests  tests.py  –with-xunit

 

Leave a Reply

Your email address will not be published. Required fields are marked *