From a Python perspective, how can one determine the unit test(s) which covered a function or method, or generally any line of code that was hit by a test in the suite by the test runner? It seems reasonable that this information should be at hand given the coverage tools know the specific code that was hit, but I cannot find any way to get at this information (I am using py.test
as my test runner with the coverage
and pytest-cov
modules).
One approach I have found is to just put a pdb.set_trace
call into the code, but it would be really helpful if I could find a more elegant way that didn't require modifying the code under test.
Unit tests should validate all of the details, the corner cases and boundary conditions, etc. Component, integration, UI, and functional tests should be used more sparingly, to validate the behavior of the APIs or application as a whole.
On the Test menu, select Analyze Code Coverage for All Tests. You can also run code coverage from the Test Explorer tool window. Show Code Coverage Coloring in the Code Coverage Results window. By default, code that is covered by tests is highlighted in light blue.
Code coverage basically show you how much of your code is actually being used by your unit tests. Running a code coverage report helps show what code is not being used to help you write more unit tests. Code coverage can also show which branches in conditional logic are not being covered.
Coverage.py doesn't yet provide this feature, but there's an open ticket where we are kicking around ideas: https://github.com/nedbat/coveragepy/issues/170
To read the old history of this issue, check out the old ticket (in the BitBucket tracker)
Smother is a wrapper utility around coverage.py that measures code coverage separately for each test in a test suite. Its main features include:
It supports py.test and nose.
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