首页 > 其他分享 >关键字 开发-09 validate断言

关键字 开发-09 validate断言

时间:2023-12-02 11:23:33浏览次数:28  
标签:09 equals value len 关键字 expect validate check

1. yaml文件中添加validate进行接口断言

首先我们在utils/validate.py文件添加需要的断言方式

import re

def equals(check_value, expect_value):
    assert check_value == expect_value, f'{check_value} == {expect_value}'

def less_than(check_value, expect_value):
    assert check_value < expect_value, f'{check_value} < {expect_value}'

def less_than_or_equals(check_value, expect_value):
    assert check_value <= expect_value, f'{check_value} <= {expect_value}'

def greater_than(check_value, expect_value):
    assert check_value > expect_value, f'{check_value} > {expect_value})'

def greater_than_or_equals(check_value, expect_value):
    assert check_value >= expect_value, f'{check_value} >= {expect_value}'

def not_equals(check_value, expect_value):
    assert check_value != expect_value, f'{check_value} != {expect_value}'

def string_equals(check_value, expect_value):
    assert str(check_value) == str(expect_value), f'{check_value} == {expect_value}'

def length_equals(check_value, expect_value):
    expect_len = _cast_to_int(expect_value)
    if check_value is None:
        check_value = []
    if isinstance(check_value, list):
        assert len(check_value) == expect_len, f'{len(check_value)} == {expect_value}'
    elif isinstance(check_value, (int, float)):
        assert len(str(check_value)) == expect_len, f'{len(str(check_value))} == {expect_value}'
    else:
        assert len(check_value) == expect_len, f'{len(check_value)} == {expect_value}'

def length_greater_than(check_value, expect_value):
    expect_len = _cast_to_int(expect_value)
    assert len(check_value) > expect_len, f'{len(check_value)} > {expect_value}'


def length_greater_than_or_equals(check_value, expect_value):
    expect_len = _cast_to_int(expect_value)
    assert len(check_value) >= expect_len, f'{len(check_value)} >= {expect_value}'


def length_less_than(check_value, expect_value):
    expect_len = _cast_to_int(expect_value)
    assert len(check_value) < expect_len, f'{len(check_value)} < {expect_value}'


def length_less_than_or_equals(check_value, expect_value):
    expect_len = _cast_to_int(expect_value)
    assert len(check_value) <= expect_len, f'{len(check_value)} <= {expect_value}'


def contains(check_value, expect_value):
    if isinstance(check_value, (list, tuple, dict, str)):
        assert expect_value in check_value, f'{expect_value} in {check_value}'
    else:
        # 数字类型包含
        assert expect_value in str(check_value), f'{expect_value} in {check_value}'


def contained_by(check_value, expect_value):
    if isinstance(expect_value, (list, tuple, dict, str)):
        assert check_value in expect_value, f'{check_value} in {expect_value}'
    else:
        # 数字类型包含
        assert str(check_value) in expect_value, f'{check_value} in {check_value}'


def regex_match(check_value, expect_value):
    assert isinstance(expect_value, str)
    assert isinstance(check_value, str)
    assert re.match(expect_value, check_value)


def startswith(check_value, expect_value):
    assert str(check_value).startswith(str(expect_value)), f'{str(check_value)} startswith {str(expect_value)}'


def endswith(check_value, expect_value):
    assert str(check_value).endswith(str(expect_value)), f'{str(check_value)} endswith {str(expect_value)}'


def _cast_to_int(expect_value):
    try:
        return int(expect_value)
    except Exception:
        raise AssertionError(f"%{expect_value} can't cast to int")


def bool_equals(check_value, expect_value):
    assert bool(check_value) == bool(expect_value), f'{check_value} -> {bool(check_value)} == {expect_value}'


def _cast_to_int(expect_value):
    try:
        return int(expect_value)
    except Exception:
        raise AssertionError(f"%{expect_value} can't cast to int")

在run.py文件中新增断言函数

    def validate_response(self, response, validate_check: list):
        """校验结果, eg: - eq: [check_type, expect_value]"""
        if not isinstance(validate_check, list):
            log.error(f'validate_check object is not list type!')
        for check in validate_check:
            for check_type, check_value in check.items():
                actual_value = extract.extract_by_object(response, check_value[0])  # 实际结果
                expect_value = check_value[1]  # 期望结果
                log.info(f'validate校验结果->{check_type}: [{actual_value}, {expect_value}]')
                if check_type in ['eq', 'equals', 'equal']:
                    validate.equals(actual_value, expect_value)
                elif check_type in ["lt", "less_than"]:
                    validate.less_than(actual_value, expect_value)
                elif check_type in ["le", "less_or_equals"]:
                    validate.less_than_or_equals(actual_value, expect_value)
                elif check_type in ["gt", "greater_than"]:
                    validate.greater_than(actual_value, expect_value)
                elif check_type in ["ne", "not_equal"]:
                    validate.not_equals(actual_value, expect_value)
                elif check_type in ["str_eq", "string_equals"]:
                    validate.string_equals(actual_value, expect_value)
                elif check_type in ["len_eq", "length_equal"]:
                    validate.length_equals(actual_value, expect_value)
                elif check_type in ["len_gt", "length_greater_than"]:
                    validate.length_greater_than(actual_value, expect_value)
                elif check_type in ["len_ge", "length_greater_or_equals"]:
                    validate.length_greater_than_or_equals(actual_value, expect_value)
                elif check_type in ["len_lt", "length_less_than"]:
                    validate.length_less_than(actual_value, expect_value)
                elif check_type in ["len_le", "length_less_or_equals"]:
                    validate.length_less_than_or_equals(actual_value, expect_value)
                elif check_type in ["contains"]:
                    validate.contains(actual_value, expect_value)
                else:
                    if hasattr(validate, check_type):
                        getattr(validate, check_type)(actual_value2, expect_value)
                    else:
                        log.error(f'{check_type} not valid check type!')

execute_yaml_case用例函数中,添加validate处理逻辑

                    elif item == 'validate':
                        log.info(f'validate校验的内容:{value}')
                        validate_value = self.validate_response(response, value)

运行,查看结果,发现断言成功,用例pass

标签:09,equals,value,len,关键字,expect,validate,check
From: https://www.cnblogs.com/dack-zt-deng/p/17871380.html

相关文章

  • day09 Helm开发与实践-基于Helm的方式运维管理应用 (3.2-3.3)
    一、Helm开发与实践1、HelmChart详解1.1Chart目录结果#helmcreatenginxCreatingnginx#treenginxnginx├──charts├──Chart.yaml├──templates│  ├──deployment.yaml│  ├──_helpers.tpl│  ├──hpa.yaml│  ├──ingre......
  • python网站创建009:注释和代码存放位置
    html的注释<!--这是html的注释-->css的注释/*这是css的注释*/javascript的注释//这是js的单行注释/*这是js的多行注释*/ css代码的存放位置:是实际项目中,css代码可以放在html的head中,如下: 也可以将css代码存放到别的文件中,如下: javascript代码的存放位置:js......
  • 977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II
    977.有序数组的平方,209.长度最小的子数组,59.螺旋矩阵II 977.有序数组的平方思路:分别从数组的左,右向另一侧/中间趋近,新建立一个数组接收(有序序列)(动态地在过程中接收数据)  拓展为各个任务分配工作指针,形成多指针有序数字序......
  • 关键字 开发-08 extract提取返回的结果
    1.extract提取请求返回结果的返回值在进行接口自动化的时候,往往需要提取上一个接口返回的值,传递给下一个接口使用。创建utils/extract.pyimportjsonimportreimportjsonpathimportjmespathfromutilsimportexceptionsfromrequestsimportResponsedefextract_b......
  • 代码随性训练营第五十一天(Python)| 309.最佳买卖股票时机含冷冻期、714.买卖股票的最佳
    309.最佳买卖股票时机含冷冻期classSolution:defmaxProfit(self,prices:List[int])->int:#dp[i][0]持有股票#dp[i][1]卖出股票那一天#dp[i][2]冷冻期#dp[i][3]保持卖出股票的状态dp=[[0]*4for_inrange(......
  • 高通4G全网通模块MSM8909 (ARM Cortex-A7架构),arm核心板
    高通4G全网通模块MSM8909(ARMCortex-A7架构),arm核心板-ARM技术论坛-电子技术论坛-广受欢迎的专业电子论坛!https://bbs.elecfans.com/jishu_1461523_1_1.html 核心板特性A7架构4核(4*1.1GHz(A7))产品尺寸小,便于客户集成,减少产品体积;支持4GLTE超高速上网,单板兼容移动......
  • @validated 验证 List 参数在spring中
    主要介绍了@validated验证List参数在spring中相关的知识,希望对你有一定的参考价值。@PostMapping(value="complete")publicVocomplete(@Valid@RequestBody@Validated(CompleteTaskRequest.class)ValidList<CompleteTaskRequest>request,BindingResultbinding......
  • macOS Sonoma 14.1.2 (23B92 | 23B2091) 正式版发布,ISO、IPSW、PKG 下载 (Webkit 零日
    macOSSonoma14.1.2(23B92|23B2091)正式版发布,ISO、IPSW、PKG下载(Webkit零日漏洞修复)本站下载的macOS软件包,既可以拖拽到Applications(应用程序)下直接安装,也可以制作启动U盘安装,或者在虚拟机中启动安装。另外也支持在Windows和Linux中创建可引导介质。请访问......
  • 【Vulnhub 靶场】【DriftingBlues: 9 (final)】【简单】【20210509】
    1、环境介绍靶场介绍:https://www.vulnhub.com/entry/driftingblues-9-final,695/靶场下载:https://download.vulnhub.com/driftingblues/driftingblues9.ova靶场难度:简单发布日期:2021年05月09日文件大小:738MB靶场作者:tasiyanci靶场描述:getflags打靶耗时:2+小时打靶关键:......
  • instanceof关键字
    instanceof用于判断类型eg:catinstanceofAnimal判断该对象引用,是否是为该类型(自身类、自身类父类、Object)都是返回true,其他情况返回falseeg:strinstanceofString例子AnimalParent类publicclassAnimalParent{}Cat类publicclassCatextendsAnimalParent......