《一》Pytest之fixture介绍
(1)Fixtures定义了构成测试准备阶段的步骤和数据,可以通过用修饰器@pytest.fixture来告诉pytest某个特定函数是一个fixture
(2)实例
<-----pytest_fixtures > test_quick_exam.py----->
import pytest
@pytest.fixture
def first_fix():
return ['a']
def test_string(first_fix):
first_fix.append('b')
print(first_fix)
def test_case2(first_fix):
first_fix.append('c')
print(first_fix)
if __name__ == '__main__':
pytest.main(['-s'])
<-----pytest_fixtures > test_quick_exam_by_hand.py----->
def first_fix():
return ['a']
def test_string(varA):
varA.append('b')
print(varA)
entry = first_fix()
test_string(entry)
(3)小贴士:编码错误解决方法(在首行加: #coding=gbk)
《二》fixture调用fixture
<-----pytest_fixtures > test_append.py----->
import pytest
@pytest.fixture
def first_fix():
return 'a'
@pytest.fixture
def order(first_fix):
return [first_fix]
def test_string(order):
order.append('b')
print(order)
if __name__ == '__main__':
pytest.main(['-s','test_append.py'])
《三》使用多个fixture
<-----pytest_fixtures > test_multi_fixtures01----->
import pytest
@pytest.fixture()
def test1():
a = 'zz'
b = '123456'
print('传出a,b')
return(a,b)
def test2(test1):
username = test1[0]
passwd = test1[1]
print('通过fixture的元祖取出用户名和密码')
print(username)
print(passwd)
if __name__ == '__main__':
pytest.main(['-s','test_multi_fixtures01.py'])
<-----pytest_fixtures > test_multi_fixtures02----->
import pytest
@pytest.fixture()
def test1():
a = 'zz'
print('\n传出a')
return a
@pytest.fixture()
def test2():
b = '123456'
print('\n传出b')
return b
def test3(test1,test2):
username = test1
passwd = test2
print('传入多个fix')
print(username)
print(passwd)
if __name__ == '__main__':
pytest.main(['-s','test_multi_fixtures02.py'])
《四》使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例,可让用例引用指定的fixture
pytest_fixtures > test_usefixtures.py
import pytest
@pytest.fixture()
def fix1():
print('\n引入fix1,开始执行function')
@pytest.fixture()
def fix2():
print('\n引入fix2,开始执行function')
@pytest.mark.usefixtures('fix1')
def test_1():
print('-----用例1执行-----')
# 1.需要前面标记了fix、fix2函数,这里才可以使用
# 2.前面标记了fix2函数,如果不引用的话,执行后
# 不会执行fix2函数
@pytest.mark.usefixtures('fix1')
class TestCase():
def test_2(self):
print('-----用例2执行-----')
def test_3(self):
print('-----用例3执行-----')
if __name__ == '__main__':
pytest.main(['-s', 'test_usefixtures.py'])
《五》conftest.py
<-----test_conftest > conftest.py----->
import pytest
@pytest.fixture()
def fix1():
print('\n开始执行fix1')
<-----test_conftest > test_case01.py----->
import pytest
def test_case01(fix1):
print('用例一')
if __name__ == '__main__':
pytest.main(['-s', 'test_case01.py'])
《六》conftest+fixture+yield实现用例前置后置(yield 之前 用例前置 yield xxx 返回信息给用例 yield 之后 用例后置)
<-----test_conftest_yied > conftest.py----->
import pytest
@pytest.fixture()
def fix1():
print('\n开始执行fix1')
yield
print('\n结束执行fix1')
<-----test_conftest_yied > test_case01.py----->
import pytest
def test_case01(fix1):
print('用例一')
if __name__ == '__main__':
pytest.main(['-s', 'test_case01.py'])
《七》python中yield关键字的用法详解(yield: return yield: return + generator 的一部分 带有yield的函数才是真正的generator)
<-----test_yield > test01.py----->
'''
yiled的实现原理:
1.程序开始执行以后,因为test 函数中有yield关键字
所以test函数并不会真正的执行,而是先得到一个生成器g
2.直到调用next方法,test函数正式开始执行。先执行test函数中的print方法
然后进入while循环
3.程序遇到yield关键字,然后把yield想成return,return了一个8之后,程序停止,
并没有执行赋值给a操作,此时next(g)语句执行完成,所以输出前面两行(第一行是while
上面的print的结果,第二行是retrun出来的结果)
4.程序执行print(*****)
5.又开始执行下面的print(next(g)),这个时候是从刚才next程序停止的地方开始执行,
也就是要执行a的赋值操作,这个时候因为赋值操作的右边是没有值的,已经被return出去了
这个时候a的值是none,所以接下来输出的是a: None
6.程序会继续在while里执行,又一次碰到yiled,这个时候同样return出8,然后程序停止。
print函数输出的8就是这次return出的8
'''
def test():
print('begin...')
while True:
a = yield 8
print('a:', a)
g = test()
print(next(g))
print('****************')
print(next(g))
<-----test_yield > test02.py----->
"""
1.程序执行g.send(7),程序会从yield关键字那一行继续往下运行,send会把7这个值
复制给a变量
2.由于send方法中包含next()方法,所以程序会继续往下运行执行Print方法,然后再次进入
while循环
3.程序再次遇到yield关键字,yield会返回后面的值后,程序再次暂停,直到再次调用
next方法或send方法
"""
def test():
print('begin...')
while True:
a = yield 8
print('a:', a)
g = test()
print(next(g))
print('****************')
print(g.send(7))
<-----test_yield > test03.py----->
def a(max):
n = 1
L = []
while n < max:
L.append(n)
n = n + 1
return L
for n in a(6):
print(n)
def test(num):
print("begin...")
while num<5:
num=num+1
yield num
for n in test(0):
print(n)
for n in range(6):
a=n
print(a)
print(type(range(6)))
《八》Pytest淘宝电商项目实战-从零开始编码
20211123_37 Fixture高阶技术(test009>test0093) > pytest_practive_V6
key_word:
keyword_web.py
logic:
bd_search.py
tb_search.py
page:
allpages.py
pageObject:
baidu_page.py
taobao_page.py
test_case:
test_kw.py
test_pom.py
test_pom_v2
test_pytest.py
test_selenium.py
人生苦短,及时行乐
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现