How to find code which is only called by tests
Asked Answered
T

3

9

Occasionally I am looking at some code, I search for usages of a method (using resharper) and find that it is only called by tests. So it's effectively redundant and I can delete it and the methods that call it.

Obviously there's no point in having unused code lying around the place, slowing down the build and the test run. What I'd like is a tool that can tell me where all the bits of production code are that are only accessed by tests.

I have a full version of resharper, and also a trial version of NDepend, but haven't found out how to use either of these to get the result I want (without paying for it). I suspect It may be possible with the full version of NDepend but are there any other tools that people know about?

If the context helps, the solution is and ASP.net website, much of whose functionality is handled by a WCF service. So the only valid entry points to the bulk of the code are the service methods. The tests are in their own seperate projects.

I've started a bounty because I'm sure someone else must have had and solved this problem before!

Trevor answered 21/9, 2010 at 15:52 Comment(2)
Some of that test only code is likely mocks, stubs, etc...Seger
@Chad all mocks, and stubs etc are in my test projects. In this question I'm more concerned about production code.Trevor
J
4

Manually looking with NDepend should work with the Dependency Matrix. There you can see which methods are used only by the Unit Test Assemblies.

I'm not sure if you can write own CQL Queries with the Trial Version. But with the Pro Version you could use a Query like this:

SELECT METHODS WHERE IsUsedBy "ASSEMBLY:NAME_OF_THE_UNIT_TEST_ASSEMBLY" 
AND !(IsUsedBy "ASSEMBLY:NAME_OF_ANOTHER_ASSEMBLY" OR IsUsedBy "ASSEMBLY:ANOTHER_NAME")

For this to work you have to create a NDepend Project that knows all your Assemblies.

For NAME_OF_THE_UNIT_TEST_ASSEMBLY you have to insert your Unit Test Assembly and in the second part you have to specify you Production Code Assemblies with IsUsedBy and seperated with OR.

Jingoism answered 27/9, 2010 at 10:0 Comment(1)
As far as I can see I need the full version to be able to run custom queries. I'm looking at making a custom FxCop rule along the lines of your query (a lot more difficult on FxCop though..)Trevor
W
3

A non-technical approach would be to temporarily remove you test project from your solution, then use Visual Studio's code analysis (or FxCop) to locate any methods that are not being called by anything else.

Wichern answered 29/9, 2010 at 14:49 Comment(1)
The trouble with using FxCop is that it ignores public methods when its looking for dead code, and I know that a lot of the things I'm trying to find here are public methodsTrevor
M
0

You could use NDepend with some custom queries... That's just off the top of my head, never used it for exactly that, but it should work.

Monroy answered 21/9, 2010 at 18:25 Comment(1)
I've looked into that, and it looks like I would probably have to get the pay version to be able to add my own queries.Trevor

© 2022 - 2024 — McMap. All rights reserved.