• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pytest-fixture

武飞扬头像
爱吃水饺的小京
帮助2

fixture的作用

fixture的作用是将一些非核心测试逻辑(如测试数据的检索和生成)从测试函数中分离出来,以便于其他测试函数复用,同时保持这些边缘逻辑的一致性
fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集,配置测试前系统的初始状态,为批量测试提供数据等

fixture的定义

如何定义一个fixture?
pytest使用装饰器@pytest.fixture(scope,params,autouse,ids,name)来声明这个函数是一个fixture

scope:

可选参数,指定作用范围,参数有四个可选值:function,class,module,sesson,默认是function
当scope为function时,每个测试函数只需运行一次
当为class时,每个测试类只需要运行一次
为module时,每个模块只需要运行一次
为session时,每次会话只需要运行一次,在每次会话的前后运行

params

给fixture传入参数,fixture本质是一个函数,但是又无法通过正常的方式传参
使用request.param能够在fixture函数内部获取到这个参数

@pytest.fixture(params=[{"name":"test1"},{"name":"test2"},{"name":"test3"}])
def my_fixture_one(request):
    return request.param["name"]

def test_my_fixture(my_fixture_one):
    print(my_fixture_one)

params是一个List,request.param可以获取到列表的元素
request是一个pytest内置的fixture
以上运行结果:

C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py"
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: E:\JenkinsLearn\My-pytest\test, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 3 items

test_my_add.py::test_my_fixture[my_fixture_one0] test1
PASSED
test_my_add.py::test_my_fixture[my_fixture_one1] test2
PASSED
test_my_add.py::test_my_fixture[my_fixture_one2] test3
PASSED

ids

如果未指定id,pytest使用fixture名加一串数字作为案例的标识
id可以是一个列表,也可以是一个函数


@pytest.fixture(params=[{"name":"test1"},{"name":"test2"},{"name":"test3"}],ids=["first","second","third"])
def my_fixture_one(request):
    return request.param["name"]

def test_my_fixture(my_fixture_one):
    print(my_fixture_one)

运行结果:

C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py"
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: E:\JenkinsLearn\My-pytest\test, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 3 items

test_my_add.py::test_my_fixture[first] test1
PASSED
test_my_add.py::test_my_fixture[second] test2
PASSED
test_my_add.py::test_my_fixture[third] test3
PASSED

name

给fixture重命名

autouse

autouse=True,该作用域内的函数都运行该fixture

fixture用法举例

传递数据

@pytest.fixture()
def my_fixture_two():
    return "a"

def test_my_fixture_two(my_fixture_two):
    lst1=["b"]
    lst1.append(my_fixture_two)
    print(lst1)

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: E:\JenkinsLearn\My-pytest\test, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 1 item

test_my_add.py::test_my_fixture_two ['b', 'a']
PASSED

在案例运行前后执行setup和teardown工作

比如,在测试运行前在数据库插入数据,在测试用例执行完成后删除该条记录等
在fixture中使用yield关键字,yield也可以返回数值

@pytest.fixture()
def my_fixture_three():
    print("测试案例执行前的动作")
    yield
    print("测试案例执行后的动作")

def test_my_fixture_three(my_fixture_three):
    print("测试用例执行中。。。。")

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: E:\JenkinsLearn\My-pytest\test, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 1 item

test_my_add.py::test_my_fixture_three 测试案例执行前的动作
测试用例执行中。。。。
PASSED测试案例执行后的动作

由此可见,fixture在测试案例执行之前就已经执行了,如果遇到yield,开始执行测试用例,如果测试用例有返回值,可以使用yield返回,当测试用例执行完毕后,再执行yield后的代码,无论测试过程中发生了什么,yield后面的代码都会执行
可以用作案例执行前的创建和销毁动作

fixture可以使用其他fixture

通过conftest.py文件可以共享fixture

放在conftest.py里的fixture可以共享

使用多个fixture

一个测试用例可以使用多个fixture

类使用fixture

如果一个类需要使用fixture,需要使用装饰器@pytest.mark.usefixture(‘fixture1’,‘fixture2’)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcfeag
系列文章
更多 icon
同类精品
更多 icon
继续加载