前言:断言是自动化最终的目的,一个用例没有断言,就失去了自动化测试的意义了。
断言用到的是 assert关键字。之前的介绍,有的测试方法中其实用到了assert断言。简单的来说,就是预期的结果去和实际结果做对比,符合预期结果就是pass,不符合就fail。
比如以下的一段代码:
def add(a,b): return a+b def test_assert(): assert 6 == add(3,4)
结果:可以看出,判断的结果是6,和方法返回的结果不一致,所以导致失败。从黄色部分的报错信息可以看出,结果不一致。
test_assert.py F
test_assert.py:11 (test_assert)
7 != 6
Expected :6
Actual :7
<Click to see difference>
def test_assert():
> assert 6 == add(3,4)
E assert 6 == 7
E + where 7 = add(3, 4)
test_assert.py:13: AssertionError
[100%]
=================================== FAILURES ===================================
_________________________________ test_assert __________________________________
def test_assert():
> assert 6 == add(3,4)
E assert 6 == 7
E + where 7 = add(3, 4)
test_assert.py:13: AssertionError
异常信息提示:如果想在异常的时候输出一些提示信息,这样报错后就方便查看是什么原因了。
def add(a,b): return a+b def test_assert(): assert 6 == add(3,4),"方法返回的值不等于6,而是等于{0}".format(add(3,4))
结果:可以看出黄色部分,这样会写清楚失败的原因,当有多个判断时,可以容易定位问题。
test_assert.py F
test_assert.py:12 (test_assert)
def test_assert():
> assert 6 == add(3,4),"方法返回的值不等于6,而是等于{0}".format(add(3,4))
E AssertionError: 方法返回的值不等于6,而是等于7
E assert 6 == 7
E + where 7 = add(3, 4)
test_assert.py:15: AssertionError
[100%]
=================================== FAILURES ===================================
_________________________________ test_assert __________________________________
def test_assert():
> assert 6 == add(3,4),"方法返回的值不等于6,而是等于{0}".format(add(3,4))
E AssertionError: 方法返回的值不等于6,而是等于7
E assert 6 == 7
E + where 7 = add(3, 4)
test_assert.py:15: AssertionError
异常断言:为了写关于引发异常的断言,可以使用pytest.raises作为上下文管理器:
import pytest def test_zero_division(): with pytest.raises(ZeroDivisionError): 1/0
结果:可以看出并没有报错,正常情况下,不加异常是会报错的。
============================= test session starts ==============================
platform darwin -- Python 2.7.10, pytest-3.6.3, py-1.5.2, pluggy-0.6.0
rootdir: /Users/newcomer/PycharmProjects/error/wuya/pytestDemo, inifile:
plugins: metadata-1.7.0, html-1.19.0, D3-2.0.13, cov-2.5.1, catchlog-1.2.2, allure-adaptor-1.7.10, georaven-17.1.0.170collected 1 item
test_assert.py . [100%]
-- Docs: http://doc.pytest.org/en/latest/warnings.html
===================== 1 passed, 1 warnings in 0.01 seconds =====================
以上,我们要断言它抛的异常是不是预期的,比如执行:1/0,预期结果是抛异常:ZeroDivisionError: division by zero,那我们要断言这个异常。通常是断言异常的type和value的值。这里1/0的异常类型是ZeroDivisionError,异常的value值是"integer division or modulo by zero",于是以下是代码的设计用例:
import pytest def test_zero_division(): with pytest.raises(ZeroDivisionError,message="Exceptions ZeroDivisionError") as exinfo: 1/0 assert exinfo.type == ZeroDivisionError assert str(exinfo.value) == "integer division or modulo by zero","{0}".format(exinfo.value)
结果:可以看出来,通过了一个测试用例,但是需要注意,需要把value的值转换成str类型的才可以比较,而且值是要加引号的。断言type的时候,不需要给异常类型添加引号。
raises里面的message关键字参数消息指定自定义失败消息。
============================= test session starts ==============================
platform darwin -- Python 2.7.10, pytest-3.6.3, py-1.5.2, pluggy-0.6.0
rootdir: /Users/newcomer/PycharmProjects/error/wuya/pytestDemo, inifile:
plugins: metadata-1.7.0, html-1.19.0, D3-2.0.13, cov-2.5.1, catchlog-1.2.2, allure-adaptor-1.7.10, georaven-17.1.0.170collected 1 item
test_assert.py .exinfo.type: <type 'exceptions.ZeroDivisionError'>
exinfo.value: integer division or modulo by zero
[100%]
===================== 1 passed, 1 warnings in 0.01 seconds =====================
import pytest def test_exception(): with pytest.raises(ZeroDivisionError,message="Exception ZeroDivisionError") as exinfo: pass
结果:可以看出来,结果中的异常是message自己定义的信息
=================================== FAILURES ===================================
________________________________ test_exception ________________________________
def test_exception():
with pytest.raises(ZeroDivisionError,message="Exception ZeroDivisionError") as exinfo:
> pass
E Failed: Exception ZeroDivisionError
test_assert.py:28: Failed
================ 1 failed, 1 warnings in 0.08 seconds ================
常用断言: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
def is_true(num): if num>0: return True else: return False def test_01(): """判断是不是为真""" a = 5 b = -1 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 = "hello" b = "hello" c = "hello world" assert a == b assert a != c
结果:由此可以看出来,三个用例都执行成功。
============================= test session starts ==============================
platform darwin -- Python 2.7.10, pytest-3.6.3, py-1.5.2, pluggy-0.6.0
rootdir: /Users/newcomer/PycharmProjects/error/wuya/pytestDemo, inifile:
plugins: metadata-1.7.0, html-1.19.0, D3-2.0.13, cov-2.5.1, catchlog-1.2.2, allure-adaptor-1.7.10, georaven-17.1.0.170collected 3 items
test_assert.py ... [100%]
=============================== warnings summary ===============================
<undetermined location>
pytest-catchlog plugin has been merged into the core, please remove it from your requirements.
-- Docs: http://doc.pytest.org/en/latest/warnings.html
===================== 3 passed, 1 warnings in 0.02 seconds =====================