在上一篇博客中,我介绍了等价类划分法的应用,并通过EditBox这个例子进行了测试。在这次测试中,通过设置3个输入框,呈现出不同的测试效果。
首先复习等价类划分法:
划分等价类的方法:下面给出六条确定等价类的原则。
①在输入条件规定了取值范围或值的个数的情况下,则可以确立一个有效等价类和两个无效等价类。
②在输入条件规定了输入值的集合或者规定了“必须如何”的条件的情况下,可确立一个有效等价类和一个无效等价类.
③在输入条件是一个布尔量的情况下,可确定一个有效等价类和一个无效等价类。
④在规定了输入数据的一组值(假定n个),并且程序要对每一个输入值分别处理的情况下,可确立n个有效等价类和一个无效等价类。
⑤在规定了输入数据必须遵守的规则的情况下,可确立一个有效等价类(符合规则)和若干个无效等价类(从不同角度违反规则)。
⑥在确知已划分的等价类中各元素在程序处理中的方式不同的情况下,则应再将该等价类进一步的划分为更小的等价类。
设计测试用例:在确立了等价类后,可建立等价类表,列出所有划分出的等价类。
1.划分等价类
编号 | 有效等价类 | 编号 | 无效等价类 |
1 | 文本1长度为1-6 | 7 | 文本1长度小于1 |
8 | 文本1长度大于6 | ||
2 | 文本2长度为1-6 | 9 | 文本2长度小于1 |
10 | 文本2长度大于6 | ||
3 | 文本3长度为1-6 | 11 | 文本3长度小于1 |
12 | 文本3长度大于6 | ||
4 | 文本1字符位a-z,A-Z,0-9 | 13 | 文本1含英文/数字以外字符,控制字符,标点符号 |
5 | 文本2字符位a-z,A-Z,0-9 | 14 | 文本2含英文/数字以外字符,控制字符,标点符号 |
6 | 文本3字符位a-z,A-Z,0-9 | 15 | 文本3含英文/数字以外字符,控制字符,标点符号 |
2.设计测试用例:
编号 | 输入 | 覆盖等价类 | 期望输出 |
1 | 111aaa 222bbb 333ccc | 1,2,3,4,5,6 | OK |
2 |
222bbb 333ccc | 7,2,3,4,5,6 | |
3 | 111aaaaa 222bbb 333ccc | 8,2,3,4,5,6 | |
4 | 111aaa
333ccc | 9,1,3,4,5,6 | |
5 | 111aaa 222bbbbb 333ccc | 10,1,3,4,5,6 | |
6 | 111aaa 222bbb
| 11,1,2,4,5,6 | |
7 | 111aaa 222bbb 333ccccc | 12,1,2,4,5,6 | |
8 | 1$%^a 222bbb 333ccc | 13,1,2,3,5,6 | |
9 | 111aaa 2)&.bb 333ccc | 14,1,2,3,4,6 | |
10 | 111aaa 222bbb 3(&#!c | 15,1,2,3,4,5 |
测试结果:
源代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string result = "OK";
string str1 = textBox1.Text.ToString();
string str2 = textBox2.Text.ToString();
string str3 = textBox3.Text.ToString();
if (str1.Length < 1 || str1.Length > 6 || str2.Length < 1 || str2.Length > 6
|| str3.Length < 1 || str3.Length > 6)
result = "fail";
foreach (char c in str1)
{
if ((!char.IsLetter(c)) && (!char.IsNumber(c)))
{
result = "fail";
}
}
foreach (char c in str2)
{
if ((!char.IsLetter(c)) && (!char.IsNumber(c)))
{
result = "fail";
}
}
foreach (char c in str3)
{
if ((!char.IsLetter(c)) && (!char.IsNumber(c)))
{
result = "fail";
}
}
label.Text = result;
}
}
}