﻿<?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>BlogJava-热爱Java，学习Java-随笔分类-软件工程</title><link>http://www.blogjava.net/wenhl5656/category/40337.html</link><description>长夜漫漫，无以为伴，唯有java</description><language>zh-cn</language><lastBuildDate>Wed, 17 Jun 2009 14:16:05 GMT</lastBuildDate><pubDate>Wed, 17 Jun 2009 14:16:05 GMT</pubDate><ttl>60</ttl><item><title>单元测试(转摘)</title><link>http://www.blogjava.net/wenhl5656/archive/2009/06/17/282924.html</link><dc:creator>爱吃鱼头</dc:creator><author>爱吃鱼头</author><pubDate>Wed, 17 Jun 2009 14:08:00 GMT</pubDate><guid>http://www.blogjava.net/wenhl5656/archive/2009/06/17/282924.html</guid><wfw:comment>http://www.blogjava.net/wenhl5656/comments/282924.html</wfw:comment><comments>http://www.blogjava.net/wenhl5656/archive/2009/06/17/282924.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wenhl5656/comments/commentRss/282924.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wenhl5656/services/trackbacks/282924.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 单元测试（模块测试）是开发者编写的一小段代码，用于检验被测代码的一个很小的、很明确的功能是否正确。通常而言，一个单元测试是用于判断某个特定条件（或者场景）下某个特定函数的行为。例如，你可能把一个很大的值放入一个有序list 中去，然后确认该值出现在list 的尾部。或者，你可能会从字符串中删除匹配某种模式的字符，然后确认字符串确实不再包含这些字符了。<br><br>    单元测试是由程序员自己来完成，最终受益的也是程序员自己。可以这么说，程序员有责任编写功能代码，同时也就有责任为自己的代码编写单元测试。执行单元测试，就是为了证明这段代码的行为和我们期望的一致。<br><br>    要进行充分的单元测试，应专门编写测试代码，并与产品代码隔离。个人认为，比较简单的办法是为产品工程建立对应的测试工程，为每个类建立对应的测试类，为每个函数（很简单的除外）建立测试函数。&nbsp;&nbsp;<a href='http://www.blogjava.net/wenhl5656/archive/2009/06/17/282924.html'>阅读全文</a><img src ="http://www.blogjava.net/wenhl5656/aggbug/282924.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wenhl5656/" target="_blank">爱吃鱼头</a> 2009-06-17 22:08 <a href="http://www.blogjava.net/wenhl5656/archive/2009/06/17/282924.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>测试用例设计</title><link>http://www.blogjava.net/wenhl5656/archive/2009/06/17/282918.html</link><dc:creator>爱吃鱼头</dc:creator><author>爱吃鱼头</author><pubDate>Wed, 17 Jun 2009 13:48:00 GMT</pubDate><guid>http://www.blogjava.net/wenhl5656/archive/2009/06/17/282918.html</guid><wfw:comment>http://www.blogjava.net/wenhl5656/comments/282918.html</wfw:comment><comments>http://www.blogjava.net/wenhl5656/archive/2009/06/17/282918.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wenhl5656/comments/commentRss/282918.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wenhl5656/services/trackbacks/282918.html</trackback:ping><description><![CDATA[<span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="宋体 ">&nbsp;&nbsp;&nbsp; 测试用例就是测试<span href="tag.php?name=%CA%FD%BE%DD" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">数据</span>及与之相关的<span href="tag.php?name=%B9%A6%C4%DC" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">功能</span>的一个特定集合，它是为验证被测试<span href="tag.php?name=%B3%CC%D0%F2" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">程序</span>（为测试程序路径或验证是否符合特定功能等方面的<span href="tag.php?name=%D0%E8%C7%F3" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">需求</span>）而<span href="tag.php?name=%C9%E8%BC%C6" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">设计</span>的。在<span href="tag.php?name=%B5%A5%D4%AA" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">单元</span><strong style="text-align: left; font-style: normal; line-height: normal; font-weight: bold;"><a href="http://www.ltesting.net/html/97/category-catid-97.html" target="_blank" style="text-decoration: none; color: #244d8f; line-height: normal;">测试过程</a></strong>中，测试用例的设计应与复审工作相结合，根据设计的测试用例选取不同的测试数据，将增加发现各类错误的可能性；另外，根据<span href="tag.php?name=%CF%EE%C4%BF" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">项目</span>的具体情况确定测试用例项。如：测试用例编号、用例输入、用例预期输出、被测单元的版本号、实际输出等。单元测试用例的设计既可以使用<span href="tag.php?name=%B0%D7%BA%D0" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">白盒</span>测试也可以使用黑盒测试，但以白盒测试为主，黑盒测试侧重于功能，白盒测试侧重于逻辑。</font></font><br style="line-height: normal;" />
<font style="line-height: normal;" color="black"><font style="line-height: normal;" face="宋体 "><br style="line-height: normal;" />
</font></font><font style="line-height: normal;" face="宋体 "><font style="line-height: normal;" color="#000000">&nbsp;&nbsp;&nbsp; 白盒测试进入的前提条件是测试人员已经对被测试对象有了一定的了解，基本上明确了被测试<span href="tag.php?name=%C8%ED%BC%FE" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">软件</span>的逻辑结构。具体过程就是针对程序逻辑结构设计和加载测试用例，驱动程序执行，<span href="tag.php?name=%BC%EC%B2%E9" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">检查</span>在不同点程序的状态，以确定实际的状态是否与预期的状态一致。</font></font><br style="line-height: normal;" />
<font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman "><br style="line-height: normal;" />
</font><font style="line-height: normal;" face="宋体 ">&nbsp;&nbsp;&nbsp; 一般来说，为了度量测试的完整性，测试工作中通常要求达到一定的覆盖率要求。因为通过覆盖率的统计可以知道测试是否充分，对软件的哪个部分所做的测试不够，指导我们如何设计增加覆盖率的测试用例。这样就能够提高测试<strong style="text-align: left; font-style: normal; line-height: normal; font-weight: bold;"><a href="http://www.ltesting.net/html/5/category-catid-5.html" target="_blank" style="text-decoration: none; color: #244d8f; line-height: normal;"><span href="tag.php?name=%D6%CA%C1%BF" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">质量</span></a></strong>，尽量避免设计无效的用例。</font></font><br style="line-height: normal;" />
<font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman "><br style="line-height: normal;" />
</font><font style="line-height: normal;" face="宋体 ">&nbsp;&nbsp;&nbsp; 在白盒测试的范畴内通常使用下面几种测试覆盖率来度量测试，如：语句覆盖、判定覆盖、条件覆盖、判定条件覆盖、条件组合覆盖、路径覆盖等。白盒测试最低应该达到的覆盖率目标是：语句覆盖率达到</font><font style="line-height: normal;" face="Times New Roman ">100</font><font style="line-height: normal;" face="宋体 ">％，分支覆盖率达到</font><font style="line-height: normal;" face="Times New Roman ">100</font><font style="line-height: normal;" face="宋体 ">％，覆盖程序中主要的路径，主要路径是指完成模块正常功能的路径和功能等其他异常处理执行的路径。</font></font><br style="line-height: normal;" />
<br style="line-height: normal;" />
<font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman ">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font style="line-height: normal;" face="宋体 ">测试人员在实际工作中要根据不同的覆盖要求来设计面向<span href="tag.php?name=%B4%FA%C2%EB" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">代码</span>的单元测试用例，<span href="tag.php?name=%D4%CB%D0%D0" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">运行</span>测试用例后至少应该实现如下几个覆盖需求：</font></font><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman "><br style="line-height: normal;" />
</font></font></span></span><blockquote>
<ul>
    <li><span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="宋体 ">对程序模块的所有独立的执行路径至少覆盖一次。</font></font></span></span></li>
    <li><span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="宋体 ">对所有的逻辑判定，真假两种情况都至少覆盖一次。</font></font></span></span></li>
    <li><span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="宋体 ">在循环的边界和运行界限内执行循环体，即用边界值的方法来测试循环体。</font></font></span></span></li>
    <li><span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="宋体 ">测试内部数据结构的有效性等。</font></font></span></span></li>
</ul>
</blockquote><span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><br style="line-height: normal;" />
<font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman ">&nbsp; &nbsp;&nbsp;&nbsp; </font><font style="line-height: normal;" face="宋体 ">黑盒测试是要首先了解软件产品具备的功能和<span href="tag.php?name=%D0%D4%C4%DC" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">性能</span>等需求，再根据需求设计一组测试用例以验证程序内部活动是否符合<strong style="text-align: left; font-style: normal; line-height: normal; font-weight: bold;"><a href="http://www.ltesting.net/html/62/category-catid-162.html" target="_blank" style="text-decoration: none; color: #244d8f; line-height: normal;">需求分析</a></strong>和设计要求的活动。在黑盒测试范畴内通常使用功能覆盖率来度量测试的完整性。而功能覆盖率中最常见的就是需求覆盖，目的就是通过设计一定的测试用例，使得每个需求点都被测试到。其次，还包括接口覆盖</font><font style="line-height: normal;" face="Times New Roman ">(</font><font style="line-height: normal;" face="宋体 ">又叫入口点覆盖</font><font style="line-height: normal;" face="Times New Roman ">)</font><font style="line-height: normal;" face="宋体 ">，其目的就是通过设计一定的测试用例使<span href="tag.php?name=%CF%B5%CD%B3" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">系统</span>的每个接口都被测试到。黑盒测试应达到的覆盖率目标是：程序单元正确地实现了需求分析和设计上要求的所有功能，满足性能要求，同时程序单元要有<strong style="text-align: left; font-style: normal; line-height: normal; font-weight: bold;"><a href="http://www.ltesting.net/html/95/category-catid-95.html" target="_blank" style="text-decoration: none; color: #244d8f; line-height: normal;">可靠性</a></strong>和安全性。</font></font><br style="line-height: normal;" />
<font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman ">&nbsp; &nbsp;&nbsp;&nbsp; </font><font style="line-height: normal;" face="宋体 ">测试人员在实际工作中至少应该设计能够覆盖如下需求的基于功能的单元测试用例：</font></font><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman "><br style="line-height: normal;" />
</font></font></span></span><blockquote><span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman ">(1)</font><font style="line-height: normal;" face="宋体 ">测试程序单元的功能是否实现。</font></font></span></span><br style="line-height: normal;" />
<span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman ">(2)</font><font style="line-height: normal;" face="宋体 ">测试程序单元性能是否满足要求（可选）。</font></font></span></span><br style="line-height: normal;" />
<span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman ">(3)</font><font style="line-height: normal;" face="宋体 ">是否有可选的其他测试特性，如边界、余量、安全性、可靠性、强度（<span href="tag.php?name=%D1%B9%C1%A6" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">压力</span>）测试、人机交互界面测试等。</font></font></span></span><br style="line-height: normal;" />
</blockquote><span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman ">&nbsp; &nbsp;&nbsp;</font><font style="line-height: normal;" face="宋体 ">无论是白盒测试还是黑盒测试，每个测试用例都应该包含下面</font><font style="line-height: normal;" face="Times New Roman ">4</font><font style="line-height: normal;" face="宋体 ">个关键元素：</font></font><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman "><br style="line-height: normal;" />
</font></font></span></span><blockquote><span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman ">(1)</font><font style="line-height: normal;" face="宋体 ">被测单元模块初始状态声明，即测试用例的开始状态（仅适用于被测单元维持了调用中间状态的情况）。</font></font></span></span><br style="line-height: normal;" />
<span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman ">(2)</font><font style="line-height: normal;" face="宋体 ">被测单元的输入，包含由被测单元读入的任何外部数据值。</font></font></span></span><br style="line-height: normal;" />
<span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman ">(3)</font><font style="line-height: normal;" face="宋体 ">该测试用例实际测试的代码，用被测单元的功能和测试<strong style="text-align: left; font-style: normal; line-height: normal; font-weight: bold;"><span style="text-decoration: none; color: #244d8f; line-height: normal;">用例设计</span></strong>中使用的分析来说明，如：单元中哪一个决策条件被测试。</font></font></span></span><br style="line-height: normal;" />
<span style="border-collapse: separate; color: #000000; font-family: simsun; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; color: #444444; font-family: Verdana; font-size: 14px; line-height: 22px; text-align: left;"><font style="line-height: normal;" color="#000000"><font style="line-height: normal;" face="Times New Roman ">(4)</font><font style="line-height: normal;" face="宋体 ">测试用例的期望输出<span href="tag.php?name=%BD%E1%B9%FB" onclick="tagshow(event)" class="t_tag" style="border-bottom: 1px solid #ff0000; line-height: normal; cursor: pointer; white-space: nowrap;">结果</span>（在测试进行之前的测试说明中定义</font><font style="line-height: normal;" face="Times New Roman ">)</font><font style="line-height: normal;" face="宋体 ">。</font></font></span></span></blockquote>
<img src ="http://www.blogjava.net/wenhl5656/aggbug/282918.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wenhl5656/" target="_blank">爱吃鱼头</a> 2009-06-17 21:48 <a href="http://www.blogjava.net/wenhl5656/archive/2009/06/17/282918.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>