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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With