转:Top 10 Negative Test Cases

2010-07-16  关敏 

负面测试(Negative testing)是相对于正面测试(Positive testing)而言的。它们也是测试设计时的两个非常重要的划分。简单点说,正面测试就是测试系统是否完成了它应该完成的工作;而负面测试就是测试系统是否不执行它不应该完成的操作。形象一点,正面测试就象一个毕恭毕敬的小学生,老师叫我做什么,我就做什么;而负面测试就象一个调皮捣蛋的孩子,你叫我这样做,我偏不这样做,而且和你对着干。开发人员也是最讨厌修改此类bug的。

正面测试主要根据需求,功能说明书,设计文档等相关参考文档来执行测试,而负面测试则主要根据错误猜测,逆向思维来测试系统,一定程序上的的依赖测试人员的经验积累。

执行负面测试时,不单单要测试系统是否处理了用户的异常操作,还要检查系统对于这些异常操作是否给予了正确的错误提示。它是系统对用户进行继续正确操作的指引。

简言之负面测试的三部曲就是:

1、检查程序中的屏幕或页面是否给出了清晰且充分的提示或约束;
2、测试系统是否处理了用户的异常操作;
3、检查系统的错误提示是否清晰且充分。

以下是Steve Miller的《Top 10 Negative Test Cases》,概括性的提到了一些做负面测试时经常需要注意的测试。

负面测试用例被设计于用软件未意欲被使用的方式测试软件,它也应该是测试工作的一部分。以下就是在设计测试工作量时你应该考虑的十大负面测试用例。

Negative test cases are designed to test the software in ways it was not intended to be used, and should be a part of your testing effort.  Below are the top 10 negative test cases you should consider when designing your test effort:

1、植入的单引号。大多数基于SQL的数据库系统在用户存储包含一个单引号的信息时会出现问题,例如John's car。每一个可以接受文字数字型数据条目的屏幕都要试试输入包含一个或多个单引号的文本。

【补充】其实不只是单引号,基本上测试人员应该测试所有的特殊字符和空/空格(单纯的空格和文本前后的空格)。单引号,逗号,/,<,>(对于web的应用程序)都是很容易引发错误的。在开发早期测试组就可以建议开发组写一个通用的函数来处理这些特殊字符,然后在处理用户的输入时套用这个函数就可以避免此类错误了。

1 Embedded Single Quote - Most SQL based database systems have issues when users store information that contain a single quote (e.g. John's car).  For each screen that accepts alphanumeric data entry, try entering text that contains one or more single quotes.



2、必需输入的数据条目。功能说明书上应该清楚的指出屏幕上必须输入数据条目的字段。测试屏幕上每一个被说明为必须输入的字段以保证它强制要求你在字段中输入数据。

【补充】对于强制输入的字段,在屏幕上最好有些标识以说明其为必须输入的字段。一般在字段前或后用红色的*号表示。测试时必须要检查有标识的字段是否和功能说明书或其他参考文档一致,错误信息提示是否正确,强制输入的字段是否真的必须输入。

2 Required Data Entry - Your functional specification should clearly indicate fields that require data entry on screens.  Test each field on the screen that has been indicated as being required to ensure it forces you to enter data in the field.



3、字段类型测试。功能说明书上应该清楚的指出要求特定数据输入要求(日期字段,数字字段,电话号码,邮编等等)的字段。测试屏幕上每一个被指出有特定类型的字段以保证你输入了基于字段类型的符合正确格式的数据(数字型字段应该不允许字符或特殊字符,日期型的字段应该允许输入一个正确的日期等等)

【补充】其实这里还有一个字段格式和字段内容的测试。有些字段对输入的格式有要求,这些字段的格式一般在屏幕上也有相应的提示。所以在测试时需要测试提示的格式是否合理(和功能说明书或其他参考文档相一致)以及系统是否正确识别输入的格式。有些字段对字段的内容有限制,如常见的用户名,不能包含特殊字符,首字不能未数字等要求。所以在测试时需要测试提示的格式是否合理(和功能说明书或其他参考文档相一致)还有不符合内容要求的数据输入时系统是否正确的处理。

3 Field Type Test -  Your functional specification should clearly indicate fields that require specific data entry requirements (date fields, numeric fields, phone numbers, zip codes, etc).  Test each field on the screen that has been indicated as having special types to ensure it forces you to enter data in the correct format based on the field type (numeric fields should not allow alphabetic or special characters, date fields should require a valid date, etc).



4、字段长度测试。功能说明书上应该清楚的指出可以在字段中输入的字符数(例如,first name必须是50个或更少的字符)。写测试用例以保证你只可以输入特定的字符数。防止用户输入比允许范围更多的字符比因用户已输入过多的字符而给出的错误信息更加的文雅些。

【补充】一般对于限制长度的字段,现在开发大多采用限制输入的方法(设置字段的长度)来处理。所以测试时需要测试限制的长度是否合理(和功能说明书或其他参考文档相一致),对于没有限制长度的字段,要测试无穷输入时是否出错,有问题报bug时建议开发人员根据需要限制长度。

4 Field Size Test -  Your functional specification should clearly indicate the number of characters you can enter into a field (for example, the first name must be 50 or less characters).  Write test cases to ensure that you can only enter the specified number of characters. Preventing the user from entering more characters than is allowed is more elegant than giving an error message after they have already entered too many characters.



5、数字型的边界测试。对于数字型的字段,测试上下边界是非常重要的。例如,如果你正在计算某个账户的利息时,你永远不会输入一个负的利息数给应该赢取利息的账户。因此,你应该尝试用负数测试。同样,如果功能说明书上要求字段在某一个特定的范围(如从10~50),你就应该尝试输入9或51,它应该给出一个得体的信息表示失败。

5 Numeric Bounds Test - For numeric fields, it is important to test for lower and upper bounds. For example, if you are calculating interest charged to an account, you would never have a negative interest amount applied to an account that earns interest, therefore, you should try testing it with a negative number.   Likewise, if your functional specification requires that a field be in a specific range (e.g. from 10 to 50), you should try entering 9 or 51, it should fail with a graceful message.



6、数字的约束测试。大多数数据库系统和编程语言允许数字条目被识别为整数或长整数。通常,整数的范围是从-32,767~32,767,长整数的范围从-2,147,483,648~2,147,483,647。对于那些没有特定边界限制的数字数据条目,用这些限制测试以确保不会出现数字的溢出错误。

【补充】小数型的数字字段同样也需要格外的测试。一般对于未指出数字类型的字段,尝试输入负整数,负小数,0,正整数,正小数进行测试。

不管是哪种数据库系统,对于数字一般都有多种数字类型。所以测试人员一定要测试的全面。

6 Numeric Limits Test - Most database systems and programming languages allow numeric items to be identified as integers or long integers.  Normally, an integer has a range of -32,767 to 32,767 and long integers can range from 
-2,147,483,648 to 2,147,483,647.  For numeric data entry that do not have specified bounds limits, work with these limits to ensure that it does not get an numeric overflow error.



7、日期边界测试。对于日期型的字段,测试上下边界是很重要的。例如,如果你正在检查一个出生日期的字段,很大可能出生日期不能早于150年前。同样,出生日期应该不是将来的某一天。

【补充】一般来说,每种数据库系统的日期都有个范围,如SQL Server最小日期是1753年1月1日,所以如果是输入型的日期字段同样也应该测试早于1753的日期。

7 Date Bounds Test - For date fields, it is important to test for lower and upper bounds. For example, if you are checking a birth date field, it is probably a good bet that the person's birth date is no older than 150 years ago.  Likewise, their birth date should not be a date in the future.



8、日期的有效性。对于日期字段,确保不允许无效的日期是很重要的(04/31/2007是一个无效的日期)。测试用例也应该检查闰年(每个第4年和第400年是一个闰年)。

8 Date Validity - For date fields, it is important to ensure that invalid dates are not allowed (04/31/2007 is an invalid date).  Your test cases should also check for leap years (every 4th and 400th year is a leap year).



9、web会话测试。很多的web应用程序依赖浏览器的会话来追踪已登录的用户,应用程序的设置等等。应用程序的大多数屏幕不被设计为没有首次登录就可以被运行。应用程序应该确保在打开应用程序的某一页面之前会话里有一个有效的登录。

9 Web Session Testing - Many web applications rely on the browser session to keep track of the person logged in, settings for the application, etc.  Most screens in a web application are not designed to be launched without first logging in.   Create test cases to launch web pages within the application without first logging in.  The web application should ensure it has a valid logged in session before rendering pages within the application.



10、性能的改变。当发布产品的最新版本时,应该有一套运行于识别屏幕(列出信息的屏幕,add/update/delete数据的屏幕等等)速度的性能测试。测试包里应该包括比较先前版本和现有版本性能统计值的测试用例。这个可以帮助识别那些可以证明是随着对现有版本的代码变更而引起的潜在的性能问题。

【补充】从第一条到第八条是我们在测试字段时常常需要做的测试,一般的测试人员都不陌生。第九条在测试web应用程序中会作为检查应用程序的安全性而做的一项测试。而第十条估计很多公司都不会将它考虑到测试的范畴中,一般最多也就是在测试用例中添加校验某一个操作是否在系统允许的响应时间里,很少去做这样的比较,除了一些有针对性的性能测试。 

10 Performance Changes - As you release new versions of your product, you should have a set of performance tests that you run that identify the speed of your screens (screens that list information, screens that add/update/delete data, etc).   Your test suite should include test cases that compare the prior release performance statistics to the current release.  This can aid in identifying potential performance problems that will be manifested with code changes to the current release.

1198°/11928 人阅读/6 条评论 发表评论

刘志强  2010-07-16

沙发


焦爱玲  2010-07-16

学习了


李蕾  2010-07-19

谢谢LZ,学习了


刘大荣  2010-07-19

Negative/ Positive  受教了。


唐燕  2010-07-19

楼主挺好学啊...


李育春  2010-11-24

学习了。


登录 后发表评论