实例分析等价类划分(二)
基于我上一篇博客http://www.cnblogs.com/xlwm/p/4347304.html 的问题稍加修改,就可以得到一个截然不同的实例:
1.问题描述
3个EditBox,每个允许输入1到6个英文字符或数字,按OK输入结束
(通过输入字符串,返回正误信息)
2.等价类划分
有效等价类 | 编号 | 无效等价类 | 编号 | |
长度 | 1-6 | 1 | 0,6以上 | 4 |
字符 | a-z,A-Z,0-9 | 2 | 英文/数字以外字符,控制字符,标点符号 | 5 |
合理个数 | 3 | 3 | 0,1,2 | 6 |
3.测试用例
针对等价类的划分,我们可以选取以下作为测试用例:
编号 | 输入 | 覆盖等价类 | 预期输出 |
Test1 |
String 1 = "1" String 2 = "2a3w9P" |
1,2,3 | It's OK. |
Test2 |
String 1 = "2w4" String 2 = "rer" |
1,2,3 | It's OK. |
Test3 |
String 1 = "a' String 2 = “1” |
1,2,3 | It's OK. |
Test4 |
String 1 = “123456" String 2 = "AFDERT" |
1,2,3 | It's OK. |
Test5 |
String 1 = "1a9T0z" String 2 = "fd" |
1,2,3 | It's OK. |
Test6 |
String 1 = "fdas" String 2 = "23" |
1,2,3 | It's OK. |
Test7 |
String 1 = "" String 2 = "fadf" |
4,6 |
Please ensure each string is reasonable.
|
Test8 |
String 1 = "ei" String 2 = "112;2312" |
4,5,6 |
Please ensure each string is reasonable.
|
Test9 |
String 1 = “12” String 2 = “dafadfsfe" |
4,6 |
Please ensure each string is reasonable.
|
Test10 |
String 1 = "12" String 2 = ""afds;" |
5,6 |
Please ensure each string is reasonable.
|
Test11 |
String 1 = "" String 2 = "f^;" |
4,5,6 |
Please ensure each string is reasonable.
|
Test12 |
String 1 = "fad" String 2 = "fdas" |
5,6 |
Please ensure each string is reasonable.
|
4.代码实现及结果样例:
只是在原来代码的基础上对判断条件和布局稍加修改,因而这里不再赘述实现原理,最终代码如下:
1 __author__ = 'xinlan' 2 # -*- coding: utf-8 -* 3 4 from Tkinter import * 5 import tkMessageBox 6 import re 7 8 def judge(str): 9 if re.match(r'^\w{1,6}$', str): 10 return True 11 else: 12 return False 13 14 class Application(Frame): 15 def __init__(self,master=None): 16 Frame.__init__(self, master) 17 self.createWidgets(master) 18 19 def createWidgets(self,master): 20 Label(master,text='String 1:').grid(row=0) 21 self.nameInput1 = Entry(master) 22 self.nameInput1.grid(row=0,column=1) 23 Label(master,text='String 2:').grid(row=1) 24 self.nameInput2 = Entry(master) 25 self.nameInput2.grid(row=1,column=1) 26 Label(master,text='String 3:').grid(row=2) 27 self.nameInput3 = Entry(master) 28 self.nameInput3.grid(row=2,column=1) 29 self.alertButton = Button(master, text='OK', command=self.ok) 30 self.alertButton.grid(column=1) 31 32 def ok(self): 33 name1 = self.nameInput1.get() 34 name2 = self.nameInput2.get() 35 name3 = self.nameInput3.get() 36 if(judge(name1) and judge(name2) and judge(name3)): 37 tkMessageBox.showinfo('Message', 'It\'s OK') 38 self.nameInput1.delete(0,last=len(name1)) 39 self.nameInput2.delete(0,last=len(name2)) 40 self.nameInput3.delete(0,last=len(name3)) 41 42 else: 43 tkMessageBox.showinfo('Message','Please ensure each string is reasonable!') 44 self.nameInput1.delete(0,last=len(name1)) 45 self.nameInput2.delete(0,last=len(name2)) 46 self.nameInput3.delete(0,last=len(name3)) 47 48 app = Application() 49 # 设置窗口标题: 50 app.master.title('Test') 51 # 主消息循环: 52 app.tk.mainloop()
这是部分测试用例的结果图:
(点击OK出现结果,点OK返回主界面)
(1)
(2)
(3)
(4)
5.总结:
由此可见,不同问题的等价类划分也不同,即使两者很相似,也会导致测试用例千差万别,而这正反映了对测试人员的要求之高。因此我们必须客观全面地分析问题,以便能够合理地进行测试分析。