我想测试我的自定义 fxrules。
我看过这篇文章:http://weblogs.asp.net/rosherove/archive/2007/02/24/writing-real-unit-tests-for-your-custom-fxcop-rules.aspx
但它不适用于最新版本的 fxcop。
Microsoft.Cci.Method.GetMethod 不存在,我找不到替代方法。
您知道如何获取 Microsoft.FxCop.Sdk.Method 对象吗?
预先感谢您的任何帮助。
此致,
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,
发布评论
评论(4)
鉴于用于编写自定义规则的 API 与单元测试所需的 API 一样有完善的文档和支持 — 并且在 1.36(对于 CLR2)和 10.0(对于 CLR4)之间保持不变 — 可能值得注意的是获取
Microsoft.FxCop.Sdk.Method
对象的过程概述,该对象只能使用 FxCop 程序集中声明的public
类型和方法来执行(无需反射技巧) )。从需要
Microsoft.FxCop.Sdk.Method
的对象的Type
开始,将此称为t
。通过静态入口点获取包含t
的程序集的AssemblyNode
获取与
t
对应的FxCopTypeNode
通过Then搜索 通过
TypeNode
的Members
字段查找其中member.Name.Name
是您要查找的方法的名称。鉴于这是一个单元测试,您应该能够安排正在检查的虚拟方法不被重载。然后调用
MyRule.Check(member)
进行测试;这将返回Problem
对象的集合,可以检查该对象以断言它包含预期结果并且仅包含预期结果。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 declaredpublic
in the FxCop assemblies (no reflection trickery required).Start with the
Type
of the object for which you want aMicrosoft.FxCop.Sdk.Method
, call thist
. Get theAssemblyNode
for the assembly containingt
via the static entrypointGet the FxCop
TypeNode
corresponding tot
viaThen search through the
TypeNode
'sMembers
field to find the one wheremember.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 ofProblem
objects, which can be inspected to assert that it contains the expected results and only the expected results.从 FxCop 中删除反射桥是 很久以前就宣布了。此外,使用未记录和不受支持的 API 并不是 FxCopUnit 中使用的方法的唯一问题,该方法没有实现误报筛选。您可能希望考虑切换到使用 FxCop 输出报告的测试方法,以便筛选丢失的违规行为和意外的违规行为。
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.
也许你应该使用 Gendarme 因为它是开源的,所以很容易遇到同样的问题。 (为什么微软不能专注于测试驱动开发?)
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?)
您可能想查看我的基于RoslynCTP的FxCop测试框架,它有执行规则并验证其标记正确问题所需的代码。由于 Roslyn 仍处于 CTP 阶段,目前无法测试所有 .NET 语言功能。
提取针对任何程序集运行规则所需的代码应该非常简单。
也欢迎任何规则贡献到这个项目:)。
http://fxcopcontrib.codeplex.com/
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/