首页 > 其他 > 详细

pytest之fixture的使用

时间:2021-04-21 16:05:59      阅读:23      评论:0      收藏:0      [点我收藏+]

fixture的用途

pytest fixture 与setup,teardown功能一样,但比之更加灵活,完全可以代替setup,teardown

1.做测试前后的初始化设置,如测试数据准备,链接数据库,打开浏览器等这些操作都可以使用fixture来实现

2.测试用例的前置条件可以使用fixture实现

3.支持经典的xunit fixture ,像unittest使用的setup和teardown

4.fixture可以实现unittest不能实现的功能,比如unittest中的测试用例和测试用例之间是无法传递参数和数据的,但是fixture却可以解决这个问题。

fixture的定义

fixture通过@pytest.fixture()装饰器装饰一个函数,那么这个函数就是一个fixture

 

import pytest


@pytest.fixture()
def login():
    print(‘登陆方法‘)
    yield   #激活fixture teardown方法
    print(‘teardown‘)

# 测试用例之前,先执行login方法
def test_case1(login):
    print(‘case1‘)

def test_case2():
    print(‘case2‘)

def test_case3():
    print(‘case3‘

  

运行结果如下:

test_login.py::test_case2
test_login.py::test_case3

============================== 3 passed in 0.02s ===============================

Process finished with exit code 0
登陆方法
PASSED [ 33%]case1
teardown
PASSED [ 66%]case2
PASSED [100%]case3

fixture源码详解
fixture(scope=‘function‘,params=None,autouse=False,ids=None,name=None):
1
scope:有四个级别参数"function"(默认),“class”,“module”,“session”。

**params:**一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它。

**autouse:**如果True,则为所有测试激活fixture func可以看到它。如果为False则显示需要参考来激活fixture。

**ids:**每个字符串id的列表,每个字符串对应于params这样他们就是测试ID的一部分。如果没有提供ID它们将从params自动生成。

name:fixture的名称。这默认为装饰函数的名称。如果fixture在定义它的统一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽,解决这个问题的一种方法时将装饰函数命。

fixture的作用范围(scope)
fixture里面有个scope参数可以控制fixture的作用范围:session > module > class > function

function 每一个函数或方法都会调用,默认为function
class 每一个类调用一次,一个类可以有多个方法
module,每一个.py文件调用一次,该文件内又有多个function和class
session 是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module
1、scope=‘function’


import pytest

@pytest.fixture() #默认为scope=‘function‘
def login():
print(‘登陆方法‘)
yield [‘username‘,‘passwd‘] #激活fixture teardown方法
print(‘teardown‘)

# 测试用例之前,先执行login方法
def test_case1(login):
print(f‘case1 login={login}‘)

def test_case2(login):
print(‘case2‘)

def test_case3(login):
print(‘case3‘)

运行结果如下:

============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
collecting ... collected 3 items

test_login.py::test_case1
test_login.py::test_case2
test_login.py::test_case3

============================== 3 passed in 0.02s ===============================

Process finished with exit code 0
登陆方法
PASSED [ 33%]case1 login=[‘username‘, ‘passwd‘]
teardown
登陆方法
PASSED [ 66%]case2
teardown
登陆方法
PASSED [100%]case3
teardown

2、scope=‘module’

import pytest


@pytest.fixture(scope=‘module‘) #默认为scope=‘function‘
def login():
print(‘登陆方法‘)
yield [‘username‘,‘passwd‘] #激活fixture teardown方法
print(‘teardown‘)

# 测试用例之前,先执行login方法
def test_case1(login):
print(f‘case1 login={login}‘)

def test_case2(login):
print(‘case2‘)

def test_case3(login):
print(‘case3‘)

运行结果如下:

============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
collecting ... collected 3 items

test_login.py::test_case1
test_login.py::test_case2
test_login.py::test_case3

============================== 3 passed in 0.02s ===============================

Process finished with exit code 0
登陆方法
PASSED [ 33%]case1 login=[‘username‘, ‘passwd‘]
PASSED [ 66%]case2
PASSED [100%]case3
teardown

fixture自动调用(autouse=True)
autouse设置为True,自动调用fixture功能,无需额外继承

import pytest


@pytest.fixture(autouse=True) # 默认为scope=‘function‘
def login():
print(‘登陆方法‘)
yield [‘username‘, ‘passwd‘] # 激活fixture teardown方法
print(‘teardown‘)

def test_search1(): #无需继承login
print(‘搜索用例1‘)

def test_search2():
print(‘搜索用例2‘)

运行结果如下:

============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
collecting ... collected 2 items

test_search.py::test_search1 登陆方法
PASSED [ 50%]搜索用例1
teardown

test_search.py::test_search2 登陆方法
PASSED [100%]搜索用例2
teardown


============================== 2 passed in 0.01s ===============================

Process finished with exit code 0

fixture参数化(params)
@pytest.fixture有一个params参数,接受一个列表,列表中每个数据都可以作为用例的输入。也就说有多少数据,就会形成多少用例。可以通过’’‘request.param’’‘来获取该次调用的参数。

import pytest


@pytest.fixture(params=[‘user1‘, ‘user2‘, ‘user3‘])
def login(request):
print(‘登陆方法‘)
print(‘传入的参数为:‘+request.param) # 获取params参数
yield [‘username‘, ‘passwd‘] # 激活fixture teardown方法
print(‘teardown‘)

# 测试用例之前,先执行login方法
def test_case1(login):
print(f‘case1 login={login}‘)

def test_case2(login):
print(‘case2‘)

def test_case3(login):
print(‘case3‘)

运行结果如下:

============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
collecting ... collected 9 items

test_login.py::test_case1[user1]
test_login.py::test_case1[user2]
test_login.py::test_case1[user3]
test_login.py::test_case2[user1]
test_login.py::test_case2[user2]
test_login.py::test_case2[user3]
test_login.py::test_case3[user1] 登陆方法
传入的参数为:user1
PASSED [ 11%]case1 login=[‘username‘, ‘passwd‘]
teardown
登陆方法
传入的参数为:user2
PASSED [ 22%]case1 login=[‘username‘, ‘passwd‘]
teardown
登陆方法
传入的参数为:user3
PASSED [ 33%]case1 login=[‘username‘, ‘passwd‘]
teardown
登陆方法
传入的参数为:user1
PASSED [ 44%]case2
teardown
登陆方法
传入的参数为:user2
PASSED [ 55%]case2
teardown
登陆方法
传入的参数为:user3
PASSED [ 66%]case2
teardown
登陆方法
传入的参数为:user1
PASSED [ 77%]case3
teardown

test_login.py::test_case3[user2] 登陆方法
传入的参数为:user2
PASSED [ 88%]case3
teardown

test_login.py::test_case3[user3] 登陆方法
传入的参数为:user3
PASSED [100%]case3
teardown


============================== 9 passed in 0.06s ===============================

Process finished with exit code 0

参数化与fixture结合(indirect=True)

import pytest


@pytest.fixture(params=[‘user1‘, ‘user2‘, ‘user3‘])
def login(request):
print(‘登陆方法‘)
print(‘传入的参数为:‘ + str(request.param)) # 获取params参数
yield [‘username‘, ‘passwd‘] # 激活fixture teardown方法
print(‘teardown‘)


# 参数化结合fixture使用
# 情况一:传入值和数据
# 情况二:传入一个fixture方法,将数据传入到fixture方法中,fixture使用request参数来接受这组数据,在方法体中使用request.param来接受这个数据
@pytest.mark.parametrize(‘login‘, [
(‘username1‘, ‘passwd1‘),
(‘username2‘, ‘passwd2‘)
], indirect=True)
def test_cart3(login):
print(‘购物车用例3‘)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
运行结果如下:

============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
collecting ... collected 2 items

test_cart.py::test_cart3[login0]
test_cart.py::test_cart3[login1]

============================== 2 passed in 0.02s ===============================

Process finished with exit code 0
登陆方法
传入的参数为:(‘username1‘, ‘passwd1‘)
PASSED [ 50%]购物车用例3
teardown
登陆方法
传入的参数为:(‘username2‘, ‘passwd2‘)
PASSED [100%]购物车用例3
teardown

conftest.py
1.conftest.py文件名字是固定的,不可以做任何修改

2.文件和用例文件在同一个目录下,那么conftest.py作用于整个目录

3.conftest.py文件不能被其他文件导入

4.所有同目录测试文件运行前都会执行conftest.py文件
————————————————
版权声明:本文为CSDN博主「「已注销」」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_55977000/article/details/115272431

pytest之fixture的使用

原文:https://www.cnblogs.com/zhangchaorun/p/14684383.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!