前言
断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了。什么是断言呢?
简单来讲就是实际结果和期望结果去对比,符合预期那就测试pass,不符合预期那就测试 failed
案例演示
def f():
return 3
def test_function():
a = f()
assert a % 2 == 0, "判断a为偶数,当前a的值为:%s"%a
执行结果
=================================== FAILURES ===================================
________________________________ test_function _________________________________
def test_function():
a = f()
> assert a % 2 == 0, "判断a为偶数,当前a的值为:%s"%a
E AssertionError: 判断a为偶数,当前a的值为:3
E assert 1 == 0
test_example.py:19: AssertionError
=========================== short test summary info ============================
FAILED test_example.py::test_function - AssertionError: 判断a为偶数,当前a的...
============================== 1 failed in 0.06s ===============================
常用断言
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