Pytest和Allure测试框架-超详细版+实战5

五,第三方插件

1,调整测试用例的执行顺序

Pytest和Allure测试框架-超详细版+实战5_第1张图片
p1

场景:未考虑按自然顺序执行时,或想变更执行顺序,比如增加 数据的用例要先执行,再执行删除的用例。测试用例默认是按名 称顺序执行的。
• 解决:
• 安装:pip install pytest-ordering
• 在测试方法上加下面装饰器
•@pytest.mark.last    —最后一个执行
• @pytest.mark.run(order=1)—第几个执行
pytest默认按字母顺序去执行的

import pytest
@pytest.mark.run(order=1)
def test_01():
    print('test01')

@pytest.mark.run(order=2)
def test_02():
    print('test01')
@pytest.mark.last
def test_06():
    print('test01')

def test_04():
    print('test01')

def test_05():
    print('test01')
@pytest.mark.run(order=3)
def test_03():
    print('test01')

pytest_order.py::test_01 PASSED [ 16%]test01
pytest_order.py::test_02 PASSED [ 33%]test01
pytest_order.py::test_03 PASSED [ 50%]test01
pytest_order.py::test_04 PASSED [ 66%]test01
pytest_order.py::test_05 PASSED [ 83%]test01
pytest_order.py::test_06 PASSED [100%]test01

2, 执行用例遇到错误停止

在这里插入图片描述

• 正常全部执行完成后才能停止,如果想遇到错误时停止测试: -x;也可以当用例错误个数n达到指定数量时,停止测试:- - maxfail=n
• 执行:
• pytest -x -v -s 文件名.py      ------- -x是遇到错误就停止
• pytest -x -v -s 文件名.py —maxfail=2  ------- --maxfail=2 是遇到两个错误就停止

3,执行用例失败后重新运行

Pytest和Allure测试框架-超详细版+实战5_第2张图片
在这里插入图片描述

**场景:
• 测试失败后要重新运行n次,要在重新运行之间添加延迟时 间,间隔n秒再运行。
• 执行:
• 安装:pip install pytest-rerunfailures
• pytest -v - -reruns 5 --reruns-delay 1 —每次等1秒 重试5次

4,多条断言前面报错后面依然执行

Pytest和Allure测试框架-超详细版+实战5_第3张图片
在这里插入图片描述

pip3 install pytest-assume 断言后继续执行,但要修改断言**

@pytest.mark.parametrize(('x', 'y'), [(1, 1), (1, 0), (0, 1)])
def test_assume(x, y):
    pytest.assume(x == y)
    pytest.assume(3 == 4)
    pytest.assume(5 == 9)

5,多线程并行与分布式执行

在这里插入图片描述

场景:测试用例1000条,一个用例执行1钟,一个测试人员执行需要1000分 钟。通常我们会用人力成本换取时间成本,加几个人一起执行,时间就会缩
短。如果10人一起执行只需要100分钟,这就是一种并行测试,分布式场景。
解决:pytest分布式执行插件:pytest-xdist,多个CPU或主机执行
前提:用例之间都是独立的,没有先后顺序,随机都能执行,可重复运行不 影响其他用例。
安装:Pip3 install pytest-xdist
• 多个CPU并行执行用例,直接加-n 3是并行数量:pytest -n 3 • 在多个终端下一起执行

import pytest
import time

@pytest.mark.parametrize('x',list(range(10)))
def test_somethins(x):
    time.sleep(1)

pytest -v -s -n 5 test_xsdist.py  ----一次执行5个

运行以下代码,项目结构如下

web_conf_py是项目工程名称

│  conftest.py
│  __init__.py
│              
├─baidu
│  │  conftest.py
│  │  test_1_baidu.py
│  │  test_2.py
│  │  __init__.py 
│          
├─blog
│  │  conftest.py
│  │  test_2_blog.py
│  │  __init__.py  

代码参考:

# web_conf_py/conftest.py
import pytest

@pytest.fixture(scope="session")
def start():
    print("\n打开首页")
    return "yoyo"

# web_conf_py/baidu/conftest.py
import pytest

@pytest.fixture(scope="session")
def open_baidu():
    print("打开百度页面_session")

# web_conf_py/baidu/test_1_baidu.py
import pytest
import time

def test_01(start, open_baidu):
    print("测试用例test_01")
    time.sleep(1)
    assert start == "yoyo"

def test_02(start, open_baidu):
    print("测试用例test_02")
    time.sleep(1)
    assert start == "yoyo"

if __name__ == "__main__":
    pytest.main(["-s", "test_1_baidu.py"])


# web_conf_py/baidu/test_2.py
import pytest
import time

def test_06(start, open_baidu):
    print("测试用例test_01")
    time.sleep(1)
    assert start == "yoyo"
def test_07(start, open_baidu):
    print("测试用例test_02")
    time.sleep(1)
    assert start == "yoyo"

if __name__ == "__main__":
    pytest.main(["-s", "test_2.py"])


# web_conf_py/blog/conftest.py
import pytest

@pytest.fixture(scope="function")
def open_blog():
    print("打开blog页面_function")

# web_conf_py/blog/test_2_blog.py

import pytest
import time
def test_03(start, open_blog):
    print("测试用例test_03")
    time.sleep(1)
    assert start == "yoyo"

def test_04(start, open_blog):
    print("测试用例test_04")
    time.sleep(1)
    assert start == "yoyo"

def test_05(start, open_blog):
    '''跨模块调用baidu模块下的conftest'''
    print("测试用例test_05,跨模块调用baidu")
    time.sleep(1)
    assert start == "yoyo"

if __name__ == "__main__":
    pytest.main(["-s", "test_2_blog.py"])

正常运行需要消耗时间:7.12 seconds

E:\YOYO\web_conf_py>pytest
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: xdist-1.23.2, metadata-1.7.0, html-1.19.0, forked-0.2
collected 7 items

baidu\test_1_baidu.py ..                                                 [ 28%]
baidu\test_2.py ..                                                       [ 57%]
blog\test_2_blog.py ...                                                  [100%]

========================== 7 passed in 7.12 seconds ===========================

设置并行运行数量为3,消耗时间:3.64 seconds,大大的缩短了用例时间

E:\YOYO\web_conf_py>pytest -n 3
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: xdist-1.23.2, metadata-1.7.0, html-1.19.0, forked-0.2
gw0 [7] / gw1 [7] / gw2 [7]
scheduling tests via LoadScheduling
.......                                                                  [100%]
========================== 7 passed in 3.64 seconds ==========================

6,其他有意思的插件

在这里插入图片描述

这里就不多说了,喜欢的可以自己研究下

7,使用pytest执行unittest的测试用例

Pytest和Allure测试框架-超详细版+实战5_第4张图片
在这里插入图片描述

执行unitest就和原来一样,尽量不要混合使用搞那些花里胡哨的,用哪个就哪个,就不多说了

8,pytest-html生成报告

Pytest和Allure测试框架-超详细版+实战5_第5张图片
在这里插入图片描述

pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告。兼容Python 2.7,3.6

pytest-html
1.github上源码地址【https://github.com/pytest-dev/pytest-html】
2.pip安装
pytest --html=report.html
html报告
1.打开cmd,cd到需要执行pytest用例的目录,执行指令:pytest --html=report.html
2.执行完之后,在当前目录会生成一个report.html的报告文件,显示效果如下

Pytest和Allure测试框架-超详细版+实战5_第6张图片
图片.png

指定报告路径

1.直接执行"pytest --html=report.html"生成的报告会在当前脚本的同一路径,如果想指定报告的存放位置,放到当前脚本的同一目录下的report文件夹里


Pytest和Allure测试框架-超详细版+实战5_第7张图片
在这里插入图片描述

2.如果想指定执行某个.py文件用例或者某个文件夹里面的所有用例,需加个参数。具体规则参考【pytest文档2-用例运行规则】

Pytest和Allure测试框架-超详细版+实战5_第8张图片
在这里插入图片描述

报告独立显示
1.上面方法生成的报告,css是独立的,分享报告的时候样式会丢失,为了更好的分享发邮件展示报告,可以把css样式合并到html里

$ pytest --html=report.html --self-contained-html

显示选项
默认情况下,“ 结果”表中的所有行都将被展开,但具测试通过的行除外Passed。

可以使用查询参数自定义此行为:?collapsed=Passed,XFailed,Skipped。

更多功能
1.更多功能查看官方文档【https://github.com/pytest-dev/pytest-html】


参考链接
https://blog.csdn.net/qq_42610167/article/details/101204066

你可能感兴趣的:(Pytest和Allure测试框架-超详细版+实战5)