Can I handle multiple statements within the same pyton pytest method?

I write tests with pytest, and I ran into the following problem: I have a test that tests some variable, and then I do a heavy calculation, and after that I want to run another test.

The problem is that if the first assertone failed, the entire test failed, and pystestdid not complete the second test. Code:

class TestSomething:
    def tests_method(self, some_variables):
        # Some actions that take a lot of time!
        assert some_var == 1
        # Some actions that take a lot of time!
        assert some_var == 2

I know that this testing method can be divided into 2 methods, but the performance problem here is crucial.

Is there a way that I can run 2 statements in one method?

+6
source share
1

. , , . :

def foo(x):
    return x + 1


def bar(y):
    return y - 1


def test_foo():
    # some expensive calculation                                                                                                                                    
    a = foo(10)

    # another expensive calculation                                                                                                                                 
    b = bar(10)

    assert (a, b) == (10, 9)

pytest, :

$ pytest scratch.py
============================= test session starts =============================
platform linux2 -- Python 2.7.12, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/don/workspace/scratch, inifile:
collected 1 items

scratch.py F

================================== FAILURES ===================================
__________________________________ test_foo ___________________________________

def test_foo():
# some expensive calculation
a = foo(10)

# another expensive calculation
b = bar(10)

>       assert (a, b) == (10, 9)
E       assert (11, 9) == (10, 9)
E         At index 0 diff: 11 != 10
E         Use -v to get the full diff

scratch.py:16: AssertionError
========================== 1 failed in 0.02 seconds ===========================

and , - short circuiting. , :

assert a == 10 and b == 9

Pytest :

>       assert a == 10 and b == 9
E       assert (11 == 10)

b, --showlocals.

+3

Source: https://habr.com/ru/post/1016175/


All Articles