Python自動化之pytest常用外掛
1、失敗重跑 pytest-rerunfailures
安裝:pip install pytest-rerunfailures
使用:pytest test_class.py --reruns 5 --reruns-delay 1 -vs (失敗後重新執行5次,每次間隔1秒)
@pytest.mark.flaky(reruns = 5 ,reruns-delay = 1) 指定某個用例
#!/usr/bin/python # -*- coding: UTF-8 -*- """ @author:chenshifeng @file:test_calc2.py @time:2020/09/16"""import pytest
@pytest.mark.parametrize('a,b,result', [
(1, 1, 3),
(2, 2, 4),
(100, 100, 200),
(0.1, 0.1, 0.2),
(-1, -1, -2)
], ids=['int', 'int', 'bignum', 'float', 'fushu']) # 引數化
def test_add(a, b, result):
# cal = Calculator()
assert result == a + b
命令列執行:
pytest test_calc2.py --reruns 5 --reruns-delay 1 -vs
結果如下:
============================================================================= test session starts ============================================================================= platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 cachedir: .pytest_cache rootdir:/Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini plugins: rerunfailures-9.1, dependency-0.5.1, ordering-0.6, assume-2.3.2 collected 5 itemstest_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] FAILED
test_calc2.py::test_add[int1] PASSED
test_calc2.py::test_add[bignum] PASSED
test_calc2.py::test_add[float] PASSED
test_calc2.py::test_add[fushu] PASSED================================================================================== FAILURES ===================================================================================
_______________________________________________________________________________ test_add[int0] ________________________________________________________________________________a = 1, b = 1, result = 3
@pytest.mark.parametrize(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a,b,result</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, [ (</span>1, 1, 3<span style="color: rgba(0, 0, 0, 1)">), (</span>2, 2, 4<span style="color: rgba(0, 0, 0, 1)">), (</span>100, 100, 200<span style="color: rgba(0, 0, 0, 1)">), (</span>0.1, 0.1, 0.2<span style="color: rgba(0, 0, 0, 1)">), (</span>-1, -1, -2<span style="color: rgba(0, 0, 0, 1)">) ], ids</span>=[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">int</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">int</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">bignum</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">float</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">fushu</span><span style="color: rgba(128, 0, 0, 1)">'</span>]) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 引數化</span> <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> test_add(a, b, result): cal </span>=<span style="color: rgba(0, 0, 0, 1)"> Calculator()
> assert result == cal.add(a, b)
E assert 3 == 2
E +3
E -2test_calc2.py:26: AssertionError
=========================================================================== short test summary info ===========================================================================
FAILED test_calc2.py::test_add[int0] - assert 3 == 2
==================================================================== 1 failed, 4 passed, 5 rerun in 5.11s =====================================================================
通過裝飾器設定重跑次數與延時時間
#!/usr/bin/python # -*- coding: UTF-8 -*- """ @author:chenshifeng @file:test_calc2.py @time:2020/09/16 """import pytest
@pytest.mark.parametrize('a,b,result', [
(1, 1, 3),
(2, 2, 4),
(100, 100, 200),
(0.1, 0.1, 0.2),
(-1, -1, -2)
], ids=['int', 'int', 'bignum', 'float', 'fushu']) # 引數化通過裝飾器設定重跑次數
@pytest.mark.flaky(reruns=6, reruns_delay=2)
def test_add(a, b, result):
# cal = Calculator()
assert result == a + b
結果:
Testing started at 10:10 下午 ... /usr/local/bin/python3.6 "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target test_calc2.py::test_add Launching pytest with arguments test_calc2.py::test_add in /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest/testcode============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: rerunfailures-9.1, dependency-0.5.1, ordering-0.6, assume-2.3.2
collecting ... collected 5 itemstest_calc2.py::test_add[int0] RERUN [ 20%]
test_calc2.py::test_add[int0] RERUN [ 20%]
test_calc2.py::test_add[int0] RERUN [ 20%]
test_calc2.py::test_add[int0] RERUN [ 20%]
test_calc2.py::test_add[int0] RERUN [ 20%]
test_calc2.py::test_add[int0] RERUN [ 20%]
test_calc2.py::test_add[int0] FAILED [ 20%]
testcode/test_calc2.py:11 (test_add[int0])
3 != 2Expected :2
Actual :3
<Click to see difference>a = 1, b = 1, result = 3
@pytest.mark.parametrize(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a,b,result</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, [ (</span>1, 1, 3<span style="color: rgba(0, 0, 0, 1)">), (</span>2, 2, 4<span style="color: rgba(0, 0, 0, 1)">), (</span>100, 100, 200<span style="color: rgba(0, 0, 0, 1)">), (</span>0.1, 0.1, 0.2<span style="color: rgba(0, 0, 0, 1)">), (</span>-1, -1, -2<span style="color: rgba(0, 0, 0, 1)">) ], ids</span>=[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">int</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">int</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">bignum</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">float</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">fushu</span><span style="color: rgba(128, 0, 0, 1)">'</span>]) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 引數化</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 通過裝飾器設定重跑次數</span> @pytest.mark.flaky(reruns=6, reruns_delay=2<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> test_add(a, b, result): </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cal = Calculator()</span>
> assert result == a + b
E assert 3 == 2test_calc2.py:23: AssertionError
PASSED [ 40%]PASSED [ 60%]PASSED [ 80%]PASSED [100%]
Assertion failedAssertion failed
Assertion failed
Assertion failed
test_calc2.py::test_add[int1]
test_calc2.py::test_add[bignum]
test_calc2.py::test_add[float]
test_calc2.py::test_add[fushu]=================================== FAILURES ===================================
test_add[int0]a = 1, b = 1, result = 3
@pytest.mark.parametrize(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a,b,result</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, [ (</span>1, 1, 3<span style="color: rgba(0, 0, 0, 1)">), (</span>2, 2, 4<span style="color: rgba(0, 0, 0, 1)">), (</span>100, 100, 200<span style="color: rgba(0, 0, 0, 1)">), (</span>0.1, 0.1, 0.2<span style="color: rgba(0, 0, 0, 1)">), (</span>-1, -1, -2<span style="color: rgba(0, 0, 0, 1)">) ], ids</span>=[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">int</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">int</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">bignum</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">float</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">fushu</span><span style="color: rgba(128, 0, 0, 1)">'</span>]) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 引數化</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 通過裝飾器設定重跑次數</span> @pytest.mark.flaky(reruns=6, reruns_delay=2<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> test_add(a, b, result): </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cal = Calculator()</span>
> assert result == a + b
E assert 3 == 2test_calc2.py:23: AssertionError
=========================== short test summary info ============================
FAILED test_calc2.py::test_add[int0] - assert 3 == 2
==================== 1 failed, 4 passed, 6 rerun in 12.13s =====================Process finished with exit code 1
Assertion failed
Assertion failed
Assertion failed
Assertion failed
2、多重校驗 pytest-assume
正常情況下一條用例如果有多條斷言,一條斷言失敗了,其他斷言就不會執行了,而使用pytest-assume可以繼續執行下面的斷言
安裝 : pip installpytest-assume
執行 : pytest.assume(1==3)
for example:
#!/usr/bin/python # -*- coding: UTF-8 -*- """ @author:chenshifeng @file:test_calc2.py @time:2020/09/16 """import pytest
def test_assume():
print('登入操作')
pytest.assume(1 == 2)
print('搜尋操作')
pytest.assume(2 == 2)
print('加購操作')
pytest.assume(3 == 2)
執行結果:
Testing started at 10:23 下午 ... /usr/local/bin/python3.6 "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target test_calc2.py::test_assume Launching pytest with arguments test_calc2.py::test_assume in /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest/testcode============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: rerunfailures-9.1, dependency-0.5.1, ordering-0.6, assume-2.3.2
collecting ... collected 1 itemtest_calc2.py::test_assume FAILED [100%]登入操作
搜尋操作
加購操作testcode/test_calc2.py:11 (test_assume)
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None</span><span style="color: rgba(0, 0, 255, 1)">def</span> reraise(tp, value, tb=<span style="color: rgba(0, 0, 0, 1)">None): </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">if</span> value <span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> None: value </span>=<span style="color: rgba(0, 0, 0, 1)"> tp() </span><span style="color: rgba(0, 0, 255, 1)">if</span> value.<span style="color: rgba(128, 0, 128, 1)">__traceback__</span> <span style="color: rgba(0, 0, 255, 1)">is</span> <span style="color: rgba(0, 0, 255, 1)">not</span><span style="color: rgba(0, 0, 0, 1)"> tb:
> raise value.with_traceback(tb)
E pytest_assume.plugin.FailedAssumption:
E 2 Failed Assumptions:
E
E test_calc2.py:14: AssumptionFailure
E >> pytest.assume(1 == 2)
E AssertionError: assert False
E
E test_calc2.py:18: AssumptionFailure
E >> pytest.assume(3 == 2)
E AssertionError: assert False/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/six.py:702: FailedAssumption
Assertion failed
Assertion failed
=================================== FAILURES ===================================
test_assume _tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None
</span><span style="color: rgba(0, 0, 255, 1)">def</span> reraise(tp, value, tb=<span style="color: rgba(0, 0, 0, 1)">None): </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">if</span> value <span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> None: value </span>=<span style="color: rgba(0, 0, 0, 1)"> tp() </span><span style="color: rgba(0, 0, 255, 1)">if</span> value.<span style="color: rgba(128, 0, 128, 1)">__traceback__</span> <span style="color: rgba(0, 0, 255, 1)">is</span> <span style="color: rgba(0, 0, 255, 1)">not</span><span style="color: rgba(0, 0, 0, 1)"> tb:
> raise value.with_traceback(tb)
E pytest_assume.plugin.FailedAssumption:
E 2 Failed Assumptions:
E
E test_calc2.py:14: AssumptionFailure
E >> pytest.assume(1 == 2)
E AssertionError: assert False
E
E test_calc2.py:18: AssumptionFailure
E >> pytest.assume(3 == 2)
E AssertionError: assert False/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/six.py:702: FailedAssumption
----------------------------- Captured stdout call -----------------------------
登入操作
搜尋操作
加購操作
=========================== short test summary info
FAILED test_calc2.py::test_assume - pytest_assume.plugin.FailedAssumption:
== 1 failed in 0.09s ===============================Process finished with exit code 1
Assertion failed
Assertion failed
Assertion failed
Assertion failed
3、設定執行順序 pytest-ordering
正常情況下,用例預設執行順序是自上而下的,對於一些有上下文依賴關係的用例,可是通過pytest-ordering 來設定執行順序,當然,通過setup、teardown和fixture來解決也是可以的
安裝外掛 : pip install pytest-ordering
使用方法 : @pytest.mark.run(order=2)
需要注意的是,當有多個裝飾器的時候,可能會發生衝突(比如引數化)
For example, this:
import pytest@pytest.mark.run(order=2)
def test_foo():
assert True@pytest.mark.run(order=1)
def test_bar():
assert True
Yields this output:
============================= test session starts ============================== platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.6 cachedir: .pytest_cache rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini plugins: rerunfailures-9.1, dependency-0.5.1, ordering-0.6, assume-2.3.2 collecting ... collected 2 itemstest_ordering.py::test_bar
test_ordering.py::test_foo============================== 2 passed in 0.02s ===============================
4、用例依賴(pytest-dependency)
使用該外掛可以標記一個testcase作為其他testcase的依賴,當依賴項執行失敗時,那些依賴它的test將會被跳過。
安裝 :pip install pytest-dependency
使用方法:用 @pytest.mark.dependency()對所依賴的方法進行標記,使用@pytest.mark.dependency(depends=["test_name"])引用依賴,test_name可以是多個。
上用例:
import pytest@pytest.mark.dependency()
def test_01():
assert False@pytest.mark.dependency(depends=["test_01"])
def test_02():
print("執行測試2")
output:
=========================== short test summary info ============================ FAILED test_ordering.py::test_01 - assert False ========================= 1 failed, 1 skipped in 0.06s =========================Process finished with exit code 1
5.分散式測試(pytest-xdist)
- 平常我們功能測試用例非常多時,比如有1千條用例,假設每個用例執行需要1分鐘,如果單個測試人員執行需要1000分鐘才能跑完
- 當專案非常緊急時,會需要協調多個測試資源來把任務分成兩部分,於是執行時間縮短一半,如果有10個小夥伴,那麼執行時間就會變成十分之一,大大節省了測試時間
- 為了節省專案測試時間,10個測試同時並行測試,這就是一種分散式場景
分散式執行用例的原則:
-
-
-
- 用例之間是獨立的,沒有依賴關係,完全可以獨立執行
- 用例執行沒有順序要求,隨機順序都能正常執行
- 每個用例都能重複執行,執行結果不會影響其他用例
-
-
外掛安裝:
pip3 install pytest-xdist -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
使用方法:
pytest -n2 (2代表2個CPU)
pytest -nauto
-
- n auto:可以自動檢測到系統的CPU核數;從測試結果來看,檢測到的是邏輯處理器的數量,即假12核
- 使用auto等於利用了所有CPU來跑用例,此時CPU佔用率會特別高
6.生成報告(pytest-html)
pytest-html是一個外掛,pytest用於生成測試結果的HTML報告。相容Python 2.7,3.6
安裝外掛: pip install pytest-html
使用方法: pytest --html=report.html
轉載自https://www.cnblogs.com/feng0815/p/13682096.html