惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - hbiftsaa

Live Writer的Code Highlighter插件 在.NET中使用COMAdmin组件管理COM+组件 使用Web Service Interface. O/R Mapping再乱弹 blog切换到 http://www.robinzhong.com/ Infopath Cookies(2) - hbiftsaa - 博客园 Infopath Cookies(1) 在Biztalk中使用Log功能:) 增强SVN的Commit权限控制 电影《幸存者游戏》的管理启示 回来了~..终于回来了... 先和大家告别一段时间了啊:) 转自DRL:向FlashFXP索取上传速度 在SP2中加速P2P类软件 爽啊~进入决赛了:) [建议] [号召] 征求博客园园标 [建议] 提问区的文章加入问题的解决方法/方案 hoho,,,要回家了:) Gmail New Features!
定制FxCop规则--检测Release版本中的TestCase
hbiftsaa · 2005-04-04 · via 博客园 - hbiftsaa

在使用NUnit进行开发的时候,TestCase是不可省的.通过TestCase,我们才可以保证所撰写的代码的正确性:)

但是由于在开发过程中是边写TestCase,边实现功能代码的,如果不注意的话,在最终的Release版本中可能会出现曾经写过的TestCase...这应该是在程序开发过程中尽量避免的:)
解决这个问题的方法有以下几种:
1,使用 #if DEBUG XXX #endif 标签.将所有的TestCase放到此标签中.这样当以Debug方式编译的时候,可以使用NUNit进行测试.当以Release方式编译的时候,其中的所有代码将会被忽略掉(相当于注释掉).这样就不会产生多余的代码了:)
2,将所有的TestCase放到单独的工程中.这样,功能代码将不可能包括测试代码,这样也可以避免上述问题..

在一个项目中,为了对所有的代码进行上述检查,如果用人工的方式来检查,那显然是不现实的.即然大家都是玩电脑的,那何不用电脑来检查了:)呵呵,对了,使用.NET下面的FxCop...

FxCop自带的Rules里面没有这样的规则,那我们得自定义一个Rule了:)

此Rule的工作流程如下:
1,得到此程序中所有的Class.
2,对每一个Class,得到其Attribute.
3,对每一个Attribute,判断是不是NUnit.Framework.TestFixtureAttribute.如果是,返回错误.

注:一定要是Release版本的程序,才可以起到真正的检测目的.

通过 定制FxCop规则示例之一:AvoidICloneableImplementation 这篇文章,我们可以学会怎么编写一个基本的FxCop的Rule...但只是文章中的东西是不能解决我们的这个问题的..我们来Google一下,看看有没有别的信息..
找到了这个:
Writing custom rules [using introspection] for FxCop 1.312 (from: James Geurts)
FxCop Custom Rule - Introspection Engine 3.12

注意这段:

对于我们现在要编写的Rule而言,我们要使用第5个函数,即: Check(TypeNode type)
通过对TypeNode类的使用,我们可以发现他有 Attributes 这样一个属性,即可以获得此Class上的所有Attribute.
其类型是 AttributeList..
AttributeList中存放的是AttributeNode,不同于.NET FX中带的Attribute.即不能直接判断AttributeNode == Attribute.
幸好TypeNode提供了FullName属性.这个属性中存放的是实际类的全名.
即NUnit.Framework.TestFixtureAttribute类的TypeNode的FullName是 "NUnit.Framework.TestFixtureAttribute"

到这里,技术上的工作都搞定了..下面是代码实现
RuleInfo.xml

AvoidNUnitTestCase.cs实现

编译成一个dll..加入到FxCop的Rules中,这样我们就可以很方便的每日Build后检测Release版本是否存在TestCase:)

All Happy~:)