一個好用的python測試架構(pytest)及其CI設定__python

來源:互聯網
上載者:User
背景

我們有部分正式代碼是採用python編寫,這部分代碼也是需要TDD的,因此,需要找到一個好用的測試架構。 架構選擇

python的測試載入器大全:
https://wiki.python.org/moin/PythonTestingToolsTaxonomy

python主流的測試載入器橫向比較
http://docs.python-guide.org/en/latest/writing/tests/
http://pythontesting.net/test-podcast/

根據下面這篇文章,我們決定使用pytest
http://mathieu.agopian.info/presentations/2015_06_djangocon_europe/?full#cover

pytest的官方資料
http://wiki.zte.com.cn/pages/viewpage.action?pageId=38150854 CI的整合

普通的pytest安裝是沒有包含覆蓋率的

pip install pytest

pytest覆蓋率需要額外安裝

pip install pytest-cov

這時,新增了py.test命令,這是原來pytest命令基礎上封裝的,新增了覆蓋率功能。

例子示範:

py.test test_conf2bin.py --junit-xml=test_conf2bin_report.xml --cov-report=html --cov-config=.coveragerc --cov=./
test_conf2bin.py是目標測試檔案 –junit-xml=test_conf2bin_report.xml是產生junit格式的test_conf2bin_report.xml –cov-report=html 輸出html的報告 –cov-config=.coveragerc 使用.coveragerc設定檔(py.test是基於coverage.py實現的,因此支援coverage.py的設定檔)

–cov=./ 這個是必要的,如果沒有這個,將無法輸出html的報告

.coveragerc這個檔案可以讓py.test實現更多進階的功能,實現舉例如下:

[run]branch = True[report]include = conf2bin.py[html]directory = python_coverage_report

[report]
include = conf2bin.py
由於py.test會計算一個檔案夾裡的所有檔案,如果只需要統計conf2bin.py這個檔案的覆蓋率,則可以這麼寫
[html]
directory = python_coverage_report
指的是html的輸出檔案夾 最後

pytest個人試用,確實很好用,比內建的測試架構強多了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.