• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

11-pytest-assert断言

武飞扬头像
爱学习de测试小白
帮助1

目录

前言

 断言passed

 断言failed

 断言error

 异常断言

 常用断言


前言

  • 在做自动化测试中,怎么把实际结果和预期结果作比较呢,有个专业的术语叫做断言,符合预期的用例状态是pass,不符合预期用例状态是 failed

 断言passed

  1.  
    # -*- coding: utf-8 -*-
  2.  
    # @Time : 2021/10/14
  3.  
    # @Author : 大海
  4.  
    # @File : test_20.py
  5.  
    import pytest
  6.  
     
  7.  
     
  8.  
    def four():
  9.  
    return 4
  10.  
     
  11.  
     
  12.  
    def test_function():
  13.  
    # 调用four方法返回4,与期望结果相同,结果为passed
  14.  
    assert four() == 4
  15.  
     
  16.  
     
  17.  
    if __name__ == '__main__':
  18.  
    pytest.main(["-s", "test_20.py"])
学新通

 断言failed

  1.  
    # -*- coding: utf-8 -*-
  2.  
    # @Time : 2021/10/14
  3.  
    # @Author : 大海
  4.  
    # @File : test_21.py
  5.  
    import pytest
  6.  
     
  7.  
     
  8.  
    def four():
  9.  
    return 4
  10.  
     
  11.  
     
  12.  
    def test_function():
  13.  
    # 调用four方法返回4,与期望结果不同,结果为failed
  14.  
    assert four() == 5
  15.  
     
  16.  
     
  17.  
    if __name__ == '__main__':
  18.  
    pytest.main(["-s", "test_21.py"])
学新通

 断言error

  1.  
    # -*- coding: utf-8 -*-
  2.  
    # @Time : 2021/10/14
  3.  
    # @Author : 大海
  4.  
    # @File : test_22.py
  5.  
     
  6.  
    import pytest
  7.  
     
  8.  
     
  9.  
    @pytest.fixture()
  10.  
    def user():
  11.  
    print("这是登录操作!")
  12.  
    user = '小白'
  13.  
    assert user == "小白兔" # 1.在fixture中断言失败就是error
  14.  
    return user
  15.  
     
  16.  
     
  17.  
    def test_1(user):
  18.  
    assert user == "小白"
  19.  
     
  20.  
     
  21.  
    # 2.代码写的有问题也会error,此处未定义名为login的fixture,
  22.  
    def test_2(login):
  23.  
    assert login == '小白'
  24.  
     
  25.  
     
  26.  
    if __name__ == "__main__":
  27.  
    pytest.main(["-s", "test_22.py"])
学新通

 异常断言

  1.  
    # -*- coding: utf-8 -*-
  2.  
    # @Time : 2021/10/14
  3.  
    # @Author : 大海
  4.  
    # @File : test_23.py
  5.  
     
  6.  
    import pytest
  7.  
     
  8.  
     
  9.  
    def test_zero_division():
  10.  
    """断言除0异常"""
  11.  
    with pytest.raises(ZeroDivisionError) as e_info:
  12.  
    1 / 0
  13.  
     
  14.  
    # 断言异常类型type
  15.  
    # e_info 是一个异常信息实例,它是围绕实际引发的异常的包装器。主要属性是.type、 .value 和 .traceback
  16.  
    print('value:', e_info.value)
  17.  
    print('type:', e_info.type)
  18.  
    print('traceback:', e_info.traceback)
  19.  
    assert e_info.type == ZeroDivisionError
  20.  
    # 断言异常value值
  21.  
    assert "division by zero" in str(e_info.value)
  22.  
     
  23.  
     
  24.  
    if __name__ == '__main__':
  25.  
    pytest.main(["-s", "test_23.py"])
学新通

 常用断言

  • assert xx 判断xx为真
  • assert not xx 判断xx不为真
  • assert a in b 判断b包含a
  • assert a not in b 判断b不包含a
  • assert a == b 判断a等于b
  • assert a != b 判断a不等于b
  1.  
    # -*- coding: utf-8 -*-
  2.  
    # @Time : 2021/10/14
  3.  
    # @Author : 大海
  4.  
    # @File : test_24.py
  5.  
    import pytest
  6.  
     
  7.  
     
  8.  
    def is_true(age):
  9.  
    if age == 18:
  10.  
    return True
  11.  
    else:
  12.  
    return False
  13.  
     
  14.  
     
  15.  
    def test_01():
  16.  
    a = 5
  17.  
    b = -1
  18.  
    assert is_true(a) # 断言为真
  19.  
    assert not is_true(b) # 断言为假
  20.  
     
  21.  
     
  22.  
    def test_02():
  23.  
    # 断言包含
  24.  
    a = "hello"
  25.  
    b = "hello world"
  26.  
    assert a in b
  27.  
     
  28.  
     
  29.  
    def test_03():
  30.  
    # 断言不包含
  31.  
    a = '小白'
  32.  
    b = "大海"
  33.  
    assert a not in b
  34.  
     
  35.  
     
  36.  
    def test_04():
  37.  
    # 断言相等
  38.  
    a = "小白"
  39.  
    b = "小白"
  40.  
    assert a == b
  41.  
     
  42.  
     
  43.  
    def test_05():
  44.  
    # 断言不等
  45.  
    a = 5
  46.  
    b = 6
  47.  
    assert a != b
  48.  
     
  49.  
     
  50.  
    if __name__ == "__main__":
  51.  
    pytest.main(["-s", "test_24.py"])
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcgcgg
系列文章
更多 icon
同类精品
更多 icon
继续加载