﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-C++ Programmer's Cookbook-随笔分类-VS2005/2008</title><link>http://www.cppblog.com/mzty/category/1588.html</link><description>&lt;br/&gt;  
&lt;br/&gt;
&lt;a href = "http://www.cppblog.com/mzty/archive/2007/03/02/19109.html"&gt;&lt;font size = 5 color ="#00FFFF"&gt;{C++ 基础}&lt;font/&gt;&lt;/a&gt;

&lt;a href = "http://www.cppblog.com/mzty/archive/2007/08/13/29922.html"&gt;&lt;font size = 5 color ="#00FFFF"&gt;{C++ 高级}&lt;font/&gt;&lt;/a&gt;

&lt;a href = "http://www.cppblog.com/mzty/archive/2007/04/16/22064.html"&gt;&lt;font size = 5 color ="#00FFFF"&gt;{C#界面，C++核心算法}&lt;font/&gt;&lt;/a&gt;

&lt;a href = "http://www.cppblog.com/mzty/archive/2007/03/04/19163.html"&gt;&lt;font size = 5 color ="#00FFFF"&gt;{设计模式}&lt;font/&gt;&lt;/a&gt;

&lt;a href = "
http://www.cppblog.com/mzty/archive/2007/03/04/19167.html"&gt;&lt;font size = 5 color ="#FF0000"&gt;{C#基础}&lt;font/&gt;&lt;/a&gt;





</description><language>zh-cn</language><lastBuildDate>Mon, 19 May 2008 17:09:08 GMT</lastBuildDate><pubDate>Mon, 19 May 2008 17:09:08 GMT</pubDate><ttl>60</ttl><item><title>Native c++ 和Managed 的 interop</title><link>http://www.cppblog.com/mzty/archive/2006/08/17/11354.html</link><dc:creator>梦在天涯</dc:creator><author>梦在天涯</author><pubDate>Thu, 17 Aug 2006 08:26:00 GMT</pubDate><guid>http://www.cppblog.com/mzty/archive/2006/08/17/11354.html</guid><wfw:comment>http://www.cppblog.com/mzty/comments/11354.html</wfw:comment><comments>http://www.cppblog.com/mzty/archive/2006/08/17/11354.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/mzty/comments/commentRss/11354.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mzty/services/trackbacks/11354.html</trackback:ping><description><![CDATA[
		<p>4种方法:<br /><br />There are four main ways to do interop in .NET between the managed and native worlds. COM interop can be accomplished using Runtime Callable Wrappers (RCW) and COM Callable Wrappers (CCW). The common language runtime (CLR) is responsible for type marshaling (except in the rare scenarios where a custom marshaler is used) and the cost of these calls can be expensive. You need to be careful that the interfaces are not too chatty; otherwise a large performance penalty could result. You also need to ensure that the wrappers are kept up to date with the underlying component. That said, COM interop is very useful for simple interop scenarios where you are attempting to bring in a large amount of native COM code.</p>
		<p>The second option for interop is to use P/Invoke. This is accomplished using the DllImport attribute, specifying the attribute on the method declaration for the function you want to import. Marshaling is handled according to how it has been specified in the declaration. However, DllImport is only useful if you have code that exposes the required functions through a DLL export.</p>
		<p>When you need to call managed code from native code, CLR hosting is an option. In such a scenario, the native application has to drive all of the execution: setting up the host, binding to the runtime, starting the host, retrieving the appropriate AppDomain, setting up the invocation context, locating the desired assembly and class, and invoking the operation on the desired class. This is definitely one of the most robust solutions in terms of control over what and when things happen, but it is also incredibly tedious and requires a lot of custom code.</p>
		<p>The fourth option, and quite possibly the easiest and the most performant, is to use the interop capabilities in C++. By throwing the /clr switch, the compiler generates MSIL instead of native machine code. The only code that is generated as native machine code is code that just can't be compiled to MSIL, including functions with inline asm blocks and operations that use CPU-specific intrinsics such as Streaming SIMD Extensions (SSE). The /clr switch is how the Quake II port to .NET was accomplished. The Vertigo software team spent a day porting the original C code for the game to code that successfully compiled as C++ and then threw the /clr switch. In no time they were up and running on the .NET Framework. Without adding any additional binaries and simply by including the appropriate header files, managed C++ and native C++ can call each other without any additional work on the part of the developer. The compiler handles the creation of the appropriate thunks to go back and forth between the two worlds.</p>
<img src ="http://www.cppblog.com/mzty/aggbug/11354.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mzty/" target="_blank">梦在天涯</a> 2006-08-17 16:26 <a href="http://www.cppblog.com/mzty/archive/2006/08/17/11354.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Visual C++ 编译器选项</title><link>http://www.cppblog.com/mzty/archive/2006/08/17/11336.html</link><dc:creator>梦在天涯</dc:creator><author>梦在天涯</author><pubDate>Thu, 17 Aug 2006 04:16:00 GMT</pubDate><guid>http://www.cppblog.com/mzty/archive/2006/08/17/11336.html</guid><wfw:comment>http://www.cppblog.com/mzty/comments/11336.html</wfw:comment><comments>http://www.cppblog.com/mzty/archive/2006/08/17/11336.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mzty/comments/commentRss/11336.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mzty/services/trackbacks/11336.html</trackback:ping><description><![CDATA[
		<p class="label">
				<b>来自msdn:http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/vccore/html/_core_compiler_options_listed_by_category.asp<br /><br />优化</b>
		</p>
		<div class="tablediv">
				<table class="dtTABLE" cellspacing="0" width="80%" border="1">
						<tbody>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.o1.2c_2f.o2.asp">/O1</a>
										</td>
										<td width="71%">创建小代码</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.o1.2c_2f.o2.asp">/O2</a>
										</td>
										<td width="71%">创建快速代码</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.oa.2c_2f.ow.asp">/Oa</a>
										</td>
										<td width="71%">假设没有别名</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.ob.asp">/Ob</a>
										</td>
										<td width="71%">控制内联展开</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.od.asp">/Od</a>
										</td>
										<td width="71%">禁用优化</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.og.asp">/Og</a>
										</td>
										<td width="71%">使用全局优化</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.oi.asp">/Oi</a>
										</td>
										<td width="71%">生成内部函数</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.op.asp">/Op</a>
										</td>
										<td width="71%">改善浮点数一致性</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.os.asp">/Os</a>
										</td>
										<td width="71%">代码大小优先</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.os.asp">/Ot</a>
										</td>
										<td width="71%">代码速度优先</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.oa.2c_2f.ow.asp">/Ow</a>
										</td>
										<td width="71%">假定在函数调用中使用别名</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.ox.asp">/Ox</a>
										</td>
										<td width="71%">使用最大优化 (/Ob2gity /Gs)</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.oy.asp">/Oy</a>
										</td>
										<td width="71%">省略框架指针</td>
								</tr>
						</tbody>
				</table>
		</div>
		<p class="label">
				<b>代码生成</b>
		</p>
		<div class="tablediv">
				<table class="dtTABLE" cellspacing="0" width="80%" border="1">
						<tbody>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vclrfarchminimumcpuarchitecture.asp">/arch</a>
										</td>
										<td width="71%">使用 SSE 或 SSE2 指令生成代码</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vcrefeecomcompilation.asp">/clr</a>
										</td>
										<td width="71%">启用 C++ 的托管扩展并产生在公共语言运行库上运行的输出文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.eh.asp">/EH</a>
										</td>
										<td width="71%">指定异常处理模型</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.g.asp">/G3</a>
										</td>
										<td width="71%">优化代码以优选 386 处理器。在 Visual C++ 5.0 中已经停用，编译器将忽略此选项</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.g.asp">/G4</a>
										</td>
										<td width="71%">优化代码以优选 486 处理器。在 Visual C++ 5.0 中已经停用，编译器将忽略此选项</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.g.asp">/G5</a>
										</td>
										<td width="71%">优化代码以优选 Pentium</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.g.asp">/G6</a>
										</td>
										<td width="71%">优化代码以优选 Pentium Pro、Pentium II 和 Pentium III 处理器</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.g.asp">/G7</a>
										</td>
										<td width="71%">针对 Pentium 4 或 Athlon 优化代码。</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.g.asp">/GB</a>
										</td>
										<td width="71%">与 /G6 等效；将 _M_IX86 的值设置为 600</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.gd.2c_2f.gr.2c_2f.gz.asp">/Gd</a>
										</td>
										<td width="71%">使用 <b>__cdecl</b> 调用约定</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.ge.asp">/Ge</a>
										</td>
										<td width="71%">激活堆栈探测</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.gf.asp">/GF</a>
												<br />
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.gf.asp">/Gf</a>
										</td>
										<td width="71%">启用字符串池</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.gh.asp">/Gh</a>
										</td>
										<td width="71%">调用挂钩函数 <b>_penter</b></td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vcrefghenable__pexithookfunction.asp">/GH</a>
										</td>
										<td width="71%">调用挂钩函数 <b>_pexit</b></td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vcgrfglwholeprogramoptimization.asp">/GL</a>
										</td>
										<td width="71%">启用全程序优化</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.gm_minimal.asp">/Gm</a>
										</td>
										<td width="71%">启用最小重新生成</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.gr_option.asp">/GR</a>
										</td>
										<td width="71%">启用运行时类型信息 (RTTI)</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.gd.2c_2f.gr.2c_2f.gz.asp">/Gr</a>
										</td>
										<td width="71%">使用 <b>__fastcall</b> 调用约定</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.gs.asp">/Gs</a>
										</td>
										<td width="71%">控制堆栈探测</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.gt.asp">/GT</a>
										</td>
										<td width="71%">支持使用静态线程本地存储区分配的数据的纤程安全</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.gx.asp">/GX</a>
										</td>
										<td width="71%">启用同步异常处理</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.gy.asp">/Gy</a>
										</td>
										<td width="71%">启用函数级链接</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.gd.2c_2f.gr.2c_2f.gz.asp">/Gz</a>
										</td>
										<td width="71%">使用 <b>__stdcall</b> 调用约定</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/MD</a>
										</td>
										<td width="71%">使用 MSVCRT.lib 创建多线程 DLL</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/MDd</a>
										</td>
										<td width="71%">使用 MSVCRTD.lib 创建调试多线程 DLL</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/ML</a>
										</td>
										<td width="71%">使用 LIBC.lib 创建单线程可执行文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/MLd</a>
										</td>
										<td width="71%">使用 LIBCD.lib 创建调试单线程可执行文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/MT</a>
										</td>
										<td width="71%">使用 LIBCMT.lib 创建多线程可执行文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/MTd</a>
										</td>
										<td width="71%">使用 LIBCMTD.lib 创建调试多线程可执行文件</td>
								</tr>
						</tbody>
				</table>
		</div>
		<p class="label">
				<b>输出文件</b>
		</p>
		<div class="tablediv">
				<table class="dtTABLE" cellspacing="0" width="80%" border="1">
						<tbody>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.fa.asp">/FA</a>
												<br />
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.fa.asp">/Fa</a>
										</td>
										<td width="71%">创建列表文件<br />设置列表文件名</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.fd.asp">/Fd</a>
										</td>
										<td width="71%">重命名程序数据库文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.fe.asp">/Fe</a>
										</td>
										<td width="71%">重命名可执行文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.fm.asp">/Fm</a>
										</td>
										<td width="71%">创建映射文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.fo.asp">/Fo</a>
										</td>
										<td width="71%">创建对象文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.fp.asp">/Fp</a>
										</td>
										<td width="71%">指定预编译头文件名</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.fr.2c_2f.fr.asp">/FR</a>
												<br />
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.fr.2c_2f.fr.asp">/FR</a>
										</td>
										<td width="71%">生成浏览器文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vcrefFxMergeInjectedCode.asp">/Fx</a>
										</td>
										<td width="71%">将插入的代码与源文件合并</td>
								</tr>
						</tbody>
				</table>
		</div>
		<p class="label">
				<b>调试</b>
		</p>
		<div class="tablediv">
				<table class="dtTABLE" cellspacing="0" width="80%" border="1">
						<tbody>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vclrfgsbuffersecurity.asp">/GS</a>
										</td>
										<td width="71%">缓冲区安全检查</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vcrefgz(catchrelease-builderrorsindebugbuild).asp">/GZ</a>
										</td>
										<td width="71%">与 <a href="http://msdn.microsoft.com/library/CHS/vccore/html/vcrefrtcruntimechecks.asp">/RTC1</a> 相同</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vcrefrtcruntimechecks.asp">/RTC</a>
										</td>
										<td width="71%">启用运行时错误检查</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vchowwp64detect64bitportabilityproblems.asp">/Wp64</a>
										</td>
										<td width="71%">检测 64 位可移植性问题</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.yd.asp">/Yd</a>
										</td>
										<td width="71%">将完整的调试信息放在所有对象文件中</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vcrefylinjectpchreferencefordebuglib.asp">/Yl</a>
										</td>
										<td width="71%">创建调试库时插入 PCH 引用</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.z7.2c_2f.zd.2c_2f.zi.asp">/Z7</a>
										</td>
										<td width="71%">生成与 C 7.0 兼容的调试信息</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.z7.2c_2f.zd.2c_2f.zi.asp">/Zd</a>
										</td>
										<td width="71%">生成行号</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.z7.2c_2f.zd.2c_2f.zi.asp">/Zi</a>
										</td>
										<td width="71%">生成完整的调试信息</td>
								</tr>
						</tbody>
				</table>
		</div>
		<p class="label">
				<b>预处理器</b>
		</p>
		<div class="tablediv">
				<table class="dtTABLE" cellspacing="0" width="80%" border="1">
						<tbody>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vclrfaispecifymetadatadirectories.asp">/AI</a>
										</td>
										<td width="71%">指定在解析传递到 #using 指令的文件引用时搜索的目录</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.c_uc.asp">/C</a>
										</td>
										<td width="71%">在预处理期间保留注释</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.d.asp">/D</a>
										</td>
										<td width="71%">定义常数和宏</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.e.asp">/E</a>
										</td>
										<td width="71%">将预处理器输出复制到标准输出</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.ep.asp">/EP</a>
										</td>
										<td width="71%">将预处理器输出复制到标准输出</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.fi.asp">/Fl</a>
										</td>
										<td width="71%">预处理指定的包含文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vclrffunameforcedusingfile.asp">/FU</a>
										</td>
										<td width="71%">强制使用文件名，就像它已被传递到 #using 指令一样</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.i.asp">/I</a>
										</td>
										<td width="71%">在目录中搜索包含文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.p.asp">/P</a>
										</td>
										<td width="71%">将预处理器输出写入文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.u.2c_2f.u.asp">/U</a>
										</td>
										<td width="71%">移除预定义宏</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.u.2c_2f.u.asp">/u</a>
										</td>
										<td width="71%">移除所有的预定义宏</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.x.asp">/X</a>
										</td>
										<td width="71%">忽略标准包含目录</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a class="POPUP" href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.z7.2c_2f.zd.2c_2f.zi.asp">/ZI</a>
										</td>
										<td width="71%">将调试信息包含在与“编辑并继续”兼容的程序数据库中</td>
								</tr>
						</tbody>
				</table>
		</div>
		<p class="label">
				<b>语言</b>
		</p>
		<div class="tablediv">
				<table class="dtTABLE" cellspacing="0" width="80%" border="1">
						<tbody>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.vd.asp">/vd</a>
										</td>
										<td width="71%">取消或启用隐藏的 vtordisp 类成员</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.vmb.2c_2f.vmg.asp">/vmb</a>
										</td>
										<td width="71%">对指向成员的指针使用最佳的基</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.vmb.2c_2f.vmg.asp">/vmg</a>
										</td>
										<td width="71%">对指向成员的指针使用完全一般性</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.vmm.2c_2f.vms.2c_2f.vmv.asp">/vmm</a>
										</td>
										<td width="71%">声明多重继承</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.vmm.2c_2f.vms.2c_2f.vmv.asp">/vms</a>
										</td>
										<td width="71%">声明单一继承</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.vmm.2c_2f.vms.2c_2f.vmv.asp">/vmv</a>
										</td>
										<td width="71%">声明虚拟继承</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.za.2c_2f.ze.asp">/Za</a>
										</td>
										<td width="71%">禁用语言扩展</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vcrefzcconformance.asp">/Zc</a>
										</td>
										<td width="71%">在 <a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.za.2c_2f.ze.asp">/Ze</a> 下指定标准行为</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.za.2c_2f.ze.asp">/Ze</a>
										</td>
										<td width="71%">启用语言扩展</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.zg.asp">/Zg</a>
										</td>
										<td width="71%">生成函数原型</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.zl.asp">/Zl</a>
										</td>
										<td width="71%">从 .obj 文件中移除默认库名</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.zp.asp">/Zp</a>
												<i>n</i>
										</td>
										<td width="71%">封装结构成员</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.zs.asp">/Zs</a>
										</td>
										<td width="71%">只检查语法</td>
								</tr>
						</tbody>
				</table>
		</div>
		<p class="label">
				<b>链接</b>
		</p>
		<div class="tablediv">
				<table class="dtTABLE" cellspacing="0" width="80%" border="1">
						<tbody>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.f.asp">/F</a>
										</td>
										<td width="71%">设置堆栈大小</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/LD</a>
										</td>
										<td width="71%">创建动态链接库</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/LDd</a>
										</td>
										<td width="71%">创建调试动态链接库</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.link.asp">/link</a>
										</td>
										<td width="71%">将指定的选项传递给 LINK</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/MD</a>
										</td>
										<td width="71%">使用 MSVCRT.lib 编译以创建多线程 DLL</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/MDd</a>
										</td>
										<td width="71%">使用 MSVCRTD.lib 编译以创建调试多线程 DLL</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/ML</a>
										</td>
										<td width="71%">使用 LIBC.lib 编译以创建单线程可执行文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/MLd</a>
										</td>
										<td width="71%">使用 LIBCD.lib 编译以创建调试单线程可执行文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/MT</a>
										</td>
										<td width="71%">使用 LIBCMT.lib 编译以创建多线程可执行文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.md.2c_2f.ml.2c_2f.mt.2c_2f.ld.asp">/MTd</a>
										</td>
										<td width="71%">使用 LIBCMTD.lib 编译以创建调试多线程可执行文件</td>
								</tr>
						</tbody>
				</table>
		</div>
		<p class="label">
				<b>预编译头</b>
		</p>
		<div class="tablediv">
				<table class="dtTABLE" cellspacing="0" width="80%" border="1">
						<tbody>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vclrfyignoreprecompiledheaderoptions.asp">/Y-</a>
										</td>
										<td width="71%">忽略当前生成中的所有其他预编译头编译器选项</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.yc.asp">/Yc</a>
										</td>
										<td width="71%">创建预编译头文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.yd.asp">/Yd</a>
										</td>
										<td width="71%">将完整的调试信息放在所有对象文件中</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.yu.asp">/Yu</a>
										</td>
										<td width="71%">在生成期间使用预编译头文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.yx.asp">/YX</a>
										</td>
										<td width="71%">自动处理预编译头</td>
								</tr>
						</tbody>
				</table>
		</div>
		<p class="label">
				<b>杂项</b>
		</p>
		<div class="tablediv">
				<table class="dtTABLE" cellspacing="0" width="80%" border="1">
						<tbody>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vctbsspecifyresponsefile.asp">@</a>
										</td>
										<td width="71%">指定响应文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.help.asp">/?</a>
										</td>
										<td width="71%">列出编译器选项</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.c_lc.asp">/c</a>
										</td>
										<td width="71%">编译但不链接</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.h.asp">/H</a>
										</td>
										<td width="71%">限制外部（公共）名称的长度</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.help.asp">/HELP</a>
										</td>
										<td width="71%">列出编译器选项</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.j.asp">/J</a>
										</td>
										<td width="71%">更改默认的 <b>char</b> 类型</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vclrfnologosuppressdisplayofbanner.asp">/nologo</a>
										</td>
										<td width="71%">取消显示登录版权标志</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.qi0f.asp">/QI0f</a>
										</td>
										<td width="71%">确保 Pentium 0F 指令没有问题</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.qifdiv.asp">/QIfdiv</a>
										</td>
										<td width="71%">FDIV、FPREM、FPTAN 和 FPATAN 指令有缺陷的 Intel Pentium 微处理器的变通方法</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vcrefqifistsuppress_ftol.asp">QIfist</a>
										</td>
										<td width="71%">当需要从浮点类型转换为整型时取消 Helper 函数 <b>_ftol</b> 的调用</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vcrefshowincludes.asp">/showIncludes</a>
										</td>
										<td width="71%">在编译期间显示所有包含文件的列表</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.tc.2c_2f.tp.asp">/Tc</a>
												<br />
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.tc.2c_2f.tp.asp">/TC</a>
										</td>
										<td width="71%">指定 C 源文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.tc.2c_2f.tp.asp">/Tp</a>
												<br />
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.tc.2c_2f.tp.asp">/TP</a>
										</td>
										<td width="71%">指定 C++ 源文件</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.v.asp">/V</a>
										</td>
										<td width="71%">设置版本字符串</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.w.2c_2f.w.asp">/W</a>
										</td>
										<td width="71%">设置警告等级</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.w.2c_2f.w.asp">/w</a>
										</td>
										<td width="71%">禁用所有警告</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.w.2c_2f.w.asp">/Wall</a>
										</td>
										<td width="71%">启用所有警告，包括默认情况下禁用的警告</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/vcrefwlenableonelinediagnostics.asp">/WL</a>
										</td>
										<td width="71%">在从命令行编译 C++ 源代码时启用错误信息和警告消息的单行诊断</td>
								</tr>
								<tr valign="top">
										<td width="29%">
												<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_.2f.zm.asp">/Zm</a>
										</td>
										<td width="71%">指定预编译头内存分配限制</td>
								</tr>
						</tbody>
				</table>
		</div>
		<h4 class="dtH4">请参见</h4>
		<p>
				<a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_overviews.3a_.compiling_and_linking.asp">C/C++ 生成参考</a> | <a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_compiler_reference.asp">编译器选项</a> | <a href="http://msdn.microsoft.com/library/CHS/vccore/html/_core_set_compiler_options.asp">设置编译器选项</a></p>
<img src ="http://www.cppblog.com/mzty/aggbug/11336.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mzty/" target="_blank">梦在天涯</a> 2006-08-17 12:16 <a href="http://www.cppblog.com/mzty/archive/2006/08/17/11336.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>调试breakpoint高级</title><link>http://www.cppblog.com/mzty/archive/2006/08/16/11289.html</link><dc:creator>梦在天涯</dc:creator><author>梦在天涯</author><pubDate>Wed, 16 Aug 2006 06:47:00 GMT</pubDate><guid>http://www.cppblog.com/mzty/archive/2006/08/16/11289.html</guid><wfw:comment>http://www.cppblog.com/mzty/comments/11289.html</wfw:comment><comments>http://www.cppblog.com/mzty/archive/2006/08/16/11289.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mzty/comments/commentRss/11289.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mzty/services/trackbacks/11289.html</trackback:ping><description><![CDATA[breakpoint的右键菜单:<br><img style="WIDTH: 585px; HEIGHT: 264px" height=264 alt=o_tiaoshi2.jpg src="http://www.cppblog.com/images/cppblog_com/mzty/2021/o_tiaoshi2.jpg" width=585 border=0><br><br>
<div class=itemTitleStyle><font style="BACKGROUND-COLOR: #ff1493">Tracepoints</font></div>
<div class=itemBodyStyle>
<p>When you have a "mystery" bug to solve, tracepoints are a vital part of your debugging arsenal. Single stepping and looking through code can be S-L-O-O-O-O-W and if you don't even know what you're looking for, it can consume hours and hours of effort. Tracepoints really speed things up. They're like breakpoints that don't break. In a way, they go back to the old "printf debugging" -- but you don't need to make code changes and recompile to change them.</p>
<p>To set a tracepoint, first set a breakpoint, then right-click on the red dot that appears in the margin and choose When Hit:</p>
<p><img src="http://www.gregcons.com/KateBlog/content/binary/tracepoint1.gif" border=0></p>
<p>In the dialog that appears, click the Print A Message box and edit the starter message you are given. You can include any expression in braces and it will be evaluated when control reaches the tracepoint:</p>
<p><img src="http://www.gregcons.com/KateBlog/content/binary/tracepoint2.gif" border=0></p>
<p>Leave the Continue Execution box checked so that you don't break. Tracepoints are identified by red diamonds instead of red dots:</p>
<p><img src="http://www.gregcons.com/KateBlog/content/binary/tracepoint3.gif" border=0></p>
<p>The output from the tracepoints appears in the output window of your debug session:</p>
<p><img style="WIDTH: 554px; HEIGHT: 126px" height=126 src="http://www.gregcons.com/KateBlog/content/binary/tracepoint4.gif" width=554 border=0></p>
<p>You can set up something suspicious, let it run, then pore through the tracepoint output and see what you learn. It's a huge timesaver when you're tackling a "we don't even know where to start" bug. Plus, if the issue is related to threading or async issues in any way (and you know me, I keep preaching we will all be facing async issues eventually) then you don't have to worry that pausing execution suppresses the collisions. I recently helped a client solve a big hairy this-stuff-fails-for-our-biggest-customer-only bug using tracepoints... and a few other tricks I will cover in upcoming posts.</p>
<div class=itemTitleStyle><font style="BACKGROUND-COLOR: #ff1493">Conditional breakpoints</font></div>
<div class=itemBodyStyle>
<p>Conditional breakpoints were another really important tool in solving a recent "only happens at the big installations" bug in a big and complicated C++ application. We wanted to debug the code with as few rebuilds as possible and we really didn't know how parts of it worked at all. <a href="http://www.gregcons.com/KateBlog/ct.ashx?id=eb64dfe1-fde4-413e-be11-2445b3f7d5e1&amp;url=http%3a%2f%2fwww.gregcons.com%2fKateBlog%2fCommentView.aspx%3fguid%3dc5590e77-c53e-4a2a-a632-80e10fe611bb" ?=""><strong><font color=#004183>Tracepoints </font></strong></a>helped us to figure out a lot of it in a short time. We didn't have a decent repro case though, so here's what we did:</p>
<ul>
    <li>created a script to add an audit table and some triggers that write to it
    <li>created a script to remove those triggers and the table
    <li>created a batch file to run a server in verbose mode, and redirect the output to a text file (remembering DOS commands sure can be helpful...&nbsp;&nbsp; <strong>myapp.exe -verbose &gt;logfile.txt</strong> was the magic phrase.)
    <li>wrote some instructions for the support guy to grab a backup of the database, run the first script, run the batch file, let it run all night, then in the morning grab another backup of the database, run the second script, and stop the batch file.</li>
</ul>
<p>Now we ran some queries on the morning version of the database to confirm that at least one X was missing a Y that should have been calculated overnight. We even had the Xid. Thanks to the tracepoint work of the previous day we knew where to be suspicious. A little digging in the trigger output told us whether the problem was "didn't get added" or "got added, but then got deleted". The last step was conditional breakpoints. These let us say "only stop here when you're processing the X with this Xid." </p>
<p><img src="http://www.gregcons.com/KateBlog/content/binary/condbreakpoint.gif" border=0></p>
<p>This really saves time when you just need to drill into what's happening in the case that is going wrong. You get to it by right clicking the red dot or diamond and choosing Condition. </p>
<br>
<div class=itemTitleStyle><font style="BACKGROUND-COLOR: #ff1493">One more breakpoint tip - hit count</font></div>
<div class=itemBodyStyle>
<p><a href="http://www.gregcons.com/KateBlog/ct.ashx?id=d9e91a84-e206-4e03-b21f-b38fcf70b138&amp;url=http%3a%2f%2fwww.gregcons.com%2fKateBlog%2fCommentView.aspx%3fguid%3deb64dfe1-fde4-413e-be11-2445b3f7d5e1" ?=""><strong><font color=#005bba>Conditional breakpoints</font></strong></a> are cool when you know the value of one variable that is associated with trouble: this only blows up&nbsp;when Xid&nbsp;is 1234. They can also be used to reduce the boredom of single stepping. Say you've got some loop that goes around a few hundred times building up some string or array or something. You don't want to keep going round and round and round... it would be nice to jump ahead to halfway through, for example, and see how things are looking. Now if this is a for loop with a handy named variable, say</p>
<p>&#160;</p>
<pre>for (int i = 0; i&lt;1000; i++)</pre>
<p>&#160;</p>
<p>Then you can use a conditional breakpoint and say "stop when i is 200". Or you could be clever and stop when <strong>i % 200 </strong>is 0 -- so that's every 200 times. But what if there's no i? What if you're going through a file or a recordset/dataset/resultset and you're going until the end? You don't want to add some fake variable that gets incremented each time through the loop, just so you can set a breakpoint that breaks on certain values of that variable. Instead you want to use the hit count property of the breakpoint. Just right click the red dot and choose Hit Count. There you can set the breakpoint to break whenever it's hit, just the 10th time it's hit, every tenth time (10, 20, 30 etc) or every time after the 10th time Or, of course, whatever number you want instead of 10:</p>
<p><img src="http://www.gregcons.com/KateBlog/content/binary/hitcount.gif" border=0></p>
<p>Don't debug the slow way when there are tools to make you so much faster!<br><br><font style="BACKGROUND-COLOR: #ff1493">Filter</font>&nbsp;&nbsp; 下次再介绍:<br><br><img height=336 alt=o_tiaoshi.jpg src="http://www.cppblog.com/images/cppblog_com/mzty/2021/o_tiaoshi.jpg" width=406 border=0><br><br><br><font style="BACKGROUND-COLOR: #ee82ee">注意:c#的也可以用的啊....<br><br><br><br>来自:http://www.gregcons.com/KateBlog/CategoryView.aspx?category=C%2B%2B<br></font></p>
</div>
</div>
</div><img src ="http://www.cppblog.com/mzty/aggbug/11289.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mzty/" target="_blank">梦在天涯</a> 2006-08-16 14:47 <a href="http://www.cppblog.com/mzty/archive/2006/08/16/11289.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TestRunner</title><link>http://www.cppblog.com/mzty/archive/2006/07/29/10675.html</link><dc:creator>梦在天涯</dc:creator><author>梦在天涯</author><pubDate>Sat, 29 Jul 2006 05:31:00 GMT</pubDate><guid>http://www.cppblog.com/mzty/archive/2006/07/29/10675.html</guid><wfw:comment>http://www.cppblog.com/mzty/comments/10675.html</wfw:comment><comments>http://www.cppblog.com/mzty/archive/2006/07/29/10675.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mzty/comments/commentRss/10675.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mzty/services/trackbacks/10675.html</trackback:ping><description><![CDATA[
		<h2>testrunner</h2>
		<p>
				<a href="http://morningspace.51.net/resource/cppunit/textui.html">
				</a> </p>
		<h2>
				<a href="http://www.mailframe.net/Products/TestRunner/default.aspx">http://www.mailframe.net/Products/TestRunner/default.aspx</a>
		</h2>
		<h2>TestRunner Provides </h2>
		<ul>
				<li>Test Driven Development inside Visual Studio 2005 
</li>
				<li>Integrated debugging from a simple context menu in the editor 
</li>
				<li>File, method, and line level code coverage in the editor 
</li>
				<li>Graphical test feedback directly in the Visual Studio editor 
</li>
				<li>Console &amp; Trace redirection to the Output Window 
</li>
				<li>Non-blocking test runs -- program while you test on a background thread 
</li>
				<li>Test failure tool tips and line markers 
</li>
				<li>Ultra Performance tested with suites of 1400 unit tests </li>
		</ul>
		<p> </p>
		<ul>
				<li>TestRunner will change the way you NUnit test by making it a seamless experience with programming. Tests run in the background and richly interact with the Visual Studio editor instead of a separate program, saving you from the window shuffle or losing your place in your code. <span class="hot">Unit testing</span>, <span class="hot">code coverage</span>, and now <span class="hot">performance profiling</span> all in one convenient tool. </li>
		</ul>
		<p>cppunit:   <a href="http://morningspace.51.net/resource/cppunit/textui.html">http://morningspace.51.net/resource/cppunit/textui.html</a><br /> </p>
		<p>------------------------------------------------------------------------------------------------------------------------------------<br /></p>
		<div class="postText">TestDriven.NET网址：<a href="http://www.testdriven.net/">http://www.testdriven.net/</a><br />下载地址：<a href="http://www.testdriven.net/Default.aspx?tabid=27">http://www.testdriven.net/Default.aspx?tabid=27</a><br />博客园下载地址：<a href="http://downloads.cnblogs.com/TestDriven.NET-1.0.915d.zip">http://downloads.cnblogs.com/TestDriven.NET-1.0.915d.zip</a><br />使用帮助：<a href="http://www.testdriven.net/Default.aspx?tabid=29">http://www.testdriven.net/Default.aspx?tabid=29</a><br /><br />使用感受： <br />1、与VS.NET集成得很不错, 使用起来很方便, 比如我们运行一个测试方法，直接在这个方法上点击右键Run Test(s): <br /><img height="250" hspace="5" src="http://www.cnblogs.com/images/cnblogs_com/dudu/testdivern001.gif" width="483" align="baseline" /><br />然后在VS.NET的输出窗口中输出测试结果。Cool! 错误信息会显示在任务窗口。 <br />2、在“解决方案资源管理器”中，可以选中相应的项目点击右键运行测试，可以通过Test With NUnit GUI启动NUnit GUI运行测试。 <br />3、通过Tests With Debugger, 在Debug模式中运行测试， <br />4、不需要另外安装NUnit。TestDriven.NET已经自带NUnit。<br /><br />------------------------------------------------------------------<br /><br />nunit的使用:<br /><br /><a href="http://ltp.cnblogs.com/archive/2005/12/03/289720.html">http://ltp.cnblogs.com/archive/2005/12/03/289720.html</a></div>
<img src ="http://www.cppblog.com/mzty/aggbug/10675.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mzty/" target="_blank">梦在天涯</a> 2006-07-29 13:31 <a href="http://www.cppblog.com/mzty/archive/2006/07/29/10675.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>升级VC7项目到VC8的注意事项(转)</title><link>http://www.cppblog.com/mzty/archive/2006/07/24/10386.html</link><dc:creator>梦在天涯</dc:creator><author>梦在天涯</author><pubDate>Mon, 24 Jul 2006 00:47:00 GMT</pubDate><guid>http://www.cppblog.com/mzty/archive/2006/07/24/10386.html</guid><wfw:comment>http://www.cppblog.com/mzty/comments/10386.html</wfw:comment><comments>http://www.cppblog.com/mzty/archive/2006/07/24/10386.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mzty/comments/commentRss/10386.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mzty/services/trackbacks/10386.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 升级VC7项目到VC8的注意事项																																																																																																																																																																																...&nbsp;&nbsp;<a href='http://www.cppblog.com/mzty/archive/2006/07/24/10386.html'>阅读全文</a><img src ="http://www.cppblog.com/mzty/aggbug/10386.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mzty/" target="_blank">梦在天涯</a> 2006-07-24 08:47 <a href="http://www.cppblog.com/mzty/archive/2006/07/24/10386.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用API实现1/1000秒的性能测试类</title><link>http://www.cppblog.com/mzty/archive/2006/06/16/8636.html</link><dc:creator>梦在天涯</dc:creator><author>梦在天涯</author><pubDate>Fri, 16 Jun 2006 06:07:00 GMT</pubDate><guid>http://www.cppblog.com/mzty/archive/2006/06/16/8636.html</guid><wfw:comment>http://www.cppblog.com/mzty/comments/8636.html</wfw:comment><comments>http://www.cppblog.com/mzty/archive/2006/06/16/8636.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/mzty/comments/commentRss/8636.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mzty/services/trackbacks/8636.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: //				 MbUnit Test Framework				//				 				//				 Copyright (c) 2004 Jonathan de Halleux				//																								//				 This software is provided 'as-is', without any expr...&nbsp;&nbsp;<a href='http://www.cppblog.com/mzty/archive/2006/06/16/8636.html'>阅读全文</a><img src ="http://www.cppblog.com/mzty/aggbug/8636.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mzty/" target="_blank">梦在天涯</a> 2006-06-16 14:07 <a href="http://www.cppblog.com/mzty/archive/2006/06/16/8636.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Visual Studio 2005 Team System：构建健壮而可靠的软件</title><link>http://www.cppblog.com/mzty/archive/2006/06/16/8630.html</link><dc:creator>梦在天涯</dc:creator><author>梦在天涯</author><pubDate>Fri, 16 Jun 2006 04:16:00 GMT</pubDate><guid>http://www.cppblog.com/mzty/archive/2006/06/16/8630.html</guid><wfw:comment>http://www.cppblog.com/mzty/comments/8630.html</wfw:comment><comments>http://www.cppblog.com/mzty/archive/2006/06/16/8630.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mzty/comments/commentRss/8630.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mzty/services/trackbacks/8630.html</trackback:ping><description><![CDATA[
		<h1>Visual Studio 2005 Team System：构建健壮而可靠的软件</h1>
		<h2 class="subtitle">
		</h2>
		<div class="date">发布日期： 6/21/2005<span class="datePipe"> | </span>更新日期： 6/21/2005</div>
		<div class="overview">
				<p>Kamran Iqbal<br />Microsoft Corporation</p>
				<p>适用于：</p>
				<p>Microsoft Visual Studio® 2005 Team System</p>
				<p>
						<b>摘要：</b>描述 Visual Studio 2005 Team System 提供的代码分析和性能工具。</p>
				<p>
						<b>注</b> 本文档于产品投入生产之前编写，因此您可能会发现这里所包含的细节与发布的产品有不一致的地方。文中的信息均依据撰写本文时的产品状况，仅供您在规划时参考。如有更改，恕不另行通知。Microsoft 拥有本文档中的主题所涉及的专利、专利应用程序、商标、版权或其他的知识产权。除非 Microsoft 以任何书面许可协议明确提供，向您提供本文档并没给予您使用这些专利、商标、版权或其他知识产权的任何许可证。</p>
		</div>
		<center>
				<img title="" height="6" alt="*" src="http://img.microsoft.com/library/gallery/templates/MNP2.Common/images/3squares.gif" width="30" border="0" />
		</center>
		<div style="HEIGHT: 18px">
		</div>
		<h5 style="PADDING-TOP: 2px">本页内容</h5>
		<table style="MARGIN-TOP: 7px; MARGIN-BOTTOM: 12px" cellspacing="0" cellpadding="0" border="0">
				<tbody>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#EDC">
												<img height="9" alt="简介" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#EDC">简介</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#EGC">
												<img height="9" alt="分析工具支持" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#EGC">分析工具支持</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#ETC">
												<img height="9" alt="我们的解决方案" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#ETC">我们的解决方案</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#E5C">
												<img height="9" alt="Visual Studio 中的工具集成" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#E5C">Visual Studio 中的工具集成</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#E1GAC">
												<img height="9" alt="小结" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#E1GAC">小结</a>
								</td>
						</tr>
				</tbody>
		</table>
		<a name="EDC">
		</a>
		<h2>简介</h2>
		<p>要开发健壮而可靠的软件，开发人员需要一套集成分析工具，来帮助他们在开发的早期检测代码缺陷和性能问题。</p>
		<div style="MARGIN-TOP: 3px; MARGIN-BOTTOM: 10px">
				<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#top">
						<img height="9" alt="返回页首" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_up.gif" width="7" border="0" />
				</a>
				<a class="topOfPage" href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#top">返回页首</a>
		</div>
		<a name="EGC">
		</a>
		<h2>分析工具支持</h2>
		<p>过去，由于 Visual Studio 中缺少对代码分析的支持，因此开发人员不得不购买第三方工具，构建自定义分析工具，或者发布未经分析的代码。 </p>
		<table cellspacing="0" cellpadding="0" border="0">
				<tbody>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>花费额外的成本购买第三方工具。 </p>
										<p>从众多的分析工具中选择一个既能满足软件分析的要求又在预算限制范围之内的工具，未必总是一项轻松的任务。另外，这些工具可能没有集成到 Visual Studio 集成开发环境 (IDE) 中，因此通常需要花费额外的时间来学习如何顺利地使用它们。 </p>
								</td>
						</tr>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>构建自定义分析工具。 </p>
										<p>构建自定义分析工具需要资源、技能和经验。这对大多数公司中的开发团队而言是不现实的，仅仅是少数公司可以担负得起的选择。 </p>
								</td>
						</tr>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>发布未经分析的代码。 </p>
										<p>在工时期限和预算限制紧张的情况下，发布未经分析的代码似乎是一种正确的方法，但如果软件在部署之后发生故障，可能证明采用这种错误方法的代价不菲。 </p>
								</td>
						</tr>
				</tbody>
		</table>
		<div style="MARGIN-TOP: 3px; MARGIN-BOTTOM: 10px">
				<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#top">
						<img height="9" alt="返回页首" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_up.gif" width="7" border="0" />
				</a>
				<a class="topOfPage" href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#top">返回页首</a>
		</div>
		<a name="ETC">
		</a>
		<h2>我们的解决方案</h2>
		<p>在 Visual Studio 2005 Team System 中，开发人员将会看到一套全新的、完全集成到 IDE 中的分析工具。这种分析工具与开发环境的紧密集成，可以帮助开发人员在产品开发的早期检测并修复代码缺陷和性能问题。这还可以帮助团队有效且高效地管理软件开发生命周期（Software Development Life Cycle，SDLC）。</p>
		<p>在 Visual Studio 2005 Team System 中，有两类分析工具可用于构建健壮而可靠的软件： </p>
		<table cellspacing="0" cellpadding="0" border="0">
				<tbody>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>代码分析工具 </p>
								</td>
						</tr>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>性能工具 </p>
								</td>
						</tr>
				</tbody>
		</table>
		<p>在开发环境中集成分析工具可以帮助开发人员检测与编码、性能及安全性相关的问题。此外，可将代码分析工具作为签入策略的一部分用于每日构建 (nightly build) 过程，使开发团队能够在将代码签入到源树 (source tree) 之前修正缺陷。通过在开发的早期（而不是后期）更正问题，团队可降低修复代码缺陷的总体成本。</p>
		<p>除了完全集成到 IDE 中之外，开发人员还可以从命令行使用这些工具。</p>
		<div style="MARGIN-TOP: 3px; MARGIN-BOTTOM: 10px">
				<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#top">
						<img height="9" alt="返回页首" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_up.gif" width="7" border="0" />
				</a>
				<a class="topOfPage" href="http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx#top">返回页首</a>
		</div>
		<a name="E5C">
		</a>
		<h2>Visual Studio 中的工具集成</h2>
		<p>在 Visual Studio 2005 Team System 中，通过使用代码分析工具和性能工具，开发人员可以对代码进行静态和动态分析。</p>
		<p>代码分析工具</p>
		<p>代码分析工具的目标是使开发人员能够对其项目进行静态分析，以检测和修复代码缺陷。为了实现这个目标，Visual Studio 2005 Team System 中包含了两个工具： </p>
		<table cellspacing="0" cellpadding="0" border="0">
				<tbody>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>PRE<i>f</i>ast </p>
								</td>
						</tr>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>FxCop </p>
								</td>
						</tr>
				</tbody>
		</table>
		<p>PRE<i>f</i>ast</p>
		<p>PRE<i>f</i>ast 是一个静态分析工具，它为开发人员提供有关其 C/C++ 源代码中可能存在缺陷的信息。PRE<i>f</i>ast 报告的常见代码错误包括缓冲区溢出、未初始化内存、空指针取消引用、内存泄漏和资源短缺。</p>
		<p>
				<em>IDE </em>
				<em>集成</em>
		</p>
		<p>为了使开发人员能够自然地使用分析工具，可以将 PRE<i>f</i>ast 完全集成到 IDE 中。通过在项目的 <b>Property Pages</b> 上选择 <b>Yes (/prefast)</b>，开发人员可以轻松启用 PRE<i>f</i>ast，如图 1 所示。</p>
		<div style="WIDTH: 470px">
				<img height="317" alt="" src="http://www.microsoft.com/china/msdn/library/langtool/vsts/art/vsts-dev-fig01.gif" width="470" border="0" />
				<br />
				<p class="figureCaption">
						<b>图</b>
						<b> 1. </b>
						<b>启用</b>
						<b> PRE</b>
						<b>
								<i>f</i>
						</b>
						<b>ast</b>
				</p>
				<div class="figureRule">
				</div>
		</div>
		<p>在生成过程中，为源代码生成的任何 PRE<i>f</i>ast 警告都出现在 <b>Error List</b> 中。这些警告包括缺陷路径信息（如果有）；双击突出显示缺陷路径的警告，可以进入代码编辑器中的警告，如图 2 所示。</p>
		<div style="WIDTH: 470px">
				<img height="226" alt="" src="http://www.microsoft.com/china/msdn/library/langtool/vsts/art/vsts-dev-fig02.gif" width="470" border="0" />
				<br />
				<p class="figureCaption">
						<b>图</b>
						<b> 2. PRE</b>
						<b>
								<i>f</i>
						</b>
						<b>ast </b>
						<b>警告</b>
				</p>
				<div class="figureRule">
				</div>
		</div>
		<p>
				<i>#pragma support</i>
		</p>
		<pre class="codeSample">Developers can use the #pragma directive to treat warnings as errors, or disable warnings, as shown below.
#pragma warning (error: 6260)  //treat warning 6260 as an error
#pragma warning (disable: 6011)  //disable warning 6011
</pre>
		<p>
				<em>批注支持</em>
		</p>
		<p>PRE<i>f</i>ast 还支持批注以改善代码分析的精确性。批注提供有关函数参数和返回类型的之前和之后条件的附加信息。</p>
		<pre class="codeSample">#include 
[SA_Post( MustCheck=SA_Yes )] double* CalcSquareRoot
( 
   [SA_Pre( Null=SA_No )] double* source, 
   unsigned int size
)
</pre>
		<p>在上面的示例中： </p>
		<table cellspacing="0" cellpadding="0" border="0">
				<tbody>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>[SA_Post ( MustCheck=SA_Yes)] 要求调用方检查 CalcSquareRoot 的返回值 </p>
								</td>
						</tr>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>[SA_Pre ( Null=SA_No)] 要求调用方将非空参数“source”传递给 CalcSquareRoot </p>
								</td>
						</tr>
				</tbody>
		</table>
		<p>
				<em>命令行支持</em>
		</p>
		<p>除了与开发环境完全集成之外，开发人员还可以从命令行使用 PRE<i>f</i>ast，如下所示。</p>
		<pre class="codeSample">C:\&gt;cl /prefast Sample.cpp
</pre>
		<p>FxCop</p>
		<p>FxCop 是一个静态分析工具，它分析托管代码程序集并报告有关程序集的信息，例如，与<a href="http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconNETFrameworkDesignGuidelines.asp" target="_blank"> Microsoft .NET Framework Design Guidelines</a> 中阐述的编程和设计规则相冲突。FxCop 表示在分析过程中作为规则执行的检查。规则是一段托管代码，该代码分析程序集并返回有关它发现结果的消息。规则消息识别任何相关的编程和设计问题，并在可能的情况下，提供有关如何修复目标的信息。</p>
		<p>
				<em>IDE </em>
				<em>集成</em>
		</p>
		<p>为了使开发人员能够自然地使用分析工具，开发人员可以在项目的 <b>Property Pages</b> 上选择 <b>Run FxCop</b>，如图 3 所示。</p>
		<div style="WIDTH: 470px">
				<img height="335" alt="" src="http://www.microsoft.com/china/msdn/library/langtool/vsts/art/vsts-dev-fig03.gif" width="470" border="0" />
				<br />
				<p class="figureCaption">
						<b>图</b>
						<b> 3. </b>
						<b>启用</b>
						<b> FxCop</b>
				</p>
				<div class="figureRule">
				</div>
		</div>
		<p>有关包含或排除规则，将规则作为警告或错误的附加选项，也可以在 <b>Property Pages</b> 中找到，如图 4 所示。</p>
		<div style="WIDTH: 470px">
				<img height="128" alt="" src="http://www.microsoft.com/china/msdn/library/langtool/vsts/art/vsts-dev-fig04.gif" width="470" border="0" />
				<br />
				<p class="figureCaption">
						<b>图</b>
						<b> 4. FxCop </b>
						<b>规则</b>
				</p>
				<div class="figureRule">
				</div>
		</div>
		<p>一旦启用 FxCop，在生成过程中，FxCop 就会在 <b>Error List</b> 中报告冲突，如图 5 所示。</p>
		<div style="WIDTH: 470px">
				<img height="183" alt="" src="http://www.microsoft.com/china/msdn/library/langtool/vsts/art/vsts-dev-fig05.gif" width="470" border="0" />
				<br />
				<p class="figureCaption">
						<b>图</b>
						<b> 5. FxCop </b>
						<b>规则冲突</b>
				</p>
				<div class="figureRule">
				</div>
		</div>
		<p>
				<b>Error List</b> 提供有关规则冲突的信息和建议，以便于您纠正错误。只需在 <b>Error List</b> 中双击规则冲突，即可轻松地定位到触犯了规则的代码行。</p>
		<p>
				<em>MSBuild </em>
				<em>集成</em>
		</p>
		<p>Visual Studio 的下一个版本从根本上改进了生成过程，其方法是引入一个新的、名为 MSBuild 的生成引擎。可以通过 MSBuild 系统调用 FxCop。这使开发人员能够从命令行运行托管项目上的 FxCop。</p>
		<p>性能工具</p>
		<p>在 Visual Studio 2005 Team System 中，开发人员可以通过性能工具来度量、计算和定位代码中与性能相关的问题。这些工具都完全集成到 IDE 中，以提供无缝、友好的用户体验。</p>
		<p>性能工具支持两种分析方法： </p>
		<table cellspacing="0" cellpadding="0" border="0">
				<tbody>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>采样 </p>
								</td>
						</tr>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>使用仪器 </p>
								</td>
						</tr>
				</tbody>
		</table>
		<p>在采样的过程中，数据收集基础结构将定期中断正在执行的应用程序，以确定正在执行的是哪个函数，并增加函数的采样计数。它存储有关准备函数调用的调用堆栈的信息。</p>
		<p>应用程序退出后，收集到的所有数据会生成为一个报告文件，您可以使用 IDE 中集成的报告功能轻松地查看该文件。</p>
		<p>采样的优势在于低开销，因为这只是定期中断应用程序。这使得应用程序的行为能够更接近实际情况。这种方法的缺点是，它只能获取已采样函数的相关性能数据。有可能没有对需要采样的函数进行采样，因而无法获取有关该函数的信息。 </p>
		<p>使用仪器的优势在于可以收集应用程序的特定部分的精确性能数据。在使用仪器的过程中，“enter”和“exit”探针被插入到应用程序的函数中。这些探针将报告返回给数据收集基础结构，并允许用户捕获函数执行所需的精确时间（或其他衡量标准）。</p>
		<p>分析应用程序</p>
		<p>分析应用程序的首选使用模式是首先开始采样，然后根据采样所产生的结果检测应用程序的特定方面。</p>
		<p>分析应用程序的过程相当简单。首先创建一个新的性能会话。在 Visual Studio 2005 Team System 中，可以使用 <b>Performance Session Wizard</b> 创建一个新的性能会话。</p>
		<p>Performance Session Wizard</p>
		<p>
				<b>Performance Session Wizard</b> 设置分析应用程序的必要环境。在 Visual Studio 2005 Team System 中，该向导为 EXE、DLL 和 ASP.NET 应用程序提供内置支持。</p>
		<p>可以使用该向导创建一个 <b>New Performance Session</b>，如图 6 所示。</p>
		<div style="WIDTH: 447px">
				<img height="448" alt="" src="http://www.microsoft.com/china/msdn/library/langtool/vsts/art/vsts-dev-fig06.gif" width="447" border="0" />
				<br />
				<p class="figureCaption">
						<b>图</b>
						<b> 6. </b>
						<b>启动性能向导</b>
				</p>
				<div class="figureRule">
				</div>
		</div>
		<p>
				<b>注</b> 要手动创建一个新的性能会话，请使用菜单上的 <b>New Performance Session</b> 命令。可以采用这种方法来手动分析其他类型的应用程序（例如，Windows 服务）。</p>
		<p>Performance Explorer</p>
		<p>性能会话作为运行会话向导或手动创建会话的结果而创建。<b>Performance Explorer</b> 可以将其直观地表示出来，如图 7 所示。</p>
		<div style="WIDTH: 182px">
				<img height="144" alt="" src="http://www.microsoft.com/china/msdn/library/langtool/vsts/art/vsts-dev-fig07.gif" width="182" border="0" />
				<br />
				<p class="figureCaption">
						<b>图</b>
						<b> 7. </b>
						<b>性能资源管理器</b>
				</p>
				<div class="figureRule">
				</div>
		</div>
		<p>
				<b>Performance Explorer</b> 给用户呈现了层次结构。该层次结构的根节点表示 <b>Performance Session</b>。这种节点的属性是用户在创建 <b>Performance Session</b> 时设置的属性。如果用户使用 <b>Performance Session Wizard</b> 创建会话，则这些属性体现了用户在使用向导的过程中所选择的值。如果用户手动创建 <b>Performance Session</b>，则这些属性包含了它们的默认值。</p>
		<p>根节点有两个子节点：<b>Targets</b> 节点和 <b>Reports</b> 节点。<b>Targets</b> 节点包含一个或多个目标，目标可以是 EXE、DLL 或 ASP.NET 应用程序。</p>
		<p>
				<b>Reports</b> 节点包含与某个特定 <b>Performance Session</b> 相关的所有报告。</p>
		<p>性能会话报告</p>
		<p>一旦应用程序执行完毕，系统就会将一个性能会话报告自动添加到 <b>Reports</b> 节点。可以通过下面的视图来查看这些报告：</p>
		<p>
				<em>Summary </em>
				<em>视图</em>
		</p>
		<p>Summary 视图为开发人员提供了其调查的起点。它显示应用程序执行过程中开销最高的函数，如图 8 所示。从该视图中的每个数据点，用户都可以定位到更详细的视图。</p>
		<div style="WIDTH: 470px">
				<img height="262" alt="" src="http://www.microsoft.com/china/msdn/library/langtool/vsts/art/vsts-dev-fig08.gif" width="470" border="0" />
				<br />
				<p class="figureCaption">
						<b>图</b>
						<b> 8. Summary </b>
						<b>视图</b>
				</p>
				<div class="figureRule">
				</div>
		</div>
		<p>
				<em>Functions </em>
				<em>视图</em>
		</p>
		<p>Functions 视图显示在应用程序的执行过程中调用的全部函数，这些函数存在于由该应用程序引用的所有模块之中，如图 9 所示。该视图显示的信息取决于所用的分析方法（采样与使用仪器）。</p>
		<div style="WIDTH: 450px">
				<img height="111" alt="" src="http://www.microsoft.com/china/msdn/library/langtool/vsts/art/vsts-dev-fig09.gif" width="450" border="0" />
				<br />
				<p class="figureCaption">图 9. Functions 视图</p>
				<div class="figureRule">
				</div>
		</div>
		<p>
				<em>Caller/Callee </em>
				<em>视图</em>
		</p>
		<p>Caller/Callee 视图提供在 Functions 视图中所列函数的详细信息，如图 10 所示。</p>
		<div style="WIDTH: 470px">
				<img height="60" alt="" src="http://www.microsoft.com/china/msdn/library/langtool/vsts/art/vsts-dev-fig10.gif" width="470" border="0" />
				<br />
				<p class="figureCaption">
						<b>图</b>
						<b> 10. Caller/Callee </b>
						<b>视图</b>
				</p>
				<div class="figureRule">
				</div>
		</div>
		<p>
				<em>Callstack </em>
				<em>视图</em>
		</p>
		<p>Callstack 视图使用户能够向下搜索特定的调用跟踪，并分析哪些跟踪对性能的影响最大。</p>
		<div style="WIDTH: 470px">
				<img height="103" alt="" src="http://www.microsoft.com/china/msdn/library/langtool/vsts/art/vsts-dev-fig11.gif" width="470" border="0" />
				<br />
				<p class="figureCaption">
						<b>图</b>
						<b> 11. Callstack </b>
						<b>视图</b>
				</p>
				<div class="figureRule">
				</div>
		</div>
		<p>
				<em>Type </em>
				<em>视图</em>
		</p>
		<p>Type 视图提供有关某个特定类型的实例数量和总字节数的信息，如图 12 所示。</p>
		<p>
				<em>命令行支持</em>
		</p>
		<p>可以通过命令行工具使用性能工具功能。这为用户提供了灵活性，用户既可以从命令行运行这些工具，又可以通过脚本来使用它们，以便自动执行任务。</p>
<img src ="http://www.cppblog.com/mzty/aggbug/8630.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mzty/" target="_blank">梦在天涯</a> 2006-06-16 12:16 <a href="http://www.cppblog.com/mzty/archive/2006/06/16/8630.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>单元测试小技巧</title><link>http://www.cppblog.com/mzty/archive/2006/06/16/8628.html</link><dc:creator>梦在天涯</dc:creator><author>梦在天涯</author><pubDate>Fri, 16 Jun 2006 03:53:00 GMT</pubDate><guid>http://www.cppblog.com/mzty/archive/2006/06/16/8628.html</guid><wfw:comment>http://www.cppblog.com/mzty/comments/8628.html</wfw:comment><comments>http://www.cppblog.com/mzty/archive/2006/06/16/8628.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/mzty/comments/commentRss/8628.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mzty/services/trackbacks/8628.html</trackback:ping><description><![CDATA[
		<h1>单元测试小技巧</h1>
		<h2 class="subtitle">编写可维护的节省时间和精力的单元测试</h2>
		<div class="date">发布日期： 2006-04-17<span class="datePipe"> | </span>更新日期： 2006-04-17</div>
		<div class="overview">
				<p>这篇文章描述了：</p>
				<table cellspacing="0" cellpadding="0" border="0">
						<tbody>
								<tr>
										<td class="listBullet" valign="top">•</td>
										<td class="listItem">
												<p>单元测试的信任</p>
										</td>
								</tr>
								<tr>
										<td class="listBullet" valign="top">•</td>
										<td class="listItem">
												<p>测试正确事件</p>
										</td>
								</tr>
								<tr>
										<td class="listBullet" valign="top">•</td>
										<td class="listItem">
												<p>创建维护测试</p>
										</td>
								</tr>
								<tr>
										<td class="listBullet" valign="top">•</td>
										<td class="listItem">
												<p>创建易读测试</p>
										</td>
								</tr>
						</tbody>
				</table>
				<p>这些天有很多的关于单元测试的和在不同的场景下为他们的应用程序编写单元测试(起始于, 我们2005年六月的 <i>MSDN</i><sup>®</sup><i>Magazine</i> 中有关测试你的数据层的文章, <em>Know Thy Code: Simplify Data Layer Unit Testing using Enterprise Services</em>)的讨论。这些意味着有很多的开发者自言自语（或者对于他们的团队）到：“哎，我们也需要开始编写测试了！”因此他们开始编写单元测试上面的单元测试直到他们达到了一个测试自己已经成为问题的程度。或许维护他们是一个太过困难，花费太长时间，或者他们并没有足够的易读性以便于理解，更或者他们本身存在bugs有一点是能够使得我们的开发人员可以下定决心去做的，那就是： 花费他们宝贵的时间以用来改进提高他们的测试或者忽略其中的问题, 从而有效的甩掉那些艰苦的工作。而这些困难的原因仅仅是因为那些不熟练的写入单元测试。.在这篇文章中，我将为大家带来在过去一年多时间里我在开发，提供咨询和培训开发者等方面有总结出来的一些最重要的练习和试验。这些小的技巧可以帮助您写出更有效的，可维护，和鲁棒性更好的单元测试。同时我希望这些总结和忠告可以帮助您避免一些由于错误引起的大量的时间的消耗。</p>
		</div>
		<center>
				<img title="" height="6" alt="*" src="http://img.microsoft.com/library/gallery/templates/MNP2.Common/images/3squares.gif" width="30" border="0" />
		</center>
		<div style="HEIGHT: 18px">
		</div>
		<h5 style="PADDING-TOP: 2px">本页内容</h5>
		<table style="MARGIN-TOP: 7px; MARGIN-BOTTOM: 12px" cellspacing="0" cellpadding="0" border="0">
				<tbody>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#EQB">
												<img height="9" alt="单元测试的信任" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#EQB">
												<font color="#002c99">单元测试的信任</font>
										</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#EDC">
												<font color="#002c99">
														<img height="9" alt="测试正确的事情" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
												</font>
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#EDC">
												<font color="#002c99">测试正确的事情</font>
										</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#EZD">
												<font color="#002c99">
														<img height="9" alt="创建维护测试" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
												</font>
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#EZD">
												<font color="#002c99">创建维护测试</font>
										</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#EQF">
												<font color="#002c99">
														<img height="9" alt="创建易读性测试" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
												</font>
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#EQF">
												<font color="#002c99">创建易读性测试</font>
										</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#ELG">
												<font color="#002c99">
														<img height="9" alt="在你的设置方法中避免部分相关的代码" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
												</font>
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#ELG">
												<font color="#002c99">在你的设置方法中避免部分相关的代码</font>
										</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#E2G">
												<font color="#002c99">
														<img height="9" alt="总结" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
												</font>
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#E2G">
												<font color="#002c99">总结</font>
										</a>
								</td>
						</tr>
				</tbody>
		</table>
		<a name="EQB">
		</a>
		<h2>单元测试的信任</h2>
		<p>在这个部分，我将略述出一些最通用的信任，这些信任来自于在使用大量单元测试获得的好处和解释为什么这些信任通常不是必须真实的。然后我们会帮助您在您的工程中拥有这些信任。</p>
		<p>
				<b>更加简单的跟踪</b>
				<b>Bug</b>  当然这并不是必须的，那么您怎么知道您的测试是正确的？ 是否存在在一些测试环节测试失败的情况？另外您又如何知道您的测试覆盖了系统中多少的代码量？是否测试到了程序中的错误，错误又在哪里等等的问题。</p>
		<p>当你在你的单元测试中发现了bug后又会发生什么事情哪？你会突然间得到很多与愿意错误的反馈，bug被发现，但是问题并不在你测试的代码中。你的测试的逻辑存在一个bug，因此测试失败了。这些bug也是您最难被检查出来的，因为您通常会去检查您的应用程序而不会去检测你的测试环节。在这部分中，我会展示给你如何确认大量的单元测试，事实上就是使得跟踪bug变得更加容易。</p>
		<p>
				<b>代码更加便于维护</b> 从最终点考虑，你可以倾向于认为这些信任并不是必须的，当然你是对的，让我们去说，代码中每个逻辑方法至少要有一个测试方法（当然，你可能拥有一个以上的方法）在一个好的测试覆盖的工程中，大概有百分之六十的代码是能够得到单元测试的，现在不得不考虑到测试也是要被维护的，如果针对一个复杂的逻辑方法你有20个测试，那么当你向这个方法添加一个参数时会发生什么事情哪？测试无法编译。当你修改了类的结构的时候同样会发生这样的事情。这时你突然发现为了能让你的应用程序继续工作你自己需要改变大量的测试。当然这会花费你大量的时间。</p>
		<p>为了使这个信任确认下来，你需要确认你的测试是便于维护的。保持DRY规则写入：不要重复你自己。我们将更加接近的来看这个问题。</p>
		<p>
				<b>代码更加容易被理解</b>  单元测试的好处通常并非是人们最初所期待的，在一个工程中考虑修改一些你之前从没有看过的代码(比方说,一个特殊的类或者方法).你将如何动手处理这些代码？你可能需要在项目中去浏览这些特定的类或者方法使用的代码，理所当然，单元测试就是这样例子的一个很好的场所。同时，当正确写入的时候，单元测试可以为工程提供一个API文件的容易读取的设置，使得文档的处理和代码的理解对于整个团队中的新老开发者一样的简单，便捷。然而，这些只能在测试是易读的和容易理解的情况下才能被确认，这个规则很多的单元测试开发者并不会遵循。我将详述这个信任，然后在这篇文章的易读测试的部分给你展现如何在去写易读的单元测试。</p>
		<div style="MARGIN-TOP: 3px; MARGIN-BOTTOM: 10px">
				<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#top">
						<img height="9" alt="返回页首" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_up.gif" width="7" border="0" />
				</a>
				<a class="topOfPage" href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#top">
						<font color="#002c99">返回页首</font>
				</a>
		</div>
		<a name="EDC">
		</a>
		<h2>测试正确的事情</h2>
		<p>新来者在Test Driven Development (TDD)中一个最通常的错误就是他们通常会搞混"Fail by testing something illogical."中的"Fail first"要求。例如，你可以用下面的规格开始这个方法：</p>
		<pre class="codeSample">' returns the sum of the two numbers
Function Sum(ByVal a As Integer, ByVal b As Integer) As Integer
</pre>
		<p>你可以向如下的方式写一个失败测试：</p>
		<pre class="codeSample">&lt;TestMethod()&gt; _
Public Sub Sum_AddsOneAndTwo()
    Dim result As Integer = Sum(1, 2)
    Assert.AreEqual(4, result, "bad sum");
End Sub
</pre>
		<p>初看上去这个处理像是一个写失败测试的好的方法，它完全错失了你写错误测试的初始点。</p>
		<p>一个失败测试验证了在代码中存在一些错误，当你的测试完成后这个测试应该是通过的，现在的例子中，无论如何，测试都将会失败，即使是代码完成，因为测试逻辑上不是正确的。如果希望测试通过测需要测试自身进行修改――而不是程序的代码的改变（当程序代码改变的时候，是test-first规划的意图）简短来说，这个测试不会反映出程序代码完成后的最终的结果，因此这个不是一个好的测试。</p>
		<p>TDD中一个好的测试要求你去修改代码，从而使它能够按照想要的方式工作，这一点要胜于强迫你去反映现在的真实情况或者一个非逻辑要求的渴望的结果。例如，当1＋1返回0时就意味着测试失败。这个简单的例子和这种情况是相似的，在练习中，如果现在的需求是在工作的，测试应该可以反映你所期待的结果，然后你可以调整现在代码的情况去通过这个测试。</p>
		<p>作为一个规则，一个已经调通的测试不应该被移除掉，因为这个测试在维护工作中可以用于恢复测试。他们在你改变代码时用来确定你没有损害到现在已经工作的函数。这就是为什么你不应该修改那些已经通过的测试，除非是一些很小的修改，例如增加它的可读性（换句话说，分解测试）</p>
		<p>
				<b>当一个测试非正常失败</b>  有时你可能遇到失败的测试，而这时你对代码的改变是完全合理的。这通常是因为你遇到了冲突的需求。一般来说，可能是一个新的需求（一个改变的特性）与一个旧的可能已经不再有效的需求发生了冲突。这有两种可能：</p>
		<table class="numberedList" cellspacing="0" cellpadding="0" border="0">
				<tbody>
						<tr valign="top">
								<td class="listNumber" nowrap="" align="right">
										<p>1.</p>
								</td>
								<td>
										<p>在旧的需求或者无效或者在别处测试的情况下删除被验证本质上不再有效的失败的测试</p>
								</td>
						</tr>
						<tr valign="top">
								<td class="listNumber" nowrap="" align="right">
										<p>2.</p>
								</td>
								<td>
										<p>改变旧的测试使你可以测试新的要求（本质上使用新的测试），然后在新的设置下（测试的逻辑状态相同，但是初始功能函数可能有所不同）测试旧的需求。</p>
								</td>
						</tr>
				</tbody>
		</table>
		<p>而有时候一个测试在使用不完整的技术去完成任务的时候也是有效的，例如，你有一个成员类带有一个FOO方法，它表现为某几种行为，它已经经由Test在X年前测试完成，然后现在一些其他的需求加了进来，方法的逻辑增强了，从而可以去处理一些类似于在获取数据时丢失一些参数的异常处理。但这时，突然Test X失败了，虽然在测试这个函数的时候只是使用了同样的类。这个测试的失败是因为在调用方法之前丢失了一些初始处理步骤。</p>
		<p>这并不意味着你需要移除Test X，你将丢失对于一些重要功能的测试，这时你应该去关心那些初始化时的问题，而不是改变类的创建以用来适应你新的意图。</p>
		<p>当然如果你那里有200个测试都是因为旧的结构导致的失败，你就应该找到这个问题来维护你的测试。这就是为什么你应该总是移除你测试中的副本尤其是在生产代码中。</p>
		<p>
				<b>测试覆盖和测试</b>
				<b>Angles</b>  你如何知道是否你的新代码是一个好的覆盖？当试图移动一个链接或者一个约束检查后，如果所有的测试依然通过，那么你就没有足够的代码复制然后你可能需要添加其他的测试单元。</p>
		<p>确认你添加正确测试的最好方法就是测试一些最平常的行和检查直到用非常的手段使它出错。这个也许很难，但是如果你不能考虑出一个让代码出错的方法，你就可能没有好的理由在最初的地方写下这行代码。</p>
		<p>你不知道什么时候下一个开发者会试图运行你的程序，他可能优化或者错误的删除一些包含本质的行。如果你没有一个测试，它就会失败，其他的开发者可能不会知道他们犯了错误。</p>
		<p>你也可能试图利用一些常量去替代一些已经通过了的测试中调用的各种各样的参数，例如，看下面的方法：</p>
		<pre class="codeSample">Public Function Sum(ByVal x As Integer, ByVal y As Integer, _
        ByVal allowNegatives As Boolean) As Integer
    If Not allowNegatives Then Throw New Exception()
    Return x + y
End Function
</pre>
		<p>你可以打乱代码去测试覆盖，这有一些关于如何测试的变化：</p>
		<pre class="codeSample">' Try this...
If Not True Then ' replace flag with const
    If x &lt; 0 OrElse y &lt; 0 Then Throw New Exception()
End If

' Or this...
If Not allowNegatives Then
    ' replace check with const
    If False OrElse y &lt; 0 Then Throw New Exception()
End If
</pre>
		<p>如果所有的测试依然通过，那么你缺少了一个测试，另外一个红色标志是在你为多种相同值测试的检查。如下：</p>
		<pre class="codeSample">Assert.AreEqual(3, retval)
</pre>
		<p>一些方法的关系只看一次（在一个测试中）意味着你可以安全的返回3作为一个值，然后所有的针对这个方法的测试都将通过，这个当然意味着你丢失了一个测试。如果你在单元测试中检查一下代码，它就很容易被检查出来。</p>
		<p>确保你的测试写的越简单越好，一个单元测试一般不包括一个if switch或者其他任何的逻辑声明。如果你发现你自己在你的测试中写了一些类似于逻辑声明的东西，这是一个好的机会来测试一个以上的事件，在做这样的操作的时候，你会使得你的测试比读和维护更加的困难，在生产代码中同样如此。保持你的测试简单，你在生产代码中发现bug要胜于在你的单元测试中。</p>
		<p>
				<b>使测试易于运行</b>  如果你的测试并不容易运行，那么人们不会信任它。你的应用程序最有可能有下面两种类型的测试：</p>
		<table cellspacing="0" cellpadding="0" border="0">
				<tbody>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>测试在没有任何配置的情况下平稳的运行（这种类型的测试，我们可以在任何的机器上，在代码的最终版上或者在源控制上测试，并且做到没有任何故障的测试）</p>
								</td>
						</tr>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>在运行前需要一些配置.</p>
								</td>
						</tr>
				</tbody>
		</table>
		<p>第一种类型是你应该模仿的，第二种类型是你通常做的，尤其你如果你是一个新的单元测试。如果你发现你自己测试时有很多的特殊的需求，现在是正常的，但是重要的一点就是你要隔离出两个组让他们能够单独的去做测试。</p>
		<p>我们的想法是任意一个开发者都应该有能力修改和运行一些不需要设置特殊的配置的测试进行测试。如果这有一些测试需要在运行前有特殊的关注，开发者应该知道他们，然后他可以花一些时间学习这些测试的方法。因为很多的开发者比较的懒（当然，不是你），你可以设想，他们不会去做那些特殊的设置，相反，他们会让测试失败因为他们有更好的事情去做。</p>
		<p>当用户让测试失败时，他们开始考虑他们不能够信任这些测试了。很难说是否测试可以在一个中找到一个正式的bug或者只是一个错误的定位。开发者可能不明白为什么测试者会在一开始就执行失败。一旦他们不再信任你的测试，开发者将会停止运行它们，那么bug就会驻留在程序中，之后一连串的麻烦就来了。。。</p>
		<p>
				<b>为了避免这件事情，确认你总是有一个组准备好了去测试，测试程序则是可以安全运行，可信任的。把那些属于配置挑战组的测试放到不同的文件夹，树或者工程中，同时标记特殊的说明指明他们在运行前需要做什么。完成这些后，开发者可以不投入时间去配置就开始测试工作。当他们有时间和需要时，他们也可以配置，运行更多的测试环节。</b>
		</p>
		<div style="MARGIN-TOP: 3px; MARGIN-BOTTOM: 10px">
				<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#top">
						<img height="9" alt="返回页首" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_up.gif" width="7" border="0" />
				</a>
				<a class="topOfPage" href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#top">
						<font color="#002c99">返回页首</font>
				</a>
		</div>
		<a name="EZD">
		</a>
		<h2>创建维护测试</h2>
		<p>我们应该试着避免测试私有或保护成员。这篇文章也许能够帮助一些人解决一部分问题，但是我很坚决相信百分之九十九的时间，你可以全面的测试一个类，通过编写一些与它的独立公共接口相反的单元测试。测试私有成员可以使你的测试更加脆弱，如果这个需要被测试的类的一些内在方面略有改动的话。你应该使用通过调用一些代码里别处的公共功能这一方法去测试私有功能。当你依然能够确定全部功能并没有改变的时候，仅仅测试公共成员会导致测试遭受常量代码的因式分解以及内部的执行情况改变。</p>
		<p>
				<b>在可能的时候，应该重新使用你的创造物，处理过程，和声明代码。不要在一个单元测试中直接的创建类的实例。如果你在任何并不包含在此单元测试框架中的类前面看到这个单词“</b>new”，你应该考虑一下将你创造的代码放在一个特殊的整体方法之中，它可以为你创建一个对象实例。你可以到时再重新使用这个方法来获得你的测试在其他测试之中的最新实例。这样可以帮助你来保持这个测试维护所需的时间，然后在测试进行的时候，从对代码无法预料的改变之中保护你的测试。作为一个例子，<a href="http://msdn.microsoft.com/msdnmag/issues/06/01/UnitTesting/default.aspx?fig=true#fig1" target="_blank"><b><font color="#002c99">Figure 1</font></b></a>展示了一对简单的测试，它使用了一个Calc类。</p>
		<p>假设你有20，或者你甚至有100，与Calc类做相反测试，所有这些看起来令人吃惊的相似。现在一个计划的改变迫使你不得不删除默认的Calc构造器并且使用一个含有一些参数的不同的构造器。马上，你所有的测试就被暂停了。你可能可以很轻易的发现问题的关键并修复它，但你也可能做不到。最主要的问题是你将会浪费很多宝贵时间在修理你的测试上面。如果你在你的测试类之中使用一个整体的方法去创建Calc 实例，就像<a href="http://msdn.microsoft.com/msdnmag/issues/06/01/UnitTesting/default.aspx?fig=true#fig2" target="_blank"><b><font color="#002c99">Figure 2</font></b></a>所显示的那样，这些就并不是个问题。</p>
		<p>我已经对测试做了一些改变已使它们能够具有更多可维护性。首先，我将新创建的代码迁移至可以再度使用的整体方法之中。这就意味着我只需仅仅改变一个简单的方法以使得在这个测试类中的所有测试在一个新的构造器中的能够正常的工作。另外一个为创造问题而设的简单解决方法是把创作物迁移到测试类的&lt;TestInitialize()&gt;方法之中。不幸运的是，这个能够很好的工作仅仅在你重新使用一个对象并在一些测试中把它当作一个局部类变量。如果你仅仅为一些测试使用它（部分相关成员），你倒不如在测试中将它们实例化，并且使它们更具易读性。</p>
		<p>顺便一提的是，请注意，我已经将方法命名为Factory_CreateDefaultCalc 。我很喜欢将我测试中的任何帮助方法用特殊的前缀来命名，这样我就能很轻易的掌握它是做什么用的。这样对易读性也是非常有帮助的。</p>
		<p>我的第二个改变是重新使用测试中的声明代码，并将这段代码迁移到一个确认方法之中。所谓确认方法是你测试中的一个可再度使用的方法， 这个方法包含了一个声明语句但是它可以接受不同输入和在输入的基础上进行校验。当你在不同输入或者不同的初始状态下一次又一次的声明同一事物时，你可以使用确认方法。这一方法的优点是既使在一个不同的方法里面声明，如果这个声明失败了你将可以继续保有一个异常处理，而且原始调用测试将会显示在测试失败输出窗口之中。</p>
		<p>我也在Calc 中传递实例而不是使用一个局部变量，因此我知道我经常传递一个实例，而且这个实例是调用测试将其初始化的。当你想要改变对象状态时你可能想要做同样的事情，举个例子来说，当在测试下或者在将会传递给测试的对象下配置特殊对象时，可以使用特殊的Configure_XX方法。这些方法应该能够解释他们配置一个对象将会用来做什么用。<a href="http://msdn.microsoft.com/msdnmag/issues/06/01/UnitTesting/default.aspx?fig=true#fig3" target="_blank"><b><font color="#002c99">Figure 3</font></b></a>之中的代码就是以上方法的实例。</p>
		<p>这个测试拥有很多设置代码可以用来处理向注册管理器对象中添加初始状态，它是这个测试类之中的成员。在此的确也有一些重复。<a href="http://msdn.microsoft.com/msdnmag/issues/06/01/UnitTesting/default.aspx?fig=true#fig4" target="_blank"><b><font color="#002c99">Figure 4</font></b></a>显示了在初始代码之外这些事例在因式分解之后将会如何变化。</p>
		<p>修订测试具有非常高的可读性和稳定性。仅仅需要注意的是不要那么的refactor你的测试，他们可能会以一个单一的，不可读的代码行作为结束。应该注意的是我在这里可能依然使用一个Verify_XX 方法，但是这并不是我真正要在这里加以说明的。</p>
		<p>
				<b>消除测试之间的依赖关系</b>
		</p>
		<p>一个测试应该能够自我独立。它不应该与其他测试相关联，也不应该依赖任何具有特殊运行顺序的测试，它应该能够获得你所写的所有测试，可以随意运行所有测试或者只运行其中的一部分，并且是以任何顺序，而且要能够确保它们无论怎样都应该正确的运行。如果你不能够执行这个规则，你将会只在某种特殊的情况下按照预期的表现来运行的状况下结束你的测试。这样子的话，当你在最终期限下与此同时你还想确定你没有向系统之中引进新的问题的时候，当然就会出现问题。你可能很困惑而且考虑着是不是你的代码出现问题，这时，在事实上，问题其实仅仅是你的测试运行顺序所引起的。因此，你可能开始错过了一些在测试中失败的结果而且使它越写越少。这将会是个长期的过程。</p>
		<p>如果你从一个测试调出至另一个测试之中，你应该在它们之间创建一个从属关系。你本质上说是在一个测试中测试两个事物（我将会在下一章中解释为什么这会成为一个问题）。就另一方面来说，如果你有测试B，它与测试A 所产生的状态是不相关的，那么你会陷入“顺序”陷阱之中。如果你或者其他人想要改变测试A，测试B将会暂停而且你不知它暂停的原因。对这些故障进行故障处理会浪费很多时间。</p>
		<p>使用&lt;TestInitialize()&gt; 和&lt;TestCleanup()&gt;方法是本质上能够获得更好的测试隔离。确定你的测试数据时刻是最新的，而且测试下对象的也具有新的实例，而且所有的状态可以提前预知，而且无论你的测试在任何地方或者任何时间被运行，运行的情况都是相同的。</p>
		<p>
				<b>在一个单独单元测试中避免多重声明</b>
		</p>
		<p>我们将声明故障看作一个程序弊病的象征且声明被当作软件体的指示点或者“血液检查”。你可以找到越多的症状，程序弊病就越可以轻松的被诊断和排除掉。如果你在一个测试中定义了多重声明，只有第一个故障声明将会以抛出异常的方式显示出来。请参考下面插图之中的测试代码：</p>
		<pre class="codeSample">&lt;TestMethod()&gt; _
Public Sub Sum_AnyParamBiggerThan1000IsNotSummed()
    Assert.AreEqual(3, Sum(1001, 1, 2)
    Assert.AreEqual(3, Sum(1, 1001, 2) ' Assert fails
    Assert.AreEqual(3, Sum(1, 2, 1001) ' This line never executes
End Sub
</pre>
		<p>
				<b>你可能没有发现以上代码之中其他可能的征兆。在一个故障之后，并发的声明不会被执行。这些不能生效的声明可能提供了有价值的数据（或者征兆）可能能够帮助你很快的集中的焦点而且发现潜在的问题。因此在一个独立的测试中运行多重声明增加了具有很少价值复杂性。另外，声明应该被独立的运行，我们应该设置自我独立的单元测试以使得你具有能够很好的发现错误的机会。</b>
		</p>
		<div style="MARGIN-TOP: 3px; MARGIN-BOTTOM: 10px">
				<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#top">
						<img height="9" alt="返回页首" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_up.gif" width="7" border="0" />
				</a>
				<a class="topOfPage" href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#top">
						<font color="#002c99">返回页首</font>
				</a>
		</div>
		<a name="EQF">
		</a>
		<h2>创建易读性测试</h2>
		<p>如果你以前写过单元测试，你是否在单元测试上写了一个好的声明行？可许不是这样的，大多数开发者并不厌烦去写一个好的声明因为他们更加关心去写测试。</p>
		<p>假设你是团队中的一个新的开发者，你试图读一个单元测试。连接这个：</p>
		<pre class="codeSample">&lt;TestMethod()&gt; _
Public Sub TestCalcParseNegative()
    Dim c As New Calc
    Assert.AreEqual(1000, c.Parse("-1, -1000")
End Sub
</pre>
		<p>作为一个简单的练习，如果你理解了上例中Calc分列方法的用法，你很可能可以进行很好的推测，但是他可以简单的作为人员数量的用例使得输出结果为1000:</p>
		<table cellspacing="0" cellpadding="0" border="0">
				<tbody>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>在组中返回最大的负数作为一个正数。</p>
								</td>
						</tr>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>如果数字是负数且返回值为剩下几个数的总和作为一个正数，那么忽略第一个数字。</p>
								</td>
						</tr>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>返回相互作乘积运算而得的数字。</p>
								</td>
						</tr>
				</tbody>
		</table>
		<p>现在请参考下面在单元测试之中的小改动：</p>
		<pre class="codeSample">&lt;TestMethod()&gt; _
Public Sub Parse_NegativeFirstNum_ReturnsSumOfTheRestAsPositive()
    Dim c As New Calc
    Dim parsedSumResult As Integer = c.Parse("-1", "-1000")
    Const SUM_WITH_IGNORED_FIRST_NUM As Integer = 1000
    Assert.AreEqual(SUM_WITH_IGNORED_FIRST_NUM, parsedSumResult)
End Sub
</pre>
		<p>
				<b>这个是不是比较容易理解呢？当声明消息消失之后，表达意图最合适的地方就是测试的名字。</b> 如果你广泛的使用了它，你将会发现你不再需要读测试代码就能明白代码测试的目的所在。事实上，你经常根本不需要写任何注释，因为代码，特别是那些带着实例的，他们自己是证明自己的。</p>
		<p>名字包含了三部分内容： 测试下方法的名字（解析），测试下的状态或者规则（带着第一个负数传递一个字符串），以及预期的输出或者运行情况（剩余数字的总和以一个正数的形式返回）。需要注意的是我从名称中将Test以及Calc这两个词删除。我已经知道这是一个属性的测试因此在此没有重复此信息的必要。我也知道这是一个在Calc类中的测试因为测试类经常是写给一个特殊类的（这个类也许已经被命名为CalcTests）。</p>
		<p>名字也许会很长，但是又有谁在乎呢？它读起来更像是一个标准英语的句子而且它使得一个新来的开发者更容易明白测试的内容。更是这样，当这个测试发生故障时，我们甚至不需要调试代码就可以知道问题究竟出在哪里。</p>
		<p>需要注意的是，我已经在前面分别实际演示了通过在不同行中创建一个结果变量的方法从声明操作中进行分解操作。这样做至少有两个理由。第一个理由是，你可以为一个变量分配一个可读性强的名字，它可以包含结果，这样可以使你的声明行非常易于理解以及易于读。第二点是，测试下与对象相反的invocation 可能非常的长，它可能会使你的声明行延伸出屏幕的边缘之外，这样导致测试者向右滚屏。就我个人而言，我认为这个是非常恼人的。</p>
		<p>我在我的测试中使用了很多常量以确保我的声明读起来像一本书。在先前的例子之中，你可以读到声明中说：“确保分解总数是与忽略第一个数后所得总和是相等的。” 为你的变量取一个很好的名字能够在某些程度上弥补对于测试的命名不足。</p>
		<p>
				<b>当然，有时一个声明</b> 消息是在一个单元测试中传递intent的最好的方法。 一个好的声明消息始终能够解释什么因该会发生或者什么发生了而且为什么会出错。举个例子来说，“分列应该忽略掉第一个数字如果这个数字是个负数的话”，“分列不能够忽略掉第一个负数”，还有“X调用对象Y标记错误”这些都是有用的声明消息，它们很清晰的描述了结果的情况。</p>
		<div style="MARGIN-TOP: 3px; MARGIN-BOTTOM: 10px">
				<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#top">
						<img height="9" alt="返回页首" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_up.gif" width="7" border="0" />
				</a>
				<a class="topOfPage" href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#top">
						<font color="#002c99">返回页首</font>
				</a>
		</div>
		<a name="ELG">
		</a>
		<h2>在你的设置方法中避免部分相关的代码</h2>
		<p>一个&lt;TestInitialize()&gt; 方法是样例成员变量在测试中使用的一个好地方。你所有的测试，只有在一部分的测试中避免变量。他们可以为测试设置本地变量。如果你创建了部分相关的实例作为类的成员，用来在测试中简单的避免创建的副本，你应该使用在文章前面解释的工厂方法，使用部分相关变量使得你的代码和设置方法缺少易读性。一旦变量在一个或者每个测试中使用，那么他应该是&lt;TestInitialize()&gt; 方法的一个成员和变量。</p>
		<p>
				<a href="http://msdn.microsoft.com/msdnmag/issues/06/01/UnitTesting/default.aspx?fig=true#fig5" target="_blank">
						<b>
								<font color="#002c99">Figure 5</font>
						</b>
				</a> 展现了一个拥有两个成员变量的类的测试。但是他们中的一个(cxNum)只被部分使用。<a href="http://msdn.microsoft.com/msdnmag/issues/06/01/UnitTesting/default.aspx?fig=true#fig6" target="_blank"><b><font color="#002c99">Figure 6</font></b></a> 展现了如何在测试中替换代码从而使它更加易读的方法。</p>
		<div style="MARGIN-TOP: 3px; MARGIN-BOTTOM: 10px">
				<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#top">
						<img height="9" alt="返回页首" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_up.gif" width="7" border="0" />
				</a>
				<a class="topOfPage" href="http://www.microsoft.com/china/msdn/library/langtool/vsts/UnitTestingTips.mspx#top">
						<font color="#002c99">返回页首</font>
				</a>
		</div>
		<a name="E2G">
		</a>
		<h2>总结</h2>
		<p>就像你所看到的，写单元测试并不是一个微不足道的任务，如果步骤正确，单元测试可以为开发者的生产力和代码的质量带来令人惊讶的提高，他可以帮助你去创建的应用程序含有更少的错误，同时也可以便于其他的开发者去洞察你的代码，但是他也需要在之前承担一个义务，确认遵循一些简单的规则。当方法并不是很好时，单元测试则可能达到一个相反的结果，从而浪费您的时间，并且使测试过程更加复杂。</p>
		<p>
				<b>Roy Osherove</b> Agile组的负责人, 这个顾问公司致力于agile software development 和 .NET architecture的研究工作. Roy同时维护了一个blog在<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/www.iserializable.com" target="_blank"><font color="#002c99"> www.iserializable.com</font></a>上有相关的信息. 你可以通过Email联系他： <em>Roy@TeamAgile.com</em>.</p>
		<p>
				<a href="http://msdn.microsoft.com/msdnmag/issues/06/01/UnitTesting/default.aspx" target="_blank">
						<font color="#002c99">转到英文页面</font>
				</a>
		</p>
<img src ="http://www.cppblog.com/mzty/aggbug/8628.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mzty/" target="_blank">梦在天涯</a> 2006-06-16 11:53 <a href="http://www.cppblog.com/mzty/archive/2006/06/16/8628.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用 Visual Studio 2005 Team System 进行单元测试</title><link>http://www.cppblog.com/mzty/archive/2006/06/16/8626.html</link><dc:creator>梦在天涯</dc:creator><author>梦在天涯</author><pubDate>Fri, 16 Jun 2006 03:50:00 GMT</pubDate><guid>http://www.cppblog.com/mzty/archive/2006/06/16/8626.html</guid><wfw:comment>http://www.cppblog.com/mzty/comments/8626.html</wfw:comment><comments>http://www.cppblog.com/mzty/archive/2006/06/16/8626.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/mzty/comments/commentRss/8626.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mzty/services/trackbacks/8626.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码						发布日期： 11/10/2005 | 更新日期： 11/10/2005						Scott DockendorfTelligent Systems, Inc.				适用于：Microsoft Visual Studio ...&nbsp;&nbsp;<a href='http://www.cppblog.com/mzty/archive/2006/06/16/8626.html'>阅读全文</a><img src ="http://www.cppblog.com/mzty/aggbug/8626.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mzty/" target="_blank">梦在天涯</a> 2006-06-16 11:50 <a href="http://www.cppblog.com/mzty/archive/2006/06/16/8626.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用 Visual Studio Team Test 进行单元测试</title><link>http://www.cppblog.com/mzty/archive/2006/06/16/8624.html</link><dc:creator>梦在天涯</dc:creator><author>梦在天涯</author><pubDate>Fri, 16 Jun 2006 03:46:00 GMT</pubDate><guid>http://www.cppblog.com/mzty/archive/2006/06/16/8624.html</guid><wfw:comment>http://www.cppblog.com/mzty/comments/8624.html</wfw:comment><comments>http://www.cppblog.com/mzty/archive/2006/06/16/8624.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/mzty/comments/commentRss/8624.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mzty/services/trackbacks/8624.html</trackback:ping><description><![CDATA[
		<h1>演练：使用 Visual Studio Team Test 进行单元测试</h1>
		<h2 class="subtitle">
		</h2>
		<div class="date">发布日期： 5/24/2005<span class="datePipe"> | </span>更新日期： 5/24/2005</div>
		<div class="overview">
				<p>Mark Michaelis<br />Itron Corporation</p>
				<p>
						<b>摘要：</b>本演练通过测试驱动开发 (TDD) 和先测试-后编码 (test-then-code) 的方法学习单元测试。</p>
		</div>
		<center>
				<img title="" height="6" alt="*" src="http://img.microsoft.com/library/gallery/templates/MNP2.Common/images/3squares.gif" width="30" border="0" />
		</center>
		<div style="HEIGHT: 18px">
		</div>
		<h5 style="PADDING-TOP: 2px">本页内容</h5>
		<table style="MARGIN-TOP: 7px; MARGIN-BOTTOM: 12px" cellspacing="0" cellpadding="0" border="0">
				<tbody>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#E4B">
												<img height="9" alt="简介" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#E4B">简介</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EGD">
												<img height="9" alt="开始" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EGD">开始</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EAF">
												<img height="9" alt="创建测试" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EAF">创建测试</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EVCAC">
												<img height="9" alt="运行测试" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EVCAC">运行测试</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EHGAC">
												<img height="9" alt="检查异常" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EHGAC">检查异常</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EXIAC">
												<img height="9" alt="从数据库中加载测试数据" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EXIAC">从数据库中加载测试数据</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EJJAC">
												<img height="9" alt="测试视图 (Test View) 窗口" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EJJAC">测试视图 (Test View) 窗口</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EMLAC">
												<img height="9" alt="增加一个测试数据库" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EMLAC">增加一个测试数据库</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#E2OAC">
												<img height="9" alt="将数据与测试关联" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#E2OAC">将数据与测试关联</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EGDAE">
												<img height="9" alt="实现和重构目标方法" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EGDAE">实现和重构目标方法</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EDEAE">
												<img height="9" alt="代码覆盖" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EDEAE">代码覆盖</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EXEAE">
												<img height="9" alt="初始化和清除测试" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EXEAE">初始化和清除测试</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EHHAE">
												<img height="9" alt="最佳实践" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EHHAE">最佳实践</a>
								</td>
						</tr>
						<tr valign="top">
								<td>
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EAIAE">
												<img height="9" alt="小结" hspace="4" src="http://www.microsoft.com/library/gallery/templates/MNP2.Common/images/arrow_px_down.gif" width="7" vspace="2" border="0" />
										</a>
								</td>
								<td class="onThisPage">
										<a href="http://www.microsoft.com/china/msdn/library/langtool/vsts/vstsunittesting.mspx#EAIAE">小结</a>
								</td>
						</tr>
				</tbody>
		</table>
		<a name="E4B">
		</a>
		<h2>简介</h2>
		<p>最新发布的 Visual Studio Test System (VSTS) 包含了一套用于 Visual Studio Team Test 的完整功能。Team Test 是 Visual Studio 集成的单元测试框架，它支持：</p>
		<table cellspacing="0" cellpadding="0" border="0">
				<tbody>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>测试方法存根 (stub) 的代码生成。</p>
								</td>
						</tr>
						<tr>
								<td class="listBullet" valign="top">•</td>
								<td class="listItem">
										<p>在 IDE 中运行测试。</p>
								</td>
						</tr>
						<tr>
								<td class="listBullet" valign="top">•</td>
		