12. 店铺shop接口测试用例的编写& 环境初始化和数据清除(fixture)) 店铺shop接口测试用例的编写 环境初始化和数据清除

店铺shop接口测试用例的编写

import ptest
import os
imoport allure
from common.baseAPI import BaseAPI
from libs.login import Login
from utils.handle_path import report_path
from utils.handle_excle import get_excle_data
# 创建测试类---业务模块
'''
店铺模块的测试:初始化操作(前置条件)
  1. 首先完成有效的登录操作----拿到token
  2.完成店铺实例的创建
'''
@allure.epic('XXXX系统') # 工程级别--项目级别
@allure.feature('店铺模块')  # 业务级别
class TestShop:
  
  # 创建测试方法---对应模块具体的接口
@pytest.mark.parametrize('tile,req_body,exp_resp',get_excle_data('店铺模块','queryshopping'))
@allure.story('店铺查询')  # 接口级别
@allure.title('{title}')  # 用例标题
  def test_shop_query(self,shop_init):
    shop_object = shop_init #  创建店铺实例,可以不写
    res = shop_init.query(res_body) # 调用店铺的查询方法
     assert res['code'] == exp_resp['code']
 
# 编辑的接口
@pytest.mark.parametrize('tile,req_body,exp_resp',get_excle_data('店铺模块','updateshopping'))
@allure.story('店铺查编辑')  # 接口级别
@allure.title('{title}')  # 用例标题
def test_shop_update(self,shhop_init):
    with allure.step('1、用户登录'):
      shop_object = shop_init 
    with allure.step('2、选中编辑店铺'):
      shop_id = shop_init.qurey({'page':1,'limit':20})['data']['records'][0]['id']
    with allure.step('3、替换店铺的图片'):
      image_info= shop_init.file_upload('../data/456.png')['data']['realFileName']
    with allure.step('4、提交店铺信息'):
      res= shop_init.update(req_body,shop_id,image_infi)
    with allure.step('5、判断是否操作成功'):
      assert res['code'] == exp_resp['code']

# 编辑的接口--方式2
@pytest.mark.parametrize('tile,req_body,exp_resp',get_excle_data('店铺模块','updateshopping'))
@allure.story('店铺查编辑)  # 接口级别
@allure.title('{title}')  # 用例标题
  def test_shop_update(self,shhop_init):
    with allure.step('1、提交店铺信息'):
      res= shop_update_init['shop_object'].update(req_body,shop_update_iinit['shop_id'])
    with allure.step('2、判断是否操作成功'):
      assert res['code'] == exp_resp['code']

if __name__ == '__main__':
  pytest.main([__file__,'-s'])

环境初始化和数据清除

原始方法的环境初始化以及数据清除

def setup_class(self):  # 前置条件
    pass
def teardown_class(self): 后置条件,环境恢复
    pass

使用fixture环境初始化以及数据清除
fixture源码详解

fixture(scope='funtion', 用例对应的模块
params=None ,
autouse=False,   # 是否自动跑用例
ids=None,
name=None
):
  • scope:有四个级别的参数
    “function”(默认)----每一个函数和方法会调用
    “class”,----每一个类调用一次
    “module”----每个py文件调用一次,
    “session”(每个包)--整个自动化只运行一次,如环境检查,登录
  • params 可选参数列表,它导致多个参数调用fixture功能和所有测试使用它
    在testCase中新建conftest.py ---专门存放fixture的配置文件,用例无需导入,pytest会自己找
import pytest

@pytest.fixture(scope='seesion',autouse=False)
def start_running():
  print('------开始自动化测试运行------')
 
# ------------1、登录--------------------------
@pytest.fixture(scope='session') 
def login_init():
  print('-----开始执行登录操作----')
  token = Login().login(NAME_PWD,get_token=True)
  # reture token  # 后续操作不能执行
  yield token
  print('登录完成')

# ------------------2、店铺初始化--------------
# 有返回值的fixture的使用:如果一个fixture函数需要使用另一个fixture的返回值,直接使用他的函数名,如shop(login_init)
# 没有返回值:@pytest.mark.usefixture('函数名')
@pytest.fixture(scope='session') 
def shop_init():
  print('-----创建店铺实例操作----')
  shop_object=Shop() # 需要拿到token
  yield shop_object # 返回出去给--测试方法用

# ----------店铺更新初始化操作-------
@pytest.fixture(scope='class')
def shop_update_init(shop_init):
  shop_object = shop_init #  创建店铺实例,可以不写
  shop_id = shop_init.qurey({'page':1,'limit':20})['data']['records'][0]['id']
  image_info= shop_init.file_upload('../data/456.png')['data']['realFileName']
  shop_update = {'shop_object ':shop_object ,'shop_id':shop_id,'image_info':image_info}
  # yield shop_object,shop_id,image_info # 元组
  #yield shop_update 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章