pytest fixtures装饰器的使用和如何控制用例的执行顺序
本文详解pytest中@pytest.fixture装饰器的使用方法,包括如何通过参数传递实现测试前置初始化及数据返回,解决setup/teardown无法处理的复杂依赖场景。同时介绍利用pytest-ordering插件指定用例执行顺序的技巧,通过@pytest.mark.run(order=num)实现如登录、购物等流程的有序执行,并提供完整代码示例与运行结果分析。
pytest fixtures装饰器
在pytest框架中,可以通过使用@pytest.fixture装饰器来定义一个方法。该被装饰方法的方法名可以直接作为参数传递给其他测试函数。这种方式不仅能够在测试执行前完成必要的初始化工作,还可以将计算或获取的数据直接返回给调用它的测试函数,从而实现数据共享和状态复用。
将fixture作为函数参数
传统的测试框架通常依赖setup和teardown方法进行资源初始化和清理。然而,当面临复杂的依赖场景时,例如测试用例1需要登录状态,测试用例2不需要登录,而测试用例3又需要登录状态,传统的setup/teardown机制难以灵活处理。此时,可以使用pytest的fixture功能:在特定方法前添加@pytest.fixture装饰器,并将该方法名作为参数传入需要依赖它的测试函数中。这样,pytest会自动先执行该fixture方法(如登录操作),再执行具体的测试步骤;若测试函数未传入该参数,则直接跳过该步骤,从而实现了按需执行。
#!/usr/bin/env python
# _*_coding: utf-8 _*_
import pytest
@pytest.fixture()
def login():
print("这时一个登入的方法")
return ('tome', '123')
@pytest.fixture()
def operate():
print("这是登入后的操作")
def test_case1(login, operate):
print(login)
print("test_case1,需要登入")
def test_case2():
print("test_case2,不需要登入")
def test_case3(login):
print(login)
print("test_case3,需要登入")
在上述代码示例中,测试用例test_case1和test_case3分别将login方法名作为参数传入。pytest框架会自动识别并调用被@pytest.fixture标记的login函数。运行测试结果如下:
Testing started at 10:17 ...
C:\Python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_fixture.py
Launching pytest with arguments C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_fixture.py in C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123
============================= test session starts =============================
platform win32 -- Python 3.8.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123
plugins: html-2.1.1, metadata-1.11.0, ordering-0.6collected 3 items
test_fixture.py 这时一个登入的方法
这是登入后的操作
.('tome', '123')
test_case1,需要登入
.test_case2,不需要登入
这时一个登入的方法
.('tome', '123')
test_case3,需要登入
[100%]
============================== 3 passed in 0.04s ==============================
Process finished with exit code 0
从上述运行结果可以看出,test_case1和test_case3在执行前均先执行了login方法,输出了登录信息;而test_case2由于未传入login参数,因此未执行登录操作,直接执行了其自身的测试逻辑。
控制用例的执行顺序
默认情况下,pytest加载并执行所有测试用例的顺序是随机的。如果希望指定具体的执行顺序,可以安装并使用pytest-ordering插件。只需在测试用例方法前添加装饰器@pytest.mark.run(order=[num]),并设置order对应的num值,pytest就会按照num数值的大小顺序来依次执行测试用例。
应用场景:在某些测试场景中,必须严格指定用例的执行顺序。例如,某些业务流程需要先执行登录操作,才能继续进行后续的购物或下单流程。此时,通过pytest-ordering插件可以精确控制用例的执行顺序,确保前置依赖(如登录)先于后续操作(如购物、下单)执行。
二、安装
pip install pytest-ordering
三、实例
#!/usr/bin/env python
# _*_coding: utf-8 _*_
import pytest
class Testpytest(object):
@pytest.mark.run(order=-1)
def test_two(self):
print("test_two, 测试用例")
@pytest.mark.run(order=3)
def test_one(self):
print("test_one, 测试用例")
@pytest.mark.run(order=1)
def test_three(self):
print("test_three, 测试用例")
四、运行结果
Testing started at 15:51 ...
C:\Python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_order.py
Launching pytest with arguments C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_order.py in C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123
============================= test session starts =============================
platform win32 -- Python 3.8.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123
plugins: html-2.1.1, metadata-1.11.0, ordering-0.6collected 3 items
test_order.py [100%]
============================== 3 passed in 0.06s ==============================
Process finished with exit code 0
.test_three, 测试用例
.test_one, 测试用例
.test_two, 测试用例
以上就是pytest fixtures装饰器的使用和如何控制用例的执行顺序的详细内容,更多关于pytest fixtures装饰器和控制用例的执行顺序的资料请关注本站其它相关文章!
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









