pytest-fixture

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

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

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

给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

如果未指定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

给fixture重命名

autouse=True,该作用域内的函数都运行该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

比如,在测试运行前在数据库插入数据,在测试用例执行完成后删除该条记录等
在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后面的代码都会执行
可以用作案例执行前的创建和销毁动作

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

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

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

Original: https://blog.csdn.net/ljsykf/article/details/122952139
Author: 爱吃水饺的小京
Title: pytest-fixture

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/776070/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球