Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display number of assertions in python unit tests

phpUnit displays the number of tests run, and the number of assertions made. The way I currently execute python's unit tests, only the number of tests run is displayed. Is there a way to also count the number of assertions?

like image 216
DudeOnRock Avatar asked Dec 28 '13 06:12

DudeOnRock


1 Answers

If you are willing to run your tests with pytest, it can count the assertions for you.

There is a hook you can implement in your conftest.py file that gets called for every passing assertion. You could count the number of times that gets called, and then print that out in the summary. That won't count calls to unittest.TestCase.assert...() functions, only assert statements.

To enable the hook, write a conftest.py file like this:

assertion_count = 0


def pytest_assertion_pass(item, lineno, orig, expl):
    global assertion_count
    assertion_count += 1


def pytest_terminal_summary(terminalreporter, exitstatus, config):
    print(f'{assertion_count} assertions tested.')

Then enable the hook in your pytest.ini file:

[pytest]
enable_assertion_pass_hook=true

You might also find the assertion compare hook useful.

After all that, I'm not sure that the number of assertions is a reliable measure of test quality. You might want to look into mutation testing with MutPy or mutmut.

like image 193
Don Kirkby Avatar answered Oct 02 '22 17:10

Don Kirkby