Can I perform multiple assertions in pytest?

后端 未结 5 1882
轻奢々
轻奢々 2020-12-25 15:24

I\'m using pytest for my selenium tests and wanted to know if it\'s possible to have multiple assertions in a single test?

I call a function that compares multiple v

5条回答
  •  醉话见心
    2020-12-25 15:45

    pytest-assume is "a pytest plugin that allows multiple failures per test". Here's an example of how you would use it (taken from the README):

    import pytest
    
    @pytest.mark.parametrize(('x', 'y'), [(1, 1), (1, 0), (0, 1)])
    def test_simple_assume(x, y):
        pytest.assume(x == y)
        pytest.assume(True)
        pytest.assume(False)
    

    Even though some of the assertions fail, they all get evaluated and reported:

    ======================================== FAILURES =========================================
    _________________________________ test_simple_assume[1-1] _________________________________
    >    pytest.assume(False)
    test_assume.py:7
    
    y          = 1
    x          = 1
    ----------------------------------------
    Failed Assumptions:1
    _________________________________ test_simple_assume[1-0] _________________________________
    >    pytest.assume(x == y)
    test_assume.py:5
    
    y          = 0
    x          = 1
    >    pytest.assume(False)
    test_assume.py:7
    
    y          = 0
    x          = 1
    ----------------------------------------
    Failed Assumptions:2
    _________________________________ test_simple_assume[0-1] _________________________________
    >    pytest.assume(x == y)
    test_assume.py:5
    
    y          = 1
    x          = 0
    >    pytest.assume(False)
    test_assume.py:7
    
    y          = 1
    x          = 0
    ----------------------------------------
    Failed Assumptions:2
    ================================ 3 failed in 0.02 seconds =================================
    

提交回复
热议问题