前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pytest(十四)用例执行顺序

Pytest(十四)用例执行顺序

作者头像
雷子
发布2022-04-06 08:38:27
1.5K0
发布2022-04-06 08:38:27
举报
文章被收录于专栏:雷子说测试开发

系列分享

Pytest系列(一)初次了解

Pytest(二)执行规则以及编写执行多条用例

Pytest(三)Pytest执行命令

Pytest(四)Pytest断言

Pytest(五)标记函数

Pytest(六)跳过测试

Pytest(七) pytest之参数化

Pytest(八) pytest Fixture(一)

Pytest(九) pytest Fixture(二)

Pytest(十) pytest Fixture(三)

Pytest(十一) pytest ini文件

Pytest(十二) Pytest产生测试报告

Pytest(十三)durations统计用例运行时间


在很多人执行用例的时候呢,都会想要调整下用例的执行顺序。在pytest的原生的顺序已经满足不了我们的需求的时候呢,我们可以用pytest-ordering来控制我们的执行的顺序。

安装

代码语言:javascript
复制
pip install pytest-ordering

使用

代码语言:javascript
复制
@pytest.mark.run(order=3)
def test_01():
    print("test_01")
    time.sleep(1.0)

@pytest.mark.run(order=2)
def test_two():
    print("test_two")
    time.sleep(10)

@pytest.mark.run(order=1)
def test_regin():
    print("用例test_regin")
    time.sleep(1.5)


def test_login():
    print("用例login")
    time.sleep(0.1)


def test_05():
    print("用例5")
    time.sleep(2.3)

执行顺序是否

可以看到,我们增加了顺序的,按照规定的数据执行,没有的,按照pytest的默认顺序执行了。

当然还可以去自定义

比如在conftest.py配置如下

代码语言:javascript
复制
# conftest.py
import pytest

def pytest_collection_modifyitems(config, items):
    """ 根据指定的mark参数场景,动态选择case的执行顺序"""
    for item in items:
        scenarios = [
            marker for marker in item.own_markers
            if marker.name.startswith('scenarios')
            and marker.name in config.option.markexpr
        ] 
        if len(scenarios) == 1 and not item.get_closest_marker('run'):
           item.add_marker(pytest.mark.run(order=scenarios[0].args[0]))

可以根据mark参数场景动态选择case执行顺序

使用

代码语言:javascript
复制
import time

import pytest


@pytest.mark.run(order=3)
def test_01():
    print("test_01")
    time.sleep(1.0)

@pytest.mark.run(order=2)
def test_two():
    print("test_two")
    time.sleep(10)

@pytest.mark.run(order=1)
def test_regin():
    print("用例test_regin")
    time.sleep(1.5)

@pytest.mark.scenarios_1(2)
def test_login():
    print("用例login")
    time.sleep(0.1)

@pytest.mark.scenarios_1(1)
def test_05():
    print("用例5")
    time.sleep(2.3)

这个时候,需要在pytest.ini配置下

代码语言:javascript
复制
[pytest]
markers=scenarios_1

执行

代码语言:javascript
复制
pytest one.py -m scenarios_1 -v -s
结果如下

正常情况下,使用自带的即可以满足测试需求。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-03-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 雷子说测试开发 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档