如何通过pytest fixtures装饰器灵活控制用例执行顺序和资源管理?

2026-04-20 09:322阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1020个文字,预计阅读时间需要5分钟。

如何通过pytest fixtures装饰器灵活控制用例执行顺序和资源管理?

pytest fixtures 装饰器 + pytest 中可使用 @pytest.fixture 装饰器来装饰一个方法,将其作为一个固定值传递到测试方法中。被装饰的方法名称可以作为参数传入测试方法中,以实现测试前的初始化工作。这种用法可完成测试前的初始化,也方便实现一测多例。

如何通过pytest fixtures装饰器灵活控制用例执行顺序和资源管理?

pytest fixtures装饰器

pytest中可以使用@pytest.fixture 装饰器来装饰一个方法,被装饰方法的方法名可以作为一个参数传入到测试方法中。可以使用这种方式来完成测试之前的初始化,也可以返回数据给测试函数。

将fixture作为函数参数

通常使用setup和teardown来进行资源的初始化,如果有这样一个场景,测试用例1需要依赖登入功能,测试用例2不需要依赖登入功能,测试用例3需要登入功能,这种场景setup,teardown无法实现,也可以使用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没有执行这个方法。

控制用例的执行顺序

一、pytest加载所有的用例都是乱序的,如果想指定用例的顺序,可以使用pytest-ordering插件,指定用例的执行顺序只需要在测试用例的方法前面加上装饰器@pytest.mark.run(order=[num])设置order的对应的num值,它就可以按照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装饰器和控制用例的执行顺序的资料请关注易盾网络其它相关文章!

标签:使用

本文共计1020个文字,预计阅读时间需要5分钟。

如何通过pytest fixtures装饰器灵活控制用例执行顺序和资源管理?

pytest fixtures 装饰器 + pytest 中可使用 @pytest.fixture 装饰器来装饰一个方法,将其作为一个固定值传递到测试方法中。被装饰的方法名称可以作为参数传入测试方法中,以实现测试前的初始化工作。这种用法可完成测试前的初始化,也方便实现一测多例。

如何通过pytest fixtures装饰器灵活控制用例执行顺序和资源管理?

pytest fixtures装饰器

pytest中可以使用@pytest.fixture 装饰器来装饰一个方法,被装饰方法的方法名可以作为一个参数传入到测试方法中。可以使用这种方式来完成测试之前的初始化,也可以返回数据给测试函数。

将fixture作为函数参数

通常使用setup和teardown来进行资源的初始化,如果有这样一个场景,测试用例1需要依赖登入功能,测试用例2不需要依赖登入功能,测试用例3需要登入功能,这种场景setup,teardown无法实现,也可以使用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没有执行这个方法。

控制用例的执行顺序

一、pytest加载所有的用例都是乱序的,如果想指定用例的顺序,可以使用pytest-ordering插件,指定用例的执行顺序只需要在测试用例的方法前面加上装饰器@pytest.mark.run(order=[num])设置order的对应的num值,它就可以按照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装饰器和控制用例的执行顺序的资料请关注易盾网络其它相关文章!

标签:使用