零基础小白(六)中篇-Assert请求接口结果断言

零基础小白 接口自动化测试集锦: https://www.jianshu.com/nb/49125734

实战--登录接口请求结果断言

代码参考

# -*- coding: utf-8 -*-
# @Time : 2021/1/13 15:20
# @File : 004 tets_login.py
# @Author : Yvon_₯㎕ζ๓

from config.Conf import ConfigYaml
import pytest,json
from  utils.RequestsUtil import Request

#1、获取测试用例内容list
#获取testlogin.yml文件路径

data_list = ConfigYaml().get_testlogin_conf_info()
# print(data_list)

'''参数化执行测试用例'''
@pytest.mark.parametrize("login",data_list) # login 是定义的变量

def test_login_pytest(login):
    # 初始化url,data
    url = ConfigYaml().get_conf_url() + login["url"]
    # print("url %s"%url)
    data = login["data"]
    headers = login["headers"]
    # print("data %s"%data)
    # 初始化requests请求
    request = Request()
    res = request.post(url,headers=headers,json = data)
    print(json.dumps(res, sort_keys=True, ensure_ascii=False, indent=4, separators=(', ', ': ')))  # Json格式打印

    code = res["code"]
    # print(code)
    assert code in(200,101)

if __name__ == "__main__":
    pytest.main(["-s","004 tets_login.py"])
增加接口请求断言.jpg

断言结果生效.jpg

实战--封装接口请求断言

工具类utils文件夹下新增AssertUtil.py文件,用来编写封装接口请求代码,供全局接口调用

代码参考

import json
from utils.LogUtil import my_log


#1、定义封装类
class AssertUtil:
#2、初始化数据、日志
    def __init__(self):
        self.log = my_log("AssertUtil")
#3、code相等
    def assert_code(self,code,expected_code):
        """
        验证返回状态码
        :param code:
        :param expected_code:
        :return:
        """
        try:
            assert int(code) == int(expected_code)
            return True
        except:
            self.log.error("code error,code is %s,expected_code is %s"%(code,expected_code))
            raise

#4、body相等
    def assert_body(self, body, expected_body):
        """
        验证返回结果内容相等
        :param body:
        :param expected_body:
        :return:
        """
        try:
            assert body == expected_body
            return True
        except:
            self.log.error("body error,body is %s,expected_body is %s" % (body, expected_body))
            raise

#5、body包含
    def assert_in_body(self, body, expected_body):
        """
        验证返回结果包含期望结果
        :param body:
        :param expected_in_body:
        :return:
        """
        try:
            body = json.dumps(body)
            print(body)
            assert expected_body in body
            return True
        except:
            self.log.error("不包含或者body是错误,body is %s,expected_body is %s" % (body, expected_body))
            raise

实战--登录接口调用封装断言脚本

重构登录脚本

from config.Conf import ConfigYaml
import pytest,json
from  utils.RequestsUtil import Request
from utils.AssertUtil import AssertUtil

#1、获取测试用例内容list
#获取testlogin.yml文件路径

data_list = ConfigYaml().get_testlogin_conf_info()
# print(data_list)

'''参数化执行测试用例'''
@pytest.mark.parametrize("login",data_list) # login 是定义的变量

def test_login_pytest(login):
    # 初始化url,data
    url = ConfigYaml().get_conf_url() + login["url"]
    # print("url %s"%url)
    params= login["data"]
    headers = login["headers"]
    # print("data %s"%data)
    # 初始化requests请求
    request = Request()
    res = request.post(url,headers=headers,json = params)
    print(json.dumps(res, sort_keys=True, ensure_ascii=False, indent=4, separators=(', ', ': ')))  # Json格式打印
    code= res["code"]
    #assert code in(200,101)
    AssertUtil().assert_code(code,200)
    data = res["data"]
    AssertUtil().assert_in_body(data, '"userPhone": ""')

if __name__ == "__main__":
    pytest.main(["-s","004 tets_login.py"])
调用封装断言.jpg

你可能感兴趣的:(零基础小白(六)中篇-Assert请求接口结果断言)