标签:-- py fixture pytest test Pytest06 print 固件 def
pytest测试固件
测试固件也叫测试夹具,用于指定初始化代码或清理代码/扫尾工作
fixture
fixture修饰器来标记固定的工厂函数,在其他函数,模块,类或整个工程调用它时会被激活并优先执行,通常会被用于完成预置处理和重复操作
使用方法:
# 导入pytest
import pytest
# 定义初始化函数
@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)
def 初始化函数():
初始化代码
常用参数:
scope:被标记方法的作用域
params:(list类型)提供参数数据,供调用标记方法的函数使用
autouse:是否自动运行,默认为False不运行,设置为True自动运行
# 引用初始化函数
def 测试函数(初始化函数名):
fixture优点
firture相对于setup和teardown来说应该有以下几点优势:
命名方式灵活,不局限于setup和teardown这几个命名
conftest.py 配置里可以实现数据共享,不需要import就能自动找到一些配置
scope=”module” 可以实现多个.py跨文件共享前置
scope=”session” 以实现多个.py跨文件使用一个session来完成多个用例
通过参数引用
在project_p2 中 test_a.py
import pytest
class Test_ABC:
@pytest.fixture()
def before(self):
print("------->before")
def test_a(self, before): # ️ test_a方法传入了被fixture标识的函数,已变量的形式
print("------->test_a")
assert 1
if __name__ == '__main__':
pytest.main("-s test_abc.py")
"""
执行结果:
test_a.py
------->before # 发现before会优先于测试函数运行
------->test_a
"""
通过函数引用
在project_p2 中 test_b.py
import pytest
@pytest.fixture() # fixture标记的函数可以应用于测试类外部
def before():
print("------->before")
@pytest.mark.usefixtures("before")
class Test_ABC:
def setup(self):
print("------->setup")
def test_a(self):
print("------->test_a")
assert 1
if __name__ == '__main__':
pytest.main("-s test_b.py")
"""
执行结果:
test_abc.py
------->before # 发现before会优先于测试类运行
------->setup
------->test_a
"""
默认设置为运行
在project_p2 中 test_c.py
import pytest
@pytest.fixture(autouse=True) # 设置为默认运行
def before():
print("------->before")
class Test_ABC:
def setup(self):
print("------->setup")
def test_a(self):
print("------->test_a")
assert 1
if __name__ == '__main__':
pytest.main("-s test_c.py")
"""
执行结果:
test_abc.py
------->before # 发现before自动优先于测试类运行
------->setup
------->test_a
"""
fixture设置作用域
测试固件可以全部放到pytest的配置文件conftest.py中,也可以将function和class范围的固件放到测试模块中,module和session放在conftest中
@pytest.fixture(scope=?)
scope="function"
每个测试函数或方法之前都运行,默认值,省略scope='function'时,括号可同时省略
scope="class"
每个测试函数之前运行一次,每个class之前只运行一次
scope="module"
每个module的所有测试方法之前只运行一次
scope="session"
每个session只运行一次,多个py文件只调用一次,约束或控制多个py文件
fixure修饰一个或多个测试函数或方法
在test_demo05.py文件中
import pytest
@pytest.fixture(scope='module')
def pre_module():
print('----------->pre_module每个测试模块前做')
@pytest.fixture(scope='class')
def pre_class():
print('----------->pre_class每个测试类前做')
@pytest.fixture(scope='function')
def pre_function():
print('----------->pre_function每个测试函数或方法前做')
def test_3(pre_module): # 第1个测试函数中的pre_module勿省
print('test3')
class Test_Case:
def test_1(self, pre_class, pre_function): # 类中第1个测试方法pre_class勿省
print('test1')
def test_2(self, pre_function): # pre_class可省
print('test2')
if __name__=='__main__':
pytest.main('-s test_demo05.py')
scope="session"
要写到conftest.py文件里
conftest.py文件名是固定的,pytest会自动识别该文件
一个项目下可以建多个conftest.py的文件,一般在项目根目录下设置的conftest文件起到全局作用,对同目录或子目录中的*test*.py文件都起作用。在不同子目录下也可以放conftest.py的文件,作用范围只能在该层级以及子目录中起作用
第一要执行的py文件中第一个要执行的测试函数或测试方法中应指定要使用的fixture名称,也可以所有测试函数或方法都指定
conftest.py与运行的用例要在同一个包下,并且有init.py文件
使用session范围的测试固件
创建project_p2项目并创建conftest文件以及两个测试文件
conftest.py
import pytest
@pytest.fixture(scope='session')
def pre_session():
print('在所有测试文件前只执行一次的代码pre_session')
@pytest.fixture(scope='module')
def pre_module():
print('pre_module')
test1.py
def test11(pre_session,pre_module):
print('测试函数test11')
def test12():
print('测试函数test12')
test2.py
class Test22:
def test221(self,pre_session,pre_module):
print('测试方法test221')
def test222(self):
print('测试方法test222')
执行测试
同时执行两个py文件,可以在cmd中在文件py文件所在目录下执行命令
pytest -s test1.py test2.py
文件执行按照书写顺序进行
fixture自动使用
@pytest.fixture(scope=?, autouse=True)
当用例很多的时候,每次都传这个参数,会很麻烦
fixture中的参数autouse,默认是False
autouse设置为True,自动调用fixture功能
使用scope=class时,每个测试函数都会自动使用其所修饰的初始化函数
yield
yield实现用例执行完之后清除数据(或还原)操作
fixture通过scope参数控制setup级别,既然有setup作为用例之前前的操作
用例执行完之后那肯定也有teardown操作。fixture的teardown操作并不是独立的函数,用yield关键字呼唤teardown操作
yield是一个关键字,它不是单独存在的,要写在fixtrue标记的固件中
test_demo06.py
import pytest
@pytest.fixture(scope="function")
def login():
print("登录成功")
yield
print("用例执行完成,收尾")
def test1(login):
print('操作1')
print("-----------------------------------------------")
def test2(login):
print('操作2')
print("-----------------------------------------------")
def test3(login):
print('操作3')
print("-----------------------------------------------")
if __name__ == "__main__":
pytest.main(["-s", "test_demo06.py"])
从结果看出,虽然test1,test2,test3三个地方都调用了login函数,并且它会在每一个用例前执行一次
标签:--,py,fixture,pytest,test,Pytest06,print,固件,def 来源: https://www.cnblogs.com/sean-test/p/15543948.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。