前言
pytest介紹
1.pytest是比較成熟的一款python測試框架
2.簡單靈活,容易上手,對比unitest框架更豐富。
3.單元測試和復雜的功能測試,都支持。
4.同時支持selenium,appium和接口自動化測試。
5.支持三方插件結合Allure生成測試報告,可以集成到jenkins。
pytest安裝
安裝方式: pip install pytest
如果安裝失敗,查看是否是需要升級pip,出現這個錯誤就是需要升級pip pip install --upgrade pip
有時網絡不好或者不穩定時候會出現升級失敗的情況,升級時加上默認超時時間就可以了:
pip --default-timeout=100 install --upgrade pip
升級pip之后再次安裝pytest
pytest的升級:
pip install -U pytest
查看是否安裝成功:
pytest --version
pytest使用規則
測試文件:
1.test_*.py
2.*_test.py
用例識別:
1.Test*類包含的所有test_*的方法,注意測試類中不能帶有__init__方法
2.不在class中的所有的test_*方法
pytest可以兼容unitest框架寫的用例和方法
pytest運行
創建一個test_a.py文件
# content of test_sample.py def inc(x): return x + 1 def test_answer(): assert inc(3) == 5
然后終端輸入pytest -v,回車。pytest會自動去找以test開頭的文件和方法,找到之后就會去執行。
platform win32 -- Python 3.6.0, pytest-6.0.1, py-1.9.0, pluggy-0.13.1 rootdir: C:\Users\10835\test plugins: allure-pytest-2.8.14, forked-1.1.3, html-1.16.0, metadata-1.9.0, repeat-0.8.0, rerunfailures-9.0, xdist-1.23.2 collected 1 item test_a.py F [100%] ================================================================================ FAILURES ================================================================================= _______________________________________________________________________________ test_answer _______________________________________________________________________________ def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3) test_a.py:7: AssertionError ========================================================================= short test summary info ========================================================================= FAILED test_a.py::test_answer - assert 4 == 5
關注公眾號獲取更多更新~