Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing custom FxCop rules

I would like to test my custom fxrules.

I've seen this post : http://weblogs.asp.net/rosherove/archive/2007/02/24/writing-real-unit-tests-for-your-custom-fxcop-rules.aspx

but it's not working with the last version of fxcop. The Microsoft.Cci.Method.GetMethod doesn't exists and I can't find an alternative.

Do you know how to get a Microsoft.FxCop.Sdk.Method object ?

Thanks in advance for any help. Best regards,

like image 777
Tim Avatar asked Mar 01 '23 01:03

Tim


2 Answers

Given that the APIs used to write the custom rules are about as well documented and supported as the ones needed for unit testing -- and have remained the same between 1.36 (for CLR2) and 10.0 (for CLR4) -- it's probably worth noting the outline of the process to get a Microsoft.FxCop.Sdk.Method object, which can be performed using only types and methods declared public in the FxCop assemblies (no reflection trickery required).

Start with the Type of the object for which you want a Microsoft.FxCop.Sdk.Method, call this t. Get the AssemblyNode for the assembly containing t via the static entrypoint

assembly = AssemblyNode.GetAssembly(t.Module.Assembly.Location)

Get the FxCop TypeNode corresponding to t via

assembly.GetType(Identifier.For(t.Namespace), Identifier.For(t.Name))

Then search through the TypeNode's Members field to find the one where member.Name.Name is the name of the method you were looking for. Given that this is a unit test, you should be able to arrange that the dummy method being examined is not overloaded.

Then call MyRule.Check(member) to perform the test; this returns the collection of Problem objects, which can be inspected to assert that it contains the expected results and only the expected results.

like image 110
Steve Gilham Avatar answered Mar 11 '23 16:03

Steve Gilham


The removal of the reflection bridge from FxCop was announced quite some time ago. Also, use of an undocumented and unsupported API is not the only problem with the approach used in FxCopUnit, which does not implement screening for false positives. You may wish to consider switching to a testing approach that consumes the FxCop output report in order to screen for both missing violations and unexpected violations.

like image 27
Nicole Calinoiu Avatar answered Mar 11 '23 16:03

Nicole Calinoiu