第四种:Pytest(四)-fixture的详细使用
一.前言
1.前面一篇讲setup、teardown可以实现在执行用例前或结束后加入一些操作,但这种都是针对整个脚本全局生效的2.场景:用例1需要先登录,用例2不需要登录,用例3需要先登录。很显然无法用setup和teardown来实现2.1.fixture可以让我们自定义测试用例的前置条件二.fixture的优势
1.命名方式灵活,不局限于setup和teardown这几个命名1.1.conftest.py配置里可以实现数据共享,不需要import就能自动找到fixture1.2.scope="module"可以实现多个.py跨文件共享前置1.3.scope="session"以实现多个.py跨文件使用一个session来完成多个用例三.fixture参数列表
@pytest.fixture(scope="function",params=None,autouse=False,ids=None,name=None)deftest():print("fixture初始化的参数列表")1.参数列表1.1.scope:可以理解成fixture的作用域,默认"function",还有"class、module、package、session"四个【常用】1.2.autouse:默认"False",需要用例手动调用该fixture;如果是"True",所有作用域内的测试用例都会自动调用该fixture1.3.name:默认"装饰器的名称",同一模块的fixture相互调用建议写个不同的name1.4.session的作用域:是整个测试会话,即开始执行Pytest到结束测试四.测试用例如何调用fixture
1.将fixture名称作为测试用例函数的输入参数2.测试用例加上装饰器:@pytest.mark.usefixtures(fixture_name)3.fixture设置autouse=True# -*- coding: utf-8 -*-importpytest# 调用方式一@pytest.fixturedeflogin():print("输入账号,密码先登录")deftest_s1(login):print("用例 1:登录之后其它动作 111")deftest_s2():# 不传 loginprint("用例 2:不需要登录,操作 222")# 调用方式二@pytest.fixturedeflogin2():print("please输入账号,密码先登录")@pytest.mark.usefixtures("login2","login")deftest_s11():print("用例 11:登录之后其它动作 111")# 调用方式三@pytest.fixture(autouse=True)deflogin3():print("====auto===")# 不是test开头,加了装饰器也不会执行fixture@pytest.mark.usefixtures("login2")defloginss():print(123)4.执行结果5.知识点5.1.在类声明上面加@pytest.mark.usefixtures(),代表这个类里面所有测试用例都会调用该fixture5.2.可以叠加多个@pytest.mark.usefixtures(),先执行的放底层,后执行的放上层5.3.可以传多个fixture参数,先执行的放前面,后执行的放后面5.4.如果fixture有返回值,用 @pytest.mark.usefixtures()是无法获取到返回值的,必须用传参的方式(方式一)五.fixture的实例化顺序
1.较高scope范围的fixture(session)在较低scope范围的fixture( function 、class)之前实例化【session>package>module>class>function】2.具有相同作用域的fixture遵循测试函数中声明的顺序,并遵循fixture之间的依赖关系【在fixture_A里面依赖的fixture_B优先实例化,然后到fixture_A实例化】3.自动使用(autouse=True)的fixture将在显式使用(传参或装饰器)的fixture之前实例化# -*- coding: utf-8 -*-importpytest order=[]@pytest.fixture(scope="session")defs1():order.append("s1")@pytest.fixture(scope="module")defm1():order.append("m1")@pytest.fixturedeff1(f3,a1):# 先实例化f3, 再实例化a1, 最后实例化f1order.append("f1")assertf3==123@pytest.fixturedeff3():order.append("f3")a=123yielda@pytest.fixturedefa1():order.append("a1")@pytest.fixturedeff2():order.append("f2")deftest_order(f1,m1,f2,s1):# m1、s1在f1后,但因为scope范围大,所以会优先实例化assertorder==["s1","m1","f3","a1","f1","f2"]六.关于fixture的注意点
1.添加@pytest.fixture ,如果fixture还想依赖其他fixture,需要用函数传参的方式1.1.不能用 @pytest.mark.usefixtures()的方式,否则会不生效@pytest.fixture(scope="session")defopen():print("===打开浏览器===")@pytest.fixture# @pytest.mark.usefixtures("open") 不可取!!!不生效!!!deflogin(open):# 方法级别前置操作setupprint(f"输入账号,密码先登录{open}")2.前面讲的,其实都是setup的操作,那么现在就来讲下teardown是怎么实现的七.fixture之yield实现teardown
1.用fixture实现teardown并不是一个独立的函数,而是用yield关键字来开启teardown操作# -*- coding: utf-8 -*-importpytest@pytest.fixture(scope="session")defopen():# 会话前置操作setupprint("===打开浏览器===")test="测试变量是否返回"yieldtest# 会话后置操作teardownprint("==关闭浏览器==")@pytest.fixturedeflogin(open):# 方法级别前置操作setupprint(f"输入账号,密码先登录{open}")name="==我是账号=="pwd="==我是密码=="age="==我是年龄=="# 返回变量yieldname,pwd,age# 方法级别后置操作teardownprint("登录成功")deftest_s1(login):print("==用例1==")# 返回的是一个元组print(login)# 分别赋值给不同变量name,pwd,age=loginprint(name,pwd,age)assert"账号"innameassert"密码"inpwdassert"年龄"inagedeftest_s2(login):print("==用例2==")print(login)2.yield注意事项2.1.如果yield前面的代码,即setup部分已经抛出异常,则不会执行yield后面的teardown内容2.2.如果测试用例抛出异常,yield后面的teardown内容还是会正常执行八.yield+with的结合
@pytest.fixture(scope="module")defsmtp_connection():withsmtplib.SMTP("smtp.gmail.com",587,timeout=5)assmtp_connection:yieldsmtp_connection# provide the fixture value1.该smtp_connection连接将测试完成执行后已经关闭,因为smtp_connection对象自动关闭时,with语句结束九.addfinalizer 终结函数
@pytest.fixture(scope="module")deftest_addfinalizer(request):# 前置操作setupprint("==再次打开浏览器==")test="test_addfinalizer"deffin():# 后置操作teardownprint("==再次关闭浏览器==")request.addfinalizer(fin)# 返回前置操作的变量returntestdeftest_anthor(test_addfinalizer):print("==最新用例==",test_addfinalizer)1.注意事项1.1.如果request.addfinalizer()前面的代码,即setup部分已经抛出异常1.1.1.则不会执行request.addfinalizer()的teardown内容(和yield相似,应该是最近新版本改成一致了)1.2.可以声明多个终结函数并调用