Reputation: 753
I'm familiar with a number of code coverage tools, which tells me what percentage of lines / branches / etc are covered by my tests, and even show me which parts of the code have poor coverage.
Are there any tools which do the opposite -- that is, given a section of code, can it show me which tests touch it? That would make it easy to start exploring unfamiliar & poorly documented code, by playing with the tests that are relevant.
You might say it should be obvious from the way the unit tests are organized, but the fact is it often is not. I've worked with more than one project where this was the case.
I happen to be interested in java / scala, but I'm also just interested if anything does this or its just a totally crazy idea.
Upvotes: 5
Views: 190
Reputation: 16380
The code coverage tool in the JMockit toolkit (which I develop) provides this feature. You can see it at work in the sample coverage report that is available online.
For line 72 in the OrderRepository.java
source file, for example, the report page shows that two tests touch it: OrderFindersTest#findOrderByCustomer
and
OrderFindersTest#findOrderByNumber
.
Upvotes: 1
Reputation: 95344
This is straightforward to accomplish. What you do is run the unit tests individually and capture their individual trace information.
Now, a trace is essentially a bit vector where each bit represents a place in the program that was covered, or not.
Then you construct an artificial trace for the code of interest. (Most tools won't make this easy to do, but the coverage information available to the tool should make this possible).
Finally, you take the artificial trace, and intersect it (its a bit vector, remember?) with the individual unit test traces. A non-empty intersection identifies those tests that exercise the code of interest.
Semantic Designs (my company, see bio) provides test coverage tools that can collect individual traces for tests, and can do the intersection using its UI. Some custom coding could manufacture the artificial traces.
Upvotes: 0