• 13.接口自动化学习-Pytest结合Yaml使用


    问题:项目自动化测试脚本迭代出现变革技术方案
    要求:测试用例从excel–变为yaml用例
    注意事项:
    1)尽可能少改代码
    2)新技术方案yaml读取,尽可能写成一样的数据返回
    [(请求体1,响应数据1),(请求体2,响应数据2)]
    3)给pytest框架使用

    • handel_yaml.py
    #安装环境yaml库 pip install pyYaml
    import yaml
    
    # -------获取yaml测试用例------
    """
    问题:项目自动化测试脚本迭代出现变革技术方案
    要求:测试用例从excel--变为yaml用例
    注意事项:
    1)尽可能少改代码
    2)新技术方案yaml读取,尽可能写成一样的数据返回
    [(请求体1,响应数据1),(请求体2,响应数据2)]
    3)给pytest框架使用
    """
    def get_yamlCase_data(fileDir):
        resList=[]
        with open(fileDir,encoding='utf-8') as fo:
            res=yaml.safe_load(fo.read())
            print(res)
            for one in res:
                resList.append((one['title'],one['data'],one['resp']))
            return resList
    
    if __name__ == '__main__':v
        resList=get_yamlCase_data('../data/loginCase.yaml')
        print(resList)
       
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • loginCase.yaml
    #-
    #  url: &url1 /account/sLogin
    #  method: &method1 POST
    -
      title: 用户名正确,密码正确
      data:
        username: zz088
        password: "123456"
      resp:
        code: 20000
        msg: 登录成功
    -
      title: 用户名正确,密码为空
      data:
        username: zz088
        password: ""
      resp:
        code: 9999
        msg: 用户名或密码错误
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • test_login.py yaml代替excel
    import pytest,allure,os
    from libs.login import Login
    from utils.handle_excel import get_excel_data
    from utils.handle_yaml import get_yamlCase_data
    from utils.handle_path import report_path,data_path
    from common.baseApi import BaseAssert
    #TestLogin继承BaseAssert
    
    @allure.epic('项目名称-外卖项目')
    @allure.feature('登录模块')
    class TestLogin(BaseAssert):
        @pytest.mark.parametrize('title,inBody,expData', get_yamlCase_data( data_path+'\\loginCase.yaml'))
        # @pytest.mark.parametrize('inBody,expData,title', get_excel_data( '登录模块', 'Login','请求参数','响应预期结果','标题'))
        @allure.story('登录接口')
        @allure.title("{title}")
        def test_login(self,title,inBody,expData):
            # 1.调用业务层封装的接口代码
            res=Login().login(inBody)
            print(res['msg'],expData['msg'])
            # 2.断言实际返回结果与预期结果
            self.define_assert(res['msg'],expData['msg'])
    
    if __name__ == '__main__':
        pytest.main([__file__,'-sv','--alluredir',report_path,'--clean-alluredir'])
        os.system(f'allure serve {report_path}')
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    # Go学习-Day10
    nginx修改配置文件不生效
    【CV】第 3 章:使用 PyTorch 构建深度神经网络
    Java业务场景(一):实现屏蔽手机号码功能 && 用户手机号隐私功能 && 字符串截取 || 拼接
    ubuntu设置 Git 代理(http/git/ssh)
    [WinError 193] %1 不是有效的 Win32 应用程序
    【SpringMVC】@ResponseBody注解响应浏览器数据
    国产芯片、数字人体……今年的服贸会正上演一场“科技大秀”
    儿童护眼哪个牌子好?精选双十一必买的儿童护眼灯品牌
    Python进程池multiprocessing.Pool
  • 原文地址:https://blog.csdn.net/Ambition_ZM/article/details/138093640