• 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 ======================================

  • 相关阅读:
    IOS快速集成下拉上拉刷新
    无限互联IOS电影项目视频笔记
    Unix时间戳(Unix timestamp)转换工具
    ***php(codeigniter)中如何重定向
    CodeIgniter 错误: In order to use the Session class you are required to set an encryption key
    一个Form中2个按钮,PHP后台如何判断提交的是哪一个按钮
    PHP 判断是否包含某字符串
    Linux那些事儿之我是Hub(大结局)挂起自动化【转】
    Linux USB Host-Controller的初始化代码框架分析【转】
    urb传输的代码分析【转】
  • 原文地址:https://www.cnblogs.com/jingxindeyi/p/13348239.html
Copyright © 2020-2023  润新知