1. 程式人生 > 其它 >【pytest單元測試框架】(3)pytest 擴充套件

【pytest單元測試框架】(3)pytest 擴充套件

pytest 擴充套件

Pytest 可以擴充套件非常多的外掛來實現各種功能,這裡介紹幾個對做 Web 自動化測試非常有用的外掛。

1、pytest-html

pytest-html 可以生成 HTML 格式的測試報告。首先,通過 pip 命令安裝 pytest-html 擴充套件。
pip install pytest-html

其次,執行測試用例,並生成測試報告

pytest ./ --html=./report/result.html
最後,在 report 目錄下開啟 result.html,pytest-html 測試報告如圖所示:

2、pytest-rerunfailures

pytest-rerunfailures 可以在測試用例失敗時進行重試:
pip install pytest-rerunfailures

通過“--reruns”引數設定測試用例執行失敗後的重試次數:

(base_practice) D:\00test\base_practice\pyTest>pytest -v ./ --reruns 3 --html=./report/20211118result.html
=============================================================== test session starts ===============================================================
platform win32 
-- Python 3.7.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\yzp\PycharmProjects\base_practice\Scripts\python.exe cachedir: .pytest_cache metadata: {'Python': '3.7.4', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins' : {'html': '3.1.1'
, 'metadata': '1.11.0', 'rerunfailures': '10.2'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_311'} rootdir: D:\00test\base_practice\pyTest plugins: html-3.1.1, metadata-1.11.0, rerunfailures-10.2 collected 17 items test_assert.py::test_add1 PASSED [ 5%] test_assert.py::test_add2 RERUN [ 11%] test_assert.py::test_add2 RERUN [ 11%] test_assert.py::test_add2 RERUN [ 11%] test_assert.py::test_add2 FAILED [ 11%] test_assert.py::test_isprime1 PASSED [ 17%] test_assert.py::test_isprime2 PASSED [ 23%] test_assert.py::test_in PASSED [ 29%] test_assert.py::test_not_in PASSED [ 35%] test_fixtrue01.py::test_35 RERUN [ 41%] test_fixtrue01.py::test_35 RERUN [ 41%] test_fixtrue01.py::test_35 RERUN [ 41%] test_fixtrue01.py::test_35 FAILED [ 41%] test_fixtrue01.py::test_45 PASSED [ 47%] test_fixtrue02.py::TestMul::test_mul35 RERUN [ 52%] test_fixtrue02.py::TestMul::test_mul35 RERUN [ 52%] test_fixtrue02.py::TestMul::test_mul35 RERUN [ 52%] test_fixtrue02.py::TestMul::test_mul35 FAILED [ 52%] test_fixtrue02.py::TestMul::test_mul45 PASSED [ 58%] test_parameterized.py::test_row[case1] PASSED [ 64%] test_parameterized.py::test_row[case2] RERUN [ 70%] test_parameterized.py::test_row[case2] RERUN [ 70%] test_parameterized.py::test_row[case2] RERUN [ 70%] test_parameterized.py::test_row[case2] FAILED [ 70%] test_parameterized.py::test_row[case3] PASSED [ 76%] test_parameterized.py::test_row[case4] PASSED [ 82%] test_rerun.py::test_fail RERUN [ 88%] test_rerun.py::test_fail RERUN [ 88%] test_rerun.py::test_fail RERUN [ 88%] test_rerun.py::test_fail FAILED [ 88%] test_sample.py::test_answer RERUN [ 94%] test_sample.py::test_answer RERUN [ 94%] test_sample.py::test_answer RERUN [ 94%] test_sample.py::test_answer FAILED [ 94%] test_project/test_sub.py::test_baidu PASSED [100%] ==================================================================== FAILURES ===================================================================== ____________________________________________________________________ test_add2 ____________________________________________________________________ def test_add2(): > assert add(5, 5) > 10 E assert 10 > 10 E + where 10 = add(5, 5) test_assert.py:34: AssertionError _____________________________________________________________________ test_35 _____________________________________________________________________ def test_35(): > assert mul(3, 5) == 14 E assert 15 == 14 E +15 E -14 test_fixtrue01.py:41: AssertionError -------------------------------------------------------------- Captured stdout setup -------------------------------------------------------------- ===========setup_module========= ==========setup_founction========= ==========setup========= ------------------------------------------------------------ Captured stdout teardown ------------------------------------------------------------- =========teardowm========== ==========setup_founction========== -------------------------------------------------------------- Captured stdout setup -------------------------------------------------------------- ==========setup_founction========= ==========setup========= ------------------------------------------------------------ Captured stdout teardown ------------------------------------------------------------- =========teardowm========== ==========setup_founction========== -------------------------------------------------------------- Captured stdout setup -------------------------------------------------------------- ==========setup_founction========= ==========setup========= ------------------------------------------------------------ Captured stdout teardown ------------------------------------------------------------- =========teardowm========== ==========setup_founction========== -------------------------------------------------------------- Captured stdout setup -------------------------------------------------------------- ==========setup_founction========= ==========setup========= _______________________________________________________________ TestMul.test_mul35 ________________________________________________________________ self = <pyTest.test_fixtrue02.TestMul object at 0x000002095B02FA48> def test_mul35(self): > assert mul(3, 5) == 16 E assert 15 == 16 E +15 E -16 test_fixtrue02.py:41: AssertionError -------------------------------------------------------------- Captured stdout setup -------------------------------------------------------------- ===============setup_class=============== ==============setup_method================= ==========setup============= ------------------------------------------------------------ Captured stdout teardown ------------------------------------------------------------- ===========teardown============ ==============teardown_method============== -------------------------------------------------------------- Captured stdout setup -------------------------------------------------------------- ==============setup_method================= ==========setup============= ------------------------------------------------------------ Captured stdout teardown ------------------------------------------------------------- ===========teardown============ ==============teardown_method============== -------------------------------------------------------------- Captured stdout setup -------------------------------------------------------------- ==============setup_method================= ==========setup============= ------------------------------------------------------------ Captured stdout teardown ------------------------------------------------------------- ===========teardown============ ==============teardown_method============== -------------------------------------------------------------- Captured stdout setup -------------------------------------------------------------- ==============setup_method================= ==========setup============= _________________________________________________________________ test_row[case2] _________________________________________________________________ base = 3, exponent = 3, expected = 9 @pytest.mark.parametrize( "base, exponent, expected", ((2, 2, 4), (3, 3, 9), (2, 3, 8), (0, 9, 0)), ids=["case1", "case2", "case3", "case4"]) def test_row(base, exponent, expected): > assert math.pow(base, exponent) == expected E assert 27.0 == 9 E +27.0 E -9 test_parameterized.py:18: AssertionError ____________________________________________________________________ test_fail ____________________________________________________________________ def test_fail(): > assert 2+2 == 5 E assert 4 == 5 E +4 E -5 test_rerun.py:2: AssertionError ___________________________________________________________________ test_answer ___________________________________________________________________ def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E +4 E -5 test_sample.py:13: AssertionError ------------------------------ generated html file: file://D:\00test\base_practice\pyTest\report\20211118result.html ------------------------------ ============================================================= short test summary info ============================================================= FAILED test_assert.py::test_add2 - assert 10 > 10 FAILED test_fixtrue01.py::test_35 - assert 15 == 14 FAILED test_fixtrue02.py::TestMul::test_mul35 - assert 15 == 16 FAILED test_parameterized.py::test_row[case2] - assert 27.0 == 9 FAILED test_rerun.py::test_fail - assert 4 == 5 FAILED test_sample.py::test_answer - assert 4 == 5 ===================================================== 6 failed, 11 passed, 18 rerun in 0.30s ======================================================

從執行結果可以看到,在測試用例執行失敗後進行了6 * 3 次重試。因為 Web 自動化測試會因為網路等因素導致測試用例執行失敗,而重試機制可以增加測試用例的穩定性。

3、pytest-parallel 擴充套件

  pytest-parallel 擴充套件可以實現測試用例的並行執行:

  建立 test_ parallel.py,在每條測試用例中分別設定 sleep()來模擬執行時間較長的測試用例。
from time import sleep

def test_sleep3():
    sleep(3)

def test_sleep4():
    sleep(4)

def test_sleep5():
    sleep(5)

  不使用執行緒執行測試用例:

(base_practice) D:\00test\base_practice\pyTest>pytest -q test_sleep.py
...                                                                                                                                          [100%]
3 passed in 12.04s

(base_practice) D:\00test\base_practice\pyTest>

  引數“--tests-per-worker”用來指定執行緒數,“auto”表示自動分配:

pytest -q test_parallel.py --tests-per-worker auto
pytest-parallel: 1 worker (process), 3 tests per worker (threads)
... [100%]
3 passed in 6.02 seconds
  pytest-parallel 的更多用法如下所示:
# runs 2 workers with 1 test per worker at a time
> pytest --workers 2
# runs 4 workers (assuming a quad-core machine) with 1 test per worker
> pytest --workers auto
# runs 1 worker with 4 tests at a time
> pytest --tests-per-worker 4
# runs 1 worker with up to 50 tests at a time
> pytest --tests-per-worker auto
# runs 2 workers with up to 50 tests per worker
> pytest --workers 2 --tests-per-worker auto

雖然並行執行測試可以非常有效地縮短測試的執行時間,但是 Web 自動化測試本身非常脆弱,在並行執行測試時很可能會產生相互干擾,從而導致測試用例失敗,因此建議謹慎使用。

本部落格所有文章僅用於學習、研究和交流目的,歡迎非商業性質轉載。

本文來自部落格園,作者:hello_殷,轉載請註明原文連結:https://www.cnblogs.com/yinzuopu/p/15573691.html

本文版權歸作者和部落格園共有,歡迎轉載,但必須給出原文連結,並保留此段宣告,否則保留追究法律責任的權利。