python 單元測試

pytest是python的一種單元測試框架,與python自帶的unittest測試框架類似,但是比unittest框架使用起來更簡潔,效率更高。根據pytest的官方網站介紹,它具有如下特點:

  • 非常容易上手,入門簡單,文檔豐富,文檔中有很多實例可以參考

  • 能夠支持簡單的單元測試和復雜的功能測試

  • 支持參數化

  • 執行測試過程中可以將某些測試跳過,或者對某些預期失敗的case標記成失敗

  • 支持重復執行失敗的case

  • 支持運行由nose, unittest編寫的測試case

  • 具有很多第三方插件,并且可以自定義擴展

  • 方便的和持續集成工具集成

    ?

安裝 pytest 包

pip install pytest

安裝 pytest-cov 包 (此插件生成覆蓋率報告)

pip install pytest-cov

安裝 pytest-html 包 (用于生成HTML報告的pytest插件)

pip install pytest-html

終端查看單元測試覆蓋率

py.test --cov=test/

生成 HTML 測試覆蓋率報告

pytest --html=report.html

在 test(測試文件) 目錄下執行 pytest 查看效果

gyw@gyw:~/Desktop/code/project/xxxxxx/src/backend$ py.test --cov=test/
=========================== test session starts =====================================
platform linux -- Python 3.5.2, pytest-3.0.7, py-1.4.34, pluggy-0.4.0
metadata: {'Platform': 'Linux-4.10.0-37-generic-x86_64-with-Ubuntu-16.04-xenial', 'Packages': {'py': '1.4.34', 'pluggy': '0.4.0', 'pytest': '3.0.7'}, 'Python': '3.5.2', 'Plugins': {'metadata': '1.5.0', 'cov': '2.5.1', 'html': '1.16.0'}}
rootdir: /home/gyw/Desktop/code/project/xxxxxx/src/backend, inifile:
plugins: metadata-1.5.0, html-1.16.0, cov-2.5.1
collected 4 items 

test/test_exampl.py .
test/test_monitor.py .
test/test_run.py .
test/test_workers.py .

----------- coverage: platform linux, python 3.5.2-final-0 -----------
Name                               Stmts   Miss  Cover
------------------------------------------------------
test/__init__.py                       0      0   100%
test/interactive/__init__.py           1      0   100%
test/interactive/backend.py           11     11     0%
test/interactive/fib.py                3      1    67%
test/interactive/frontend.py          17     17     0%
test/interactive/monitor_test.py      10      5    50%
test/test_exampl.py                   38      3    92%
test/test_monitor.py                  62     10    84%
test/test_run.py                       4      0   100%
test/test_workers.py                  90     15    83%
------------------------------------------------------
TOTAL                                236     62    74%


=========================== 4 passed in 20.61 seconds =========================
gyw@gyw:~/Desktop/code/project/xxxxxx/src/backend$ pytest --html=report.html
=========================== test session starts ================================
platform linux -- Python 3.5.2, pytest-3.0.7, py-1.4.34, pluggy-0.4.0
metadata: {'Plugins': {'metadata': '1.5.0', 'cov': '2.5.1', 'html': '1.16.0'}, 'Python': '3.5.2', 'Packages': {'py': '1.4.34', 'pluggy': '0.4.0', 'pytest': '3.0.7'}, 'Platform': 'Linux-4.10.0-38-generic-x86_64-with-Ubuntu-16.04-xenial'}
rootdir: /home/gyw/Desktop/code/project/xxxxxx/src/backend, inifile:
plugins: metadata-1.5.0, html-1.16.0, cov-2.5.1
collected 4 items 

test/test_exampl.py .
test/test_monitor.py .
test/test_run.py .
test/test_workers.py .

------ generated html file: /home/gyw/Desktop/code/project/xxxxxx/src/backend/report.html ------
============================= 4 passed in 31.82 seconds =======================

別人的教程

http://www.cnblogs.com/landhu/p/7461501.html

http://www.cnblogs.com/landhu/p/7463631.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容