断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了。什么是断言呢? 简单来说就是实际结果和指望结果去对比,符合预期那就测试pass,不符合预期那就测试 failedhtml
pytest容许您使用标准Python断言来验证Python测试中的指望和值。例如,你能够写下python
# content of test_assert1.py def f(): return 3 def test_function(): assert f() == 4
断言f()函数的返回值,接下来会看到断言失败,由于返回的值是3,判断等于4,因此失败了linux
$ pytest test_assert1.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item test_assert1.py F [100%] ================================= FAILURES ================================= ______________________________ test_function _______________________________ def test_function(): > assert f() == 4 E assert 3 == 4 E + where 3 = f() test_assert1.py:5: AssertionError ========================= 1 failed in 0.12 seconds =========================
从报错信息能够看到断言失败缘由:E assert 3 == 4session
接下来再看一个案例,若是想在异常的时候,输出一些提示信息,这样报错后,就方便查看是什么缘由了函数
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 (3 % 2) == 0 test_03.py:9: AssertionError ========================== 1 failed in 0.18 seconds ===========================
这样当断言失败的时候,会给出本身写的失败缘由了E AssertionError: 判断a为偶数,当前a的值为:3spa
为了写关于引起异常的断言,可使用pytest.raises做为上下文管理器,以下设计
# content of test_assert1.py import pytest def test_zero_division(): with pytest.raises(ZeroDivisionError): 1 / 0
运行结果code
============================= test session starts ============================= platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 rootdir: D:\YOYO\canshuhua, inifile: plugins: metadata-1.7.0, html-1.19.0 collected 1 item test_assert1.py. ========================== 1 passed in 0.31 seconds ===========================
若是咱们要断言它抛的异常是否是预期的,好比执行:1/0,预期结果是抛异常:ZeroDivisionError: division by zero,那咱们要断言这个异常,一般是断言异常的type和value值了。 这里1/0的异常类型是ZeroDivisionError,异常的value值是division by zero,因而用例能够这样设计orm
# content of test_assert1.py # ** 做者:上海-悠悠 QQ交流群:588402570** import pytest def test_zero_division(): '''断言异常''' with pytest.raises(ZeroDivisionError) as excinfo: 1 / 0 # 断言异常类型type assert excinfo.type == ZeroDivisionError # 断言异常value值 assert "division by zero" in str(excinfo.value)
excinfo 是一个异常信息实例,它是围绕实际引起的异常的包装器。主要属性是.type、 .value 和 .traceback
注意:断言type的时候,异常类型是不须要加引号的,断言value值的时候需转str
在上下文管理器窗体中,可使用关键字参数消息指定自定义失败消息:
with pytest.raises(ZeroDivisionError, message="Expecting ZeroDivisionError"): pass 结果:Failed: Expecting ZeroDivisionError
pytest里面断言实际上就是python里面的assert断言方法,经常使用的有如下几种
import pytest # ** 做者:上海-悠悠 QQ交流群:588402570** def is_true(a): if a > 0: return True else: return False def test_01(): '''断言xx为真''' 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 = "yoyo" b = "yoyo" assert a == b def test_04(): '''断言不等于''' a = 5 b = 6 assert a != b if __name__ == "__main__": pytest.main(["-s", "test_01.py"])
---------------------------------pytest结合selenium自动化完整版-------------------------
全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b
做者:上海-悠悠 QQ交流群:874033608
也能够关注下个人我的公众号:yoyoketang