一、断言介绍
(1)介绍
断言是自动化最终的目的,一个用例没有断言,就失去了自动化测试的意义了
断言用到的是 assert关键字。预期的结果和实际结果做对比,符合预期就是pass,不符合就fail
(2)常用断言
pytest里面的断言就是python里assert的断言方法
assert xx 判断xx为真
assert not xx 判断xx不为真
assert a in b 判断b包含a
assert a == b 判断a等于b
assert a != b 判断a不等于b
(3)案例
def is_true(num):
if num>0:
return True
else:
return False
def test_01():
"""判断是不是为真"""
a = 2
b = 0
assert is_true(a)
assert not is_true(b)
def test_02():
"""判断b包含a"""
a = "hello"
b = "hello world"
assert a in b
def test_03():
"""判断是否相等"""
a = b = "hello"
c = "hello world"
assert a == b
assert a != c
备注 unittest框架实现结果断言,pytest可以直接兼容运行 pytest.main()
二、结果断言验证
1、结果断言封装
from utils.LogUtil import my_log
import json
#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:
&#