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.C开发者_高级运维ci.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,
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.
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.
Maybe you should be using Gendarme as it is open source and so it unlickly to have the same problems. (Why can't Microsoft get its head round test-driven-development?)
You might want to check out my RoslynCTP based FxCop test framework, it has the code required to execute a rule and to verify it flagged the right issues. Due to the fact that Roslyn is still in CTP not all .NET language features can be tested at this point in time.
It should be pretty simple to extract the code required to run the rules against any assembly.
Any rule contributions are welcome to this project as well :).
http://fxcopcontrib.codeplex.com/
精彩评论