English 中文(简体)
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,

最佳回答

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.

问题回答

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.

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/





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签