单元测试自定义 FxCop 规则

发布于 2024-08-11 06:05:15 字数 453 浏览 1 评论 0 原文

我想测试我的自定义 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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吻风 2024-08-18 06:05:15

鉴于用于编写自定义规则的 API 与单元测试所需的 API 一样有完善的文档和支持 — 并且在 1.36(对于 CLR2)和 10.0(对于 CLR4)之间保持不变 — 可能值得注意的是获取 Microsoft.FxCop.Sdk.Method 对象的过程概述,该对象只能使用 FxCop 程序集中声明的 public 类型和方法来执行(无需反射技巧) )。

从需要 Microsoft.FxCop.Sdk.Method 的对象的 Type 开始,将此称为 t。通过静态入口点获取包含t的程序集的AssemblyNode

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

获取与t对应的FxCop TypeNode

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

通过Then搜索 通过 TypeNodeMembers 字段查找其中 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 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.

当梦初醒 2024-08-18 06:05:15

从 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.

岁月如刀 2024-08-18 06:05:15

也许你应该使用 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?)

知足的幸福 2024-08-18 06:05:15

您可能想查看我的基于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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文