[Python] 用 Pytest 來做 Unit Test
avatar-img
Wis Vocus

[Python] 用 Pytest 來做 Unit Test

更新於 發佈於 閱讀時間約 8 分鐘

什麼是Pytest?

Pytest是一款強大的Python測試工具,支援平行化測試,使用上簡單方便,適合用在各種不同類型的軟體架構,也適用於RD、QA或獨立測試小組,也適合正要導入TDD的公司。

Python Developer Survey 2018 Results 被選為最受歡迎的Testing Framework。(不多說了自己看吧 XD)

raw-image

來寫測試吧!

  • 先用pip安裝pytest:
pip install pytest
  • 測試專案 - Folder Structure [Link]
pytest-demo
│ README.md

├─libs
│ module.py
│ __init__.py

└─tests
test_module.py
  • libs\module.py


  • tests\test_module.py


1. 在 module.py 定義一個名為 Module 的 class,再將class import 至 test_module.py

2. 接著,宣告兩個測試function,命名方式為test_開頭

3. 將pytest-demo這個路徑加入PYTHONPATH。 [Windows] [Linux]

我們將驗證 Module.get_true() 的回傳值為True,而 Module.get_false() 回傳值為 False

在pytest-demo路徑下執行測試:

pytest-demo> pytest
==================== test session starts ====================
platform win32 -- Python 3.6.2, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: ..\pytest-demo
collected 2 items
tests\test_module.py .. [100%]
==================== 2 passed in 0.03 seconds ====================
測試成功的執行了! 但...我想看更詳細的訊息怎麼辦?
使用 pytest -v 或是 pytest --verbose 會得到以下結果~~~~
\pytest-demo> pytest -v
==================== test session starts ==================== platform win32 -- Python 3.6.2, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- ..\programs\python\python36-32\python.exe
cachedir: .pytest_cache
rootdir: ..\pytest-demo
collected 2 items
tests/test_module.py::test_get_true_func PASSED [ 50%] tests/test_module.py::test_get_false_func PASSED [100%]
==================== 2 passed in 0.03 seconds ====================
太神啦! 簡單快速地完成了! 
但我們更改一下 get_true() 的回傳值為 False 來產生測試失敗的狀況:
\pytest-demo> pytest -v
==================== test session starts ====================
platform win32 -- Python 3.6.2, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- ..\programs\python\python36-32\python.exe
cachedir: .pytest_cache
rootdir: ..\pytest-demo
collected 2 items
tests/test_module.py::test_get_true_func FAILED [ 50%] tests/test_module.py::test_get_false_func PASSED [100%]
==================== FAILURES ==================== ____________________________________________________________ test_get_true_func _____________________________________________________________
def test_get_true_func():
> assert Module.get_true() is True
E assert False is True
E + where False = <function Module.get_true at 0x036A0930>()
E + where <function Module.get_true at 0x036A0930> = Module.get_true
tests\test_module.py:5: AssertionError
==================== 1 failed, 1 passed in 0.07 seconds ====================
在 Windows PowerShell Console 下會有顏色可以很簡單的區分 PASS or FAIL
raw-image

參考資料:

WisChang005/pytest-demo
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…github.com

[Python][Selenium] Chrome Browser 與 Driver 惱人的版本管理
最近手邊事情告了一個段落,剛好有空就來記錄一下 2019 寫過的小工具,Selenium 這套 Web Testing Framework…medium.com

[Python] 那些好用的 Pytest Plugins
由 Google Trend 數據可以得知,Pytest 在 Google 關鍵字搜尋有逐漸增加的趨勢。當隕石紛紛落下,軟體工程師還是必須要生存下去,隨著開發時程的迭代不斷的變快,Software QA…medium.com

avatar-img
Wis Vocus
3會員
9內容數
Drunk Wis
留言
avatar-img
留言分享你的想法!
Wis Vocus 的其他內容
Software QA Engineer 成長紀錄與面試經驗分享 個人背景 面試經驗 1. 一動數據 Quadas - Sr. Software Engineer (Automation Test) 結果: 感謝函 結果: 婉拒 3. 紅點子科技 Voice Tube - 自動化軟體測試工程師
Software QA Engineer 成長紀錄與面試經驗分享 個人背景 面試經驗 1. 一動數據 Quadas - Sr. Software Engineer (Automation Test) 結果: 感謝函 結果: 婉拒 3. 紅點子科技 Voice Tube - 自動化軟體測試工程師