views:

279

answers:

3

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!

A: 

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.

Alex Paven
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.
Jonny Cundall
+2  A: 

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.

Noffls
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..)
Jonny Cundall
Would be cool if you could publish the rule
Noffls
+2  A: 

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.

Iain Hoult
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 methods
Jonny Cundall