• pytest-断言


    断言

    pytest允许在assert关键字后面添加任何表达式(assert )。如果表达式的值通过bool转换后等于False,则意味着用例执行失败。

    断言声明

    #!/usr/bin/python3
    #-*- conding:utf-8 -*-
     
    def test_one():
        assert 1 == 2
    
    pytest
    ==================================== test session starts =====================================
    platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
    rootdir: /media/_dde_data/python
    collected 1 item                                                                             
    
    test_001.py F                                                                          [100%]
    
    ========================================== FAILURES ==========================================
    __________________________________________ test_one __________________________________________
    
        def test_one():
    >       assert 1 == 2
    E       assert 1 == 2
    
    test_001.py:5: AssertionError
    ================================== short test summary info ===================================
    FAILED test_001.py::test_one - assert 1 == 2
    ===================================== 1 failed in 0.17s ======================================
    

    断言后添加说明

    #!/usr/bin/python3
    #-*- conding:utf-8 -*-
     
    def test_one():
        a = 1
        b = 2
        assert a == b, "判断%s = %s"%(a,b)
    
    ==================================== test session starts =====================================
    platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
    rootdir: /media/_dde_data/python
    collected 1 item                                                                             
    
    test_001.py F                                                                          [100%]
    
    ========================================== FAILURES ==========================================
    __________________________________________ test_one __________________________________________
    
        def test_one():
            a = 1
            b = 2
    >       assert a == b, "判断%s = %s"%(a,b)
    E       AssertionError: 判断1 = 2
    E       assert 1 == 2
    
    test_001.py:7: AssertionError
    ================================== short test summary info ===================================
    FAILED test_001.py::test_one - AssertionError: 判断1 = 2
    ===================================== 1 failed in 0.21s ======================================
    

    异常的断言

    断言异常类型

    #!/usr/bin/python3
    #-*- conding:utf-8 -*-
    import pytest
    
    def test_one():
        a = "test"
        with pytest.raises(ValueError):
            int(a)
    
    ==================================== test session starts =====================================
    platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
    rootdir: /media/_dde_data/python
    collected 1 item                                                                             
    
    test_001.py .                                                                          [100%]
    
    ===================================== 1 passed in 0.01s ======================================
    

    断言异常的值

    #!/usr/bin/python3
    #-*- conding:utf-8 -*-
    import pytest
    
    def test_one():
        a = "test"
        with pytest.raises(ValueError) as f:
            int(a)
    
        assert f.type == ValueError
        assert "invalid" in str(f.value)
    

    ==================================== test session starts =====================================
    platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
    rootdir: /media/_dde_data/python
    collected 1 item

    test_001.py . [100%]

    ===================================== 1 passed in 0.01s ======================================

  • 相关阅读:
    ASP.NET 身份验证机制
    ASP.NET 一般处理程序下载
    [c、c++]宏中"#"和"##"的用法(zz)
    XML 链接语言(XLink) 版本 1.0
    网上看到的对个人发展很不错的话
    【分享】WebForm中DataGrid的20篇经典文章
    Ajax
    ISO/OSI网络体系结构
    经典推荐.Net面试法宝(面试题收集)
    Union的问题
  • 原文地址:https://www.cnblogs.com/jingxindeyi/p/13348239.html
Copyright © 2020-2023  润新知