簡體   English   中英

在自定義fxcop規則中跳過測試方法

[英]Skip testmethods in custom fxcop rule

我正在編寫一個自定義fxcop規則來檢查未使用的本地人。 (是的,有一個現有的,但我想從被檢查排除TestMethods。)

Introspector向我展示TestMethodAttribute在編譯時可用: 在此輸入圖像描述

我似乎無法真正檢查屬性是否存在。

我嘗試了以下方法:

方法1。

_TestAttribType = FrameworkAssemblies.Mscorlib.GetType(Identifier.For("Microsoft.VisualStudio.TestTools.UnitTesting"), Identifier.For("TestMethodAttribute"));
AttributeNode testAttribute = method.GetAttribute(_TestAttribType);
if (testAttribute != null)
    return null;

方法2。

if(method.Attributes.Any(attrib => attrib.ToString().Contains("Test")))
    return null;

方法3。

if(method.Attributes.Any(attrib => attrib.Type == typeof(TestMethodAttribute))
    return null;

方法1不起作用,因為Microsoft.VisualStudio.TestTools.Unittesting不在mscorlib中。 第二種方法也不起作用,我不知道為什么。 第三種方法無法編譯,因為FxCop-API不支持TestMethodAttribute。

如何排除在我的FxCop規則中檢查測試方法?

基於名稱的簡單方法是:

method.Attributes.Any(a => a.Type.Name.Name == "TestMethodAttribute")

如果您想要一個具有一些性能增強功能的更完整的解決方案,請查看反編譯器中MarkMembersAsStatic規則中IsVSUnitTestMethod方法的實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM