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

推荐订阅源

博客园_首页
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Latest news
Latest news
H
Help Net Security
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Security Latest
Security Latest
B
Blog RSS Feed
V
Vulnerabilities – Threatpost
T
Threatpost
量子位
P
Proofpoint News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Recorded Future
Recorded Future
博客园 - 聂微东
博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
Forbes - Security
Forbes - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
小众软件
小众软件
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
PCI Perspectives
PCI Perspectives
T
Tenable Blog
C
Cybersecurity and Infrastructure Security Agency CISA
L
LangChain Blog
SecWiki News
SecWiki News
H
Hacker News: Front Page
WordPress大学
WordPress大学
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Schneier on Security
H
Heimdal Security Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 【当耐特】
A
About on SuperTechFans
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog

博客园 - 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~:)