pytest是python中較常用的測驗框架,官方檔案見:
https://docs.pytest.org/en/stable/contents.html#toc
安裝命令:
pip install -U pytest
檢查是否安裝成功命令:
pytest --version
能查到版本號說明安裝OK,否則嘿嘿,
創建第一個測驗腳本test_sample.py:
# content of test_sample.py def func(x): return x + 1 def test_answer(): assert func(3) == 5
通過命令pytest執行,得到結果如下:
$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 1 item
test_sample.py F [100%]
================================= FAILURES =================================
_______________________________ test_answer ________________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
test_sample.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_sample.py::test_answer - assert 4 == 5
============================ 1 failed in 0.12s =============================
跟Junit測驗框架類似都有assert的方法來對比實際結果和預期的結果是否一致,如果不一致則回傳F,并指出錯誤的位置,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/206410.html
標籤:Python
