目錄
1、pytest-html(生成html報告)
1.1、安裝
1.2、操作引數
1.3、報告優化(報錯截圖)
1.4、報告優化(用例描述+報錯截圖)
2、pytest-repeat(重復執行用例)
2.1、安裝
2.2、操作引數
2.3、兼容性
3、pytest-ordering(用例執行順序)
3.1、安裝
3.2、pytest默認執行
3.3、pytest-ordering自定義用例順序
4、pytest-assume(多重斷言)
4.1、安裝
4.2、assert多重斷言
4.3、pytest-assume多重斷言
4.4、背景關系管理器
1、pytest-html(生成html報告)
1.1、安裝
在命令列中運行以下命令進行安裝:
pip install pytest-html
或者(使用國內的豆瓣源,資料會定期同步國外官網,速度快,)
pip install pytest-html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
1.2、操作引數
命令列引數:
-
--html=report.html(普通HTML報告,CSS是獨立的,分享報告的時候樣式會丟,)
-
--html=report.html --self-contained-html(合并CSS的HTML報告,分享報告樣式不丟失,如:分享發郵件展示報告,)
創建test_html.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
def func(abc):
return abc + 1
def test_case3():
assert func(3) == 5
class TestClass:
def test_case1(self):
x = "AllTests"
assert "t" in x
def test_case2(self):
y = "hello"
assert "h" in y
1.2.1、HTML報告(普通)
打開命令列,在該腳本目錄下,輸入執行命令:
pytest test_html.py --html=report.html

執行完成后,在當前目錄下自動創建一個report.html的測驗報告,

打開測驗報告:

1.2.2、HTML報告(合并CSS)
打開命令列,在該腳本目錄下,輸入執行命令:
pytest test_html.py --html=report.html --self-contained-html

執行完成后,在當前目錄下自動創建一個report.html的測驗報告,

打開測驗報告:

1.3、報告優化(報錯截圖)
在執行web自動化的時候,希望測驗用例失敗時,以截圖的方式展示在html報告里,
1、修改conftest.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
from selenium import webdriver
import pytest
driver = None
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
當測驗失敗的時候,自動截圖,展示到html報告中
"""
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call' or report.when == "setup":
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
file_name = report.nodeid.replace("::", "_")+".png"
screen_img = _capture_screenshot()
if file_name:
html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:600px;height:300px;" ' \
'onclick="window.open(this.src)" align="right"/></div>' % screen_img
extra.append(pytest_html.extras.html(html))
report.extra = extra
def _capture_screenshot():
"""
截圖保存為base64,展示到html中
"""
return driver.get_screenshot_as_base64()
@pytest.fixture(scope='session', autouse=True)
def browser(request):
global driver
if driver is None:
driver = webdriver.Chrome()
def end():
driver.quit()
request.addfinalizer(end)
return driver
2、創建test_html_screenshot.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
from selenium import webdriver
import time
def test_case(browser):
browser.get("https://www.baidu.com/")
time.sleep(2)
t = browser.title
assert t == "AllTests軟體測驗"
3、打開命令列,在該腳本目錄下,輸入執行命令:
pytest test_html_screenshot.py --html=report.html --self-contained-html
4、運行結果:

將失敗截圖展示在報告里

1.4、報告優化(用例描述+報錯截圖)
pytest-html 測驗報告默認是不展示用例描述 Description 內容,可以修改生成的報告內容,添加或洗掉 html 報告的 table 內容,
1、修改conftest.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
from selenium import webdriver
from py.xml import html
import pytest
driver = None
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
當測驗失敗的時候,自動截圖,展示到html報告中
"""
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call' or report.when == "setup":
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
file_name = report.nodeid.replace("::", "_")+".png"
screen_img = _capture_screenshot()
if file_name:
html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:600px;height:300px;" ' \
'onclick="window.open(this.src)" align="right"/></div>' % screen_img
extra.append(pytest_html.extras.html(html))
report.extra = extra
report.description = str(item.function.__doc__)
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
cells.insert(1, html.th('Description'))
@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
cells.insert(1, html.td(report.description))
def _capture_screenshot():
"""
截圖保存為base64,展示到html中
"""
return driver.get_screenshot_as_base64()
@pytest.fixture(scope='session', autouse=True)
def browser(request):
global driver
if driver is None:
driver = webdriver.Chrome()
def end():
driver.quit()
request.addfinalizer(end)
return driver
2、創建test_html_screenshot_description.py檔案
測驗用例下三個引號里面的注釋(docstring)內容就是測驗報告展示的Description內容;如果沒有注釋內容,報告Description內容顯示None,
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
from selenium import webdriver
import time
def test_case01(browser):
"""
測驗用例1 失敗用例
"""
browser.get("https://www.baidu.com/")
time.sleep(2)
t = browser.title
assert t == "AllTests軟體測驗"
def test_case02(browser):
"""
測驗用例2 成功用例
"""
browser.get("https://www.baidu.com/")
time.sleep(2)
t = browser.title
assert t == "百度一下,你就知道"
3、打開命令列,在該腳本目錄下,輸入執行命令:
pytest test_html_screenshot_description.py --html=report.html --self-contained-html
4、運行結果:

用例描述顯示在報告的Description內,并且將失敗截圖展示在報告里,

2、pytest-repeat(重復執行用例)
2.1、安裝
在命令列中運行以下命令進行安裝:
pip install pytest-repeat
或者(使用國內的豆瓣源,資料會定期同步國外官網,速度快,)
pip install pytest-repeat -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
2.2、操作引數
1、命令列引數:
--count=n(運行n次一個或多個測驗用例)
或者(兩種方式皆可,等號或空格)
--count n
--repeat-scope(可以覆寫默認的測驗用例執行順序,類似 fixture 的 scope 引數)
(1)function:默認值,范圍針對每個用例重復執行,再執行下一個用例,
(2)class:以類為用例集合單位,重復執行類里面的用例,再執行下一個,
(3)module:以模塊為單位,重復執行模塊里面的用例,再執行下一個,
(4)session:重復整個測驗會話,即所有測驗用例執行一次,然后再次執行所有此類測驗,
-x(如果您嘗試診斷間歇性故障,則一次又一次地運行相同的測驗直到失敗,將很有用,您可以將 pytest 的 -x 選項與 pytest-repeat 結合使用,以強制測驗運行器在第一次失敗時停止,)
例如:
pytest --count=1000 -x test_file.py
這將嘗試運行test_file.py 1000次,但將在發生故障后立即停止,
2、裝飾器引數(如果要在代碼中將某些測驗用例標記為執行重復多次,可以使用此裝飾器,):
@pytest.mark.repeat(count)
2.2.1、重復執行(命令列)
1、創建test_repeat.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
def test_case3():
assert 123 == 123
class TestClass:
def test_case1(self):
x = "AllTests"
assert "t" in x
def test_case2(self):
y = "hello"
assert "h" in y
2、運行結果:
打開命令列,在該腳本目錄下,輸入執行命令:
pytest --count=2 test_repeat.py
3條用例,每條用例執行2次,所以總共執行6次用例,

2.2.2、重復執行(裝飾器@pytest.mark.repeat(count))
如果要在代碼中標記要重復多次的測驗,可以使用 @pytest.mark.repeat(count) 裝飾器,
1、創建test_repeat2.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
@pytest.mark.repeat(5)
def test_case():
print("\n執行測驗用例")
2、運行結果:
打開命令列,在該腳本目錄下,輸入執行命令:
pytest -s test_repeat2.py
用例執行5次,

2.2.3、重復執行(執行順序-class)
1、創建test_repeat3.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
class Test_class1:
def test_case1(self):
print("執行測驗用例test_case1")
class Test_class2:
def test_case2(self):
print("執行測驗用例test_case2")
2、運行結果:
打開命令列,在該腳本目錄下,輸入執行命令:
pytest -s --count=2 --repeat-scope=class test_repeat3.py
重復執行類里面的用例,再執行下一個類里面的用例,

2.2.4、重復執行(執行順序-module)
1、創建test_repeat4.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
def test_case1():
print("執行測驗用例test_case1")
def test_case2():
print("執行測驗用例test_case2")
class Test_class1:
def test_case3(self):
print("執行測驗用例test_case3")
2、運行結果:
打開命令列,在該腳本目錄下,輸入執行命令:
pytest -s --count=2 --repeat-scope=module test_repeat4.py

2.2.5、重復執行直到失敗
1、創建test_repeat5.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import random
def test_case():
flag = random.choice([True, False])
print(flag)
assert flag
2、運行結果:
打開命令列,在該腳本目錄下,輸入執行命令:
pytest -s --count=10 -x test_repeat5.py
嘗試執行10次,運行第4次失敗,立即停止,

2.3、兼容性
pytest-repeat 不能與 unittest.TestCase 測驗類一起使用,無論 --count 設定多少,這些測驗始終僅運行一次,并顯示警告,
3、pytest-ordering(用例執行順序)
pytest-ordering 插件可以控制用例的執行順序,
3.1、安裝
在命令列中運行以下命令進行安裝:
pip install pytest-ordering
或者(使用國內的豆瓣源,資料會定期同步國外官網,速度快,)
pip install pytest-ordering -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
3.2、pytest默認執行
1、創建test_ordering.py檔案
pytest默認的執行順序(用例先后順序執行)
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
def test_case2():
print("用例test_case2")
assert True
def test_case1():
print("用例test_case1")
assert True
def test_case3():
print("用例test_case3")
assert True
2、運行結果:
打開命令列,在該腳本目錄下,輸入執行命令:
pytest -s test_ordering.py
按照用例順序,先執行test_case2,之后執行test_case1,最后執行test_case3,

3.3、pytest-ordering自定義用例順序
1、修改test_ordering.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
@pytest.mark.run(order=2)
def test_case2():
print("用例test_case2")
assert True
@pytest.mark.run(order=1)
def test_case1():
print("用例test_case1")
assert True
@pytest.mark.run(order=3)
def test_case3():
print("用例test_case3")
assert True
2、運行結果:
打開命令列,在該腳本目錄下,輸入執行命令:
pytest -s test_ordering.py
使用pytest-ordering自定義用例順序,先執行test_case1,之后執行test_case2,最后執行test_case3,

4、pytest-assume(多重斷言)
pytest 中可以用 python 的 assert 斷言,也可以寫多個斷言,但是如果一個失敗,那么后面的斷言將不再執行,
此時可以用 pytest-assume 插件,每個測驗允許多次失敗,
4.1、安裝
在命令列中運行以下命令進行安裝:
pip install pytest-assume
或者(使用國內的豆瓣源,資料會定期同步國外官網,速度快,)
pip install pytest-assume -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
4.2、assert多重斷言
1、創建test_assert.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
def test_addcase():
assert 1 + 2 == 3
assert 1 + 3 == 4
assert 1 + 4 == 6
assert 1 + 5 == 6
print("測驗完成")
2、運行結果:
第三個斷言(assert 1 + 4 == 6)失敗之后,后面的斷言也不會執行,包括正常的代碼,

4.3、pytest-assume多重斷言
1、創建test_assume.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
def test_addcase():
pytest.assume(1 + 2 == 3)
pytest.assume(1 + 3 == 4)
pytest.assume(1 + 4 == 6)
pytest.assume(1 + 5 == 6)
print("測驗完成")
2、運行結果:
第三個斷言(pytest.assume(1 + 4 == 6))失敗之后,后面的斷言還是會執行的,

4.4、背景關系管理器
pytest.assume 也可以用作圍繞普通斷言的背景關系管理器,
1、創建test_assume2.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
from pytest import assume
@pytest.mark.parametrize(('x', 'y'), [(1, 1), (1, 0), (0, 1)])
def test_simple_assume(x, y):
with assume: assert x == y
with assume: assert True
with assume: assert False
2、運行結果:


注意:每個 with 塊只能有一個斷言,如果有多個斷言,當第一個失敗了,則不會完全驗證之后的所有斷言,
1、創建test_assume3.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
@pytest.mark.parametrize(('x', 'y'), [(1, 1)])
def test_simple_assume(x, y):
with pytest.assume:
assert x != y
assert x == y
2、運行結果:



轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342067.html
標籤:其他
下一篇:軟體測驗之軟體配置項測驗
