Pytest系列(4) - fixture的详细使用

若是你还想从头学起Pytest,能够看看这个系列的文章哦!html

https://www.cnblogs.com/poloyy/category/1690628.htmlpython

 

前言

  • 前面一篇讲了setup、teardown能够实如今执行用例前或结束后加入一些操做,但这种都是针对整个脚本全局生效的
  • 若是有如下场景:用例 1 须要先登陆,用例 2 不须要登陆,用例 3 须要先登陆。很显然没法用 setup 和 teardown 来实现了
  • fixture可让咱们自定义测试用例的前置条件

 

fixture的优点

  • 命名方式灵活,不局限于 setup 和teardown 这几个命名
  • conftest.py 配置里能够实现数据共享,不须要 import 就能自动找到fixture
  • scope="module" 能够实现多个.py 跨文件共享前置
  • scope="session" 以实现多个.py 跨文件使用一个 session 来完成多个用例
 

fixture参数列表

@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)
def test():
    print("fixture初始化的参数列表")

参数列表

  • scope:能够理解成fixture的做用域,默认:function,还有class、module、package、session四个【经常使用】
  • autouse:默认:False,须要用例手动调用该fixture;若是是True,全部做用域内的测试用例都会自动调用该fixture
  • name:默认:装饰器的名称,同一模块的fixture相互调用建议写个不一样的name

注意

session的做用域:是整个测试会话,即开始执行pytest到结束测试浏览器

 

测试用例如何调用fixture

  1. 将fixture名称做为测试用例函数的输入参数
  2. 测试用例加上装饰器:@pytest.mark.usefixtures(fixture_name)
  3. fixture设置autouse=True
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  =
__Time__   = 2020-04-06 15:50
__Author__ = 小菠萝测试笔记
__Blog__   = https://www.cnblogs.com/poloyy/
"""
import pytest

# 调用方式一
@pytest.fixture
def login():
    print("输入帐号,密码先登陆")


def test_s1(login):
    print("用例 1:登陆以后其它动做 111")


def test_s2():  # 不传 login
    print("用例 2:不须要登陆,操做 222")


# 调用方式二
@pytest.fixture
def login2():
    print("please输入帐号,密码先登陆")


@pytest.mark.usefixtures("login2", "login")
def test_s11():
    print("用例 11:登陆以后其它动做 111")


# 调用方式三
@pytest.fixture(autouse=True)
def login3():
    print("====auto===")


# 不是test开头,加了装饰器也不会执行fixture
@pytest.mark.usefixtures("login2")
def loginss():
    print(123)

执行结果

 

fixture的实例化顺序

  • 较高 scope 范围的fixture(session在较低 scope 范围的fixture( function 、 class 以前实例化【session > package > module > class > function】
  • 具备相同做用域的fixture遵循测试函数中声明的顺序,并遵循fixture之间的依赖关系【在fixture_A里面依赖的fixture_B优先实例化,而后到fixture_A实例化】
  • 自动使用(autouse=True)的fixture将在显式使用(传参或装饰器)的fixture以前实例化
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  =
__Time__   = 2020-04-06 16:14
__Author__ = 小菠萝测试笔记
__Blog__   = https://www.cnblogs.com/poloyy/
"""
import pytest

order = []

@pytest.fixture(scope="session")
def s1():
    order.append("s1")


@pytest.fixture(scope="module")
def m1():
    order.append("m1")


@pytest.fixture
def f1(f3, a1):
    # 先实例化f3, 再实例化a1, 最后实例化f1
    order.append("f1")
    assert f3 == 123


@pytest.fixture
def f3():
    order.append("f3")
    a = 123
    yield a


@pytest.fixture
def a1():
    order.append("a1")


@pytest.fixture
def f2():
    order.append("f2")


def test_order(f1, m1, f2, s1):
    # m一、s1在f1后,但由于scope范围大,因此会优先实例化
    assert order == ["s1", "m1", "f3", "a1", "f1", "f2"]

执行结果

断言成功session

 

关于fixture的注意点

添加了 @pytest.fixture ,若是fixture还想依赖其余fixture,须要用函数传参的方式,不能用 @pytest.mark.usefixtures() 的方式,不然会不生效app

@pytest.fixture(scope="session")
def open():
    print("===打开浏览器===")

@pytest.fixture
# @pytest.mark.usefixtures("open") 不可取!!!不生效!!!
def login(open):
    # 方法级别前置操做setup
    print(f"输入帐号,密码先登陆{open}")

 

前面讲的,其实都是setup的操做,那么如今就来说下teardown是怎么实现的ide

 

fixture之yield实现teardown

用fixture实现teardown并非一个独立的函数,而是用 yield 关键字来开启teardown操做函数

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  =
__Time__   = 2020-04-06 15:50
__Author__ = 小菠萝测试笔记
__Blog__   = https://www.cnblogs.com/poloyy/
"""

import pytest


@pytest.fixture(scope="session")
def open():
    # 会话前置操做setup
    print("===打开浏览器===")
    test = "测试变量是否返回"
    yield test
    # 会话后置操做teardown
    print("==关闭浏览器==")


@pytest.fixture
def login(open):
    # 方法级别前置操做setup
    print(f"输入帐号,密码先登陆{open}")
    name = "==我是帐号=="
    pwd = "==我是密码=="
    age = "==我是年龄=="
    # 返回变量
    yield name, pwd, age
    # 方法级别后置操做teardown
    print("登陆成功")


def test_s1(login):
    print("==用例1==")
    # 返回的是一个元组
    print(login)
    # 分别赋值给不一样变量
    name, pwd, age = login
    print(name, pwd, age)
    assert "帐号" in name
    assert "密码" in pwd
    assert "年龄" in age


def test_s2(login):
    print("==用例2==")
    print(login)

yield注意事项

  • 若是yield前面的代码,即setup部分已经抛出异常了,则不会执行yield后面的teardown内容
  • 若是测试用例抛出异常,yield后面的teardown内容仍是会正常执行

 

yield+with的结合

# 官方例子
@pytest.fixture(scope="module")
def smtp_connection():
    with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp_connection:
        yield smtp_connection  # provide the fixture value

该 smtp_connection 链接将测试完成执行后已经关闭,由于 smtp_connection 对象自动关闭时, with 语句结束。测试

 

addfinalizer 终结函数

@pytest.fixture(scope="module")
def test_addfinalizer(request):
    # 前置操做setup
    print("==再次打开浏览器==")
    test = "test_addfinalizer"

    def fin():
        # 后置操做teardown
        print("==再次关闭浏览器==")

    request.addfinalizer(fin)
    # 返回前置操做的变量
    return test


def test_anthor(test_addfinalizer):
    print("==最新用例==", test_addfinalizer)

注意事项

  • 若是 request.addfinalizer() 前面的代码,即setup部分已经抛出异常了,则不会执行 request.addfinalizer() 的teardown内容(和yield类似,应该是最近新版本改为一致了)
  • 能够声明多个终结函数并调用
相关文章
相关标签/搜索