Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find code which is only called by tests

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!

like image 828
Jonny Cundall Avatar asked Sep 21 '10 15:09

Jonny Cundall


2 Answers

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.

like image 150
Noffls Avatar answered Oct 16 '22 06:10

Noffls


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.

like image 37
Iain Hoult Avatar answered Oct 16 '22 08:10

Iain Hoult