pytest学习和使用7-fixture参数scope作用域(范围)执行顺序
  NHjqxS4aAIAZ 2023年11月02日 99 0

(7-fixture参数scope作用域(范围)执行顺序) 之前学习了fixture的基本使用,其中参数scope类似作用域,就是fixture的使用范围,那么针对scope的这几个值,他的执行顺序是怎样的?

1 scope的五个范围

作用范围
session 整个测试会话,跨文件调用
package 跨文件调用,可以跨 .py 文件
module 一个.py 执行一次,一个.py 文件可能包含多个类和方法
class 每个类都会执行一次。类中有多个方法调用,只在第一个方法调用时执行
function 每个方法(函数)都会执行一次 。.如果@pytest.fixture()里面没有参数,那么默认scope=function

2 执行顺序

  • 较高 scope 范围的fixture(session)在较低 scope 范围的fixture( function 、 class )之前执行:
【session > package > module > class > function】
  • 具有相同作用域的fixture遵循测试函数中声明的顺序,并遵循fixture之间的依赖关系;
【在test_one里面依赖的fixture_A优先执行,然后到test_one本身】
  • 使用(autouse=True)的fixture在使用传参或装饰器的fixture之前执行。
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/17 
# 文件名称:test_fixture_scope.py
# 作用:scope的范围
# 博客:https://blog.csdn.net/NoamaNelson

import pytest

@pytest.fixture(scope="function")
def case2():
    print("session执行后,再执行module,最后执行function")

@pytest.fixture(scope="session")
def case():
    print("session先执行")

@pytest.fixture(scope="module")
def case1():
    print("session执行后,再执行module")

def test_one(case2, case1, case):
    print("111111111111")

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

test_fixture_scope.py::test_one         session先执行
                                        session执行后,再执行module
                                        session执行后,再执行module,最后执行function
PASSED                                  [100%]111111111111


============================== 1 passed in 0.02s ==============================

3 多个fixture依赖

  • 添加了 @pytest.fixture ,如果fixture还想依赖其他fixture,需要用函数传参的方式;
  • 不能用 @pytest.mark.usefixtures() 的方式,否则会不生效
  • 那么我们来试试,看下效果如何?

3.1 正常调用

# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/17 
# 文件名称:test_fixtures.py
# 作用:多个fixture依赖
# 博客:https://blog.csdn.net/NoamaNelson

import pytest

@pytest.fixture()
def one():
    print("第一个fixture")

@pytest.fixture()
def two(one):
    print("第二个fixture依赖第一个fixture")

def test_case(two):
    print("测试用例依赖第一个fixture和第二个fixture")


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

test_fixtures.py::test_case           第一个fixture
                                      第二个fixture依赖第一个fixture
PASSED                                [100%]测试用例依赖第一个fixture和第二个fixture


============================== 1 passed in 0.02s ==============================

3.2 使用mark.usefixtures

# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/17 
# 文件名称:test_fixtures.py
# 作用:多个fixture依赖
# 博客:https://blog.csdn.net/NoamaNelson

import pytest

@pytest.fixture()
def one():
    print("第一个fixture")

@pytest.fixture()
def two(one):
    print("第二个fixture依赖第一个fixture")

@pytest.mark.usefixtures("one") # 没有生效
def three():
    print("多个依赖不能用mark.usefixtures===这句话之前应该不会打印‘第一个fixture’")

def test_case(two):
    print("测试用例依赖第一个fixture和第二个fixture")
    three()


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

test_fixtures.py::test_case     第一个fixture
								第二个fixture依赖第一个fixture
PASSED                          [100%]测试用例依赖第一个fixture和第二个fixture
                                多个依赖不能用mark.usefixtures===这句话之前应该不会打印‘第一个fixture’


============================== 1 passed in 0.03s ==============================
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
NHjqxS4aAIAZ
最新推荐 更多