Can I perform multiple assertions in pytest?
<p>I'm using pytest for my selenium tests and wanted to know if it's possible to have multiple assertions in a single test?</p>

<p>I call a function that compares multiple values and I want the test to report on all the values that don't match up. The problem I'm having is that using "assert" or "pytest.fail" stops the test as soon as it finds a value that doesn't match up.</p>

<p>Is there a way to make the test carry on running and report on all values that don't match?</p>

<p>As Jon Clements commented, you can fill a list of error messages and then assert the list is empty, displaying each message when the assertion is false.</p>

<p>concretely, it could be something like that:</p>

<pre><code>def test_something(self):
    errors = []

    # replace assertions by conditions
    if not condition_1:
        errors.append("an error message")
    if not condition_2:
        errors.append("an other error message")

    # assert no error message has been registered, else print messages
    assert not errors, "errors occured:\n{}".format("\n".join(errors))
</code></pre>

<p>The original assertions are replaced by <code>if</code> statements which append messages to an <code>errors</code> list in case condition are not met.
Then you assert the <code>errors</code> list is empty (an empty list is False) and make the assertion message contains each message of the <code>errors</code> list.</p>

<hr>

<p>You could also make a test generator as described in the <a href="http://nose.readthedocs.io/en/latest/writing_tests.html#test-generators" rel="nofollow">nose documentation</a>. I did not find any pytest doc which describes it, but I know that pytest handled this exactly the same manner as nose.</p>

python selenium py.test