『德不孤』Pytest框架 — 13、Pytest中Fixture装饰器(三)

9、ids参数说明

ids参数就是给每一个变量起一个别名。

示例:

import pytest

data = [("孙悟空", 666), ("猪八戒", 777), ("沙和尚", 888)]


# ids参数的个数要与params参数的个数相同,用的很少
@pytest.fixture(params=data, ids=['suk', 'zbj', 'shs'])
def need_data(request):
    return request.param


def test_data(need_data):
    print(f"测试人物:{need_data[0]}")
    print(f"测试分数:{need_data[1]}")


if __name__ == '__main__':
    pytest.main()

"""
之前的测试结果:
test_01.py::test_data[need_data0] 测试人物:孙悟空
测试分数:666
PASSED
test_01.py::test_data[need_data1] 测试人物:猪八戒
测试分数:777
PASSED
test_01.py::test_data[need_data2] 测试人物:沙和尚
测试分数:888
PASSED

加上ids参数后的执行结果:只是[]的内容变化了,这就是ids参数的作用
test_01.py::test_data[suk] 测试人物:孙悟空
测试分数:666
PASSED
test_01.py::test_data[zbj] 测试人物:猪八戒
测试分数:777
PASSED
test_01.py::test_data[shs] 测试人物:沙和尚
测试分数:888
PASSED
"""

10、name参数说明

name参数就是给Fixture修饰的方法起个别名。

示例:

import pytest

data = [("孙悟空", 666), ("猪八戒", 777), ("沙和尚", 888)]


# ids参数的个数要与params参数的个数相同,用的很少
@pytest.fixture(params=data, name="abc")
def need_data(request):
    return request.param

# 注意,如果定义了name属性,用例的方法参数就需要传入name属性定义的值。
# 否则将会报错。
# 还有用例中调用参数的使用,也需要使用name定义的别名。
# 即:当取了别名之后,那么原来的方法名就用不了了。
def test_data(abc):
    print(f"测试人物:{abc[0]}")
    print(f"测试分数:{abc[1]}")


if __name__ == '__main__':
    pytest.main()

"""
执行结果:

test_01.py::test_data[abc0] 测试人物:孙悟空
测试分数:666
PASSED
test_01.py::test_data[abc1] 测试人物:猪八戒
测试分数:777
PASSED
test_01.py::test_data[abc2] 测试人物:沙和尚
测试分数:888
PASSED
"""

11、scope参数说明

scope参数有四个级别:function(默认)classmodulesession

作用范围:session > module > class > function

  • scope="function"@pytest.fixture()装饰器如果不写参数,默认参数就是scope="function"。它的作用范围是,在每个测试用例执行的前(后)运行一次。
  • scope="class"@pytest.fixture()装饰器为class级别的时候,Fixture方法只在此class里所有用例开始执行前(后)执行一次。与class中的function没有关系。
  • scope="module"@pytest.fixture()装饰器为module级别的时候,在一个.py脚本中所有用例开始执行前(后)执行一次。
  • scope="session"@pytest.fixture()装饰器为session级别的时候,是可以跨.py模块调用的,每个.py文件就是module

(1)scope="function"

import pytest


@pytest.fixture(scope="function")
def login():
    print("打开APP")
    print("输入账号,密码,进行登录")
    yield  # 当用例执行完成后,执行yield后的代码
    print("关闭APP")


def test_add_cart(login):
    print("添加购物车--需要登录")


def test_browser_goods():
    print("浏览商品--不需要登录")


if __name__ == '__main__':
    pytest.main()


"""
执行结果: 只作用域方法或者函数范围

test_01.py::test_add_cart 打开APP
输入账号,密码,进行登录
添加购物车--需要登录
PASSED关闭APP

test_01.py::test_browser_goods 浏览商品--不需要登录
PASSED
"""

(2)scope="class"

import pytest


@pytest.fixture(scope="class")
def login():
    print("\nscope=class 作用域的前置函数\n")
    yield  # 当用例执行完成后,执行yield后的代码
    print("\n\nscope=class 作用域的后置函数")


class Test_Demo:

    def test_add_cart(self, login):
        print("添加购物车--测试用例")

    def test_add_address(self):
        print("添加收货地址--测试用例")


if __name__ == '__main__':
    pytest.main()

"""
执行结果: 只在测试类开始前后执行了

scope=class 作用域的前置函数

添加购物车--测试用例
PASSED
test_01.py::Test_Demo::test_add_address 添加收货地址--测试用例
PASSED

scope=class 作用域的后置函数
"""

(3)scope="module"

import pytest


@pytest.fixture(scope="module")
def login():
    print("\nscope=module 作用域的前置函数\n")
    yield  # 当用例执行完成后,执行yield后的代码
    print("\n\nscope=module 作用域的后置函数")

# 如果该用例不传入fixture方法,该用例会优先与fixture前置执行
def test_browser_goods(login):
    print("浏览商品--不需要登录")

class Test_Demo:

    def test_add_cart(self, login):
        print("添加购物车--测试用例")

    def test_add_address(self):
        print("添加收货地址--测试用例")

class Test_Demo2:

    def test_add_cart(self, login):
        print("第2次-添加购物车--测试用例")

    def test_add_address(self):
        print("第2次-添加收货地址--测试用例")


if __name__ == '__main__':
    pytest.main()

"""
执行结果:
说明一下:scope=module时,整个.py模块中fixture只执行一次

scope=module 作用域的前置函数

浏览商品--不需要登录
PASSED
test_01.py::Test_Demo::test_add_cart 添加购物车--测试用例
PASSED
test_01.py::Test_Demo::test_add_address 添加收货地址--测试用例
PASSED
test_01.py::Test_Demo2::test_add_cart 第2次-添加购物车--测试用例
PASSED
test_01.py::Test_Demo2::test_add_address 第2次-添加收货地址--测试用例
PASSED

scope=module 作用域的后置函数

"""

(4)scope="session"

test_01.py文件中的用例。

import pytest

@pytest.fixture(scope="session")
def login():
    print("\nscope=session 作用域的前置函数\n")
    yield  # 当用例执行完成后,执行yield后的代码
    print("\n\nscope=session 作用域的后置函数")

# 如果该用例不传入fixture方法,该用例会优先与fixture前置执行
def test_browser_goods(login):
    print("浏览商品--不需要登录")

class Test_Demo:

    def test_add_cart(self, login):
        print("添加购物车--测试用例")

    def test_add_address(self):
        print("添加收货地址--测试用例")


if __name__ == '__main__':
    pytest.main()

test_02.py文件中的用例。

# 如果该用例不传入fixture方法,该用例会优先与fixture前置执行
def test_browser_goods():
    print("第2次-浏览商品--不需要登录")

class Test_Demo2:

    def test_add_cart(self):
        print("第2次-添加购物车--测试用例")

    def test_add_address(self):
        print("第2次-添加收货地址--测试用例")

all.py文件内容:

import pytest

if __name__ == '__main__':
    pytest.main()

"""
执行结果:只在该包内执行一次fixture前后置

scope=session 作用域的前置函数

浏览商品--不需要登录
PASSED
test_01.py::Test_Demo::test_add_cart 添加购物车--测试用例
PASSED
test_01.py::Test_Demo::test_add_address 添加收货地址--测试用例
PASSED
test_02.py::test_browser_goods 第2次-浏览商品--不需要登录
PASSED
test_02.py::Test_Demo2::test_add_cart 第2次-添加购物车--测试用例
PASSED
test_02.py::Test_Demo2::test_add_address 第2次-添加收货地址--测试用例
PASSED

scope=session 作用域的后置函数
"""

说明:

我把session作用域的Fixture写在了test_01.py文件中,其实应该提取出来写到conftest.py文件中。(后面文章会说明)

all.py文件就是为了执行所有测试用例,pytest.ini文件中配置的模糊匹配查询,这样两个文件中的用例就都能执行到了。

这里就是一个练习,写的十分不规范,理解其意思就可以。

12、autouse参数说明

示例:实全部用例的前后置

很简单,只需要把Fixture中的autouse参数设置为True即可。

Fixture装饰器会自动执行作用域范围内的所有用例的前后置。

import pytest

@pytest.fixture(autouse=True)
def login():
    print("输入账号,密码,进行登录")

def test_add_cart():  # 不需要传入fixture方法
    print("添加购物车--需要登录")

def test_add_address():  # 不需要传入fixture方法
    print("添加收货地址--需要登录")


if __name__ == '__main__':
    pytest.main()

"""
执行结果:

输入账号,密码,进行登录
添加购物车--需要登录
PASSED输入账号,密码,进行登录
添加收货地址--需要登录
PASSED
"""
posted @ 2022-03-13 23:07  繁华似锦Fighting  阅读(317)  评论(0编辑  收藏  举报