Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Determine Which Unit Test Covered a Function or Method

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.

like image 615
Joe Bane Avatar asked Oct 29 '15 20:10

Joe Bane


People also ask

What should be covered by unit tests?

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.

How do you check vs test coverage?

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.

How does unit test coverage work?

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.


2 Answers

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)

like image 108
Ned Batchelder Avatar answered Oct 24 '22 16:10

Ned Batchelder


Smother is a wrapper utility around coverage.py that measures code coverage separately for each test in a test suite. Its main features include:

  • Fast and reliable coverage tracking using coverage.py.
  • Ability to lookup which tests visit an arbitrary section of your application code.
  • Ability to convert version control diffs into a subset of affected tests to rerun.

It supports py.test and nose.

like image 38
Croad Langshan Avatar answered Oct 24 '22 16:10

Croad Langshan