I am using pytest for writing some tests at integration level. I would like to be able to also report the number of assertions done on each test case. By default, pytest will only report the number of test cases which have passed and failed.
As far as I can see there is no such possibility to get the number of passed assertions out of pytest.
Reason: it employs Python's standard assert statement, so the determination whether the assertion is a pass or a fail is done within the engine used, and in case the engine does not count how often it got into the "pass" branch of the necessary if in assert's implementation, and offer a way to read out that counter, there is just no way to elicit that information from the engine. So pyTest can't tell you that either.
Many (unit) test frameworks report assertion count by default, and IMHO i deem it desirable, because it is - amongst others - a measure for test quality: sloppy tests might apply too few checks to values.
I am also interested into printing the count of assertions of unit-tests with pytest.
Although I do not have a working solution, this piece of documentation shows that pytest rewrites the statements assert behind the scene: https://docs.pytest.org/en/stable/how-to/assert.html#assert-details
Assertion introspection details
Reporting details about a failing assertion is achieved by rewriting assert statements before they are run. Rewritten assert statements put introspection information into the assertion failure message.
The fact that pytest rewrites the statements assert has been there for a while, see this blog post from Benjamin Peterson, the author of the rewriting of the statements asserts by pytest: https://pybites.blogspot.com/2011/07/behind-scenes-of-pytests-new-assertion.html
py.test 2.1's main new feature, which I wrote (with generous sponsorship from Merlinux GmbH), is a new assertion introspection technique called assertion rewriting. Assertion rewriting modifies the AST of test modules to produce subexpression information when assertions fail. This blog post will give a peek into how this is done and what the rewritten tests look like.
That invalidates a comment above saying that pytest uses Python's native assert.
Though I still don't know how to have pytest print the count of asserts. Maybe with a plugin.
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