Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I perform multiple assertions in pytest?

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 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.

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

like image 418
ChrisG29 Avatar asked Oct 06 '16 13:10

ChrisG29


People also ask

Can you have multiple asserts in one test?

Multiple asserts are good if you are testing more than one property of an object simultaneously. This could happen because you have two or more properties on an object that are related. You should use multiple asserts in this case so that all the tests on that object fail if any one of them fails.

How do you assert multiple conditions in Python?

If you assert multiple conditions in a single assert statement, you only make the job harder. This is because you cannot determine which one of the conditions caused the bug. If you want to push it, you can assert multiple conditions similar to how you check multiple conditions in an if statement.

How many assertions should be included in a JUnit test?

I should definitely use only one assert in test method!


1 Answers

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.

concretely, it could be something like that:

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)) 

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


You could also make a test generator as described in the nose documentation. I did not find any pytest doc which describes it, but I know that pytest handled this exactly the same manner as nose.

like image 131
Tryph Avatar answered Sep 23 '22 21:09

Tryph