一、 fixture
1. fixture 详解
fixture(scope='function',params=None,autouse=False,ids=None,name=None)
scope:有四个级别参数”function”(默认),”class”,”module”,”session”params:一个可选的参数列表,它将导致多个参数调用 fixture 功能和所有测试使用它。autouse:如果 True,则为所有测试激活 fixture func 可以看到它。如果为 False 则显示需要参考来激活 fixtureids:每个字符串 id 的列表,每个字符串对应于 params 这样他们就是测试 ID 的一部分。如果没有提供 ID 它们将从 params 自动生成name:fixture 的名称。这默认为装饰函数的名称。如果 fixture 在定义它的统一模块中使用,夹具的功能名称将被请求夹具的功能 arg 遮蔽,解决这个问题的一种方法时将装饰函数命
2. fixture 作用范围
scope 参数可以控制 fixture 的作用范围:
session > module > class > function
function:每一个函数或方法都会调用class:每一个类调用一次,一个类中可以有多个方法module:每一个.py 文件调用一次,该文件内又有多个 function 和 classsession:是多个文件调用一次,可以跨 .py 文件调用,每个 .py 文件就是 module
3. 调用 fixture 的三种方法
1. 函数或类里面方法直接传 fixture 的函数参数名称
1 2 3 4 5 6
   | @pytest.fixture() def test1(): 	print('\n开始执行function')
  def test_a(test1): 	print('---用例a执行---')
   | 
2. 使用 装饰器 @pytest.mark.usefixtures() 修饰 需要运行的用例
1 2 3 4 5 6 7 8 9
   | import pytest
  @pytest.fixture() def test1(): 	print('\n开始执行function')
  @pytest.mark.usefixtures('test1') def test_a(): 	print('---用例a执行---')
   | 
3. 叠加 usefixtures
如果一个方法或者一个 class 用例想要勇士调用多个 fixture
可以使用 @pytest.mark.usefixture() 进行叠加
注意叠加顺序:先执行的放底层,后执行的放上层
1 2 3 4 5 6 7 8 9 10 11 12
   | @pytest.fixture() def test1(): 	print('\n开始执行function1')
  @pytest.fixture() def test2(): 	print('\n开始执行function2')
  @pytest.mark.usefixtures('test1') @pytest.mark.usefixtures('test2') def test_a(): 	print('---用例a执行---')
   | 
4. usefixtures 和 fixture 区别
装饰器 usefixture 与用例直接传 fixture 参数的区别 :如果 fixture 有返回值,那么 usefixture 无法获取到返回
值
fixture 需要用到 return 出来的参数时,只能将参数名称直接当参数传入,
不需要用到 return 出来的参数时,两种方式都可以。
autouse 设置为 True,自动调用 fixture 功能
二、fixture 的使用
创建 conftest.py 文件,所有初始化内容写在里边。
1. 直接使用
1 2 3 4 5 6 7 8 9
   | @pytest.fixture(scope='session', autouse=True)   def start_demo(request):       print('---开始执行自动化测试---')
           def fin():         print('---自动化测试---结束')
      request.addfinalizer(fin)
   | 
2. 手动调用 —- 无 return 传参
1 2 3 4 5 6
   | 
  @pytest.fixture(scope='function') def update_shop_init():            print('---我的作用是商铺更新的初始化操作---')
 
  | 
1 2 3 4 5 6 7 8 9
   | 
  @pytest.mark.usefixtures('update_shop_init')   @pytest.mark.parametrize('inData,respData', get_excelData2('我的商铺', 'updateshopping')) def test_shop_update(self, inData, respData, update_shop_init):       res = MyShop(self.token).shop_update(inData, update_shop_init[0], update_shop_init[1])            print('update_shop_init:--->', update_shop_init[0], update_shop_init[1])     assert res['code'] == respData['code']
 
  | 
3. 手动调用 —- 有 return 传参
1 2 3 4 5 6 7 8 9 10 11 12 13
   | 
  @pytest.fixture(scope='function') def update_shop_init():            print('---我的作用是商铺更新的初始化操作---')          token = Login().login({"username": "sq0001", "password": "123456"}, getToken=True)          shopId = MyShop(token).shop_list({'page': 1, 'limit': 20})['data']['records'][0]['id']          imageInfo = MyShop(token).file_upload('123.png', '../data/123.png')     return shopId, imageInfo  
 
  | 
1 2 3 4 5 6 7 8 9
   | 
 
  @pytest.mark.parametrize('inData,respData', get_excelData2('我的商铺', 'updateshopping')) def test_shop_update(self, inData, respData, update_shop_init):       res = MyShop(self.token).shop_update(inData, update_shop_init[0], update_shop_init[1])            print('update_shop_init:--->', update_shop_init[0], update_shop_init[1])     assert res['code'] == respData['code']
 
  |