黑盒测试之等价类划分测试
实验要求:
分析电话号码的输入情况,划分等价类表(要求考虑无效输入);根据(1)所划分的等价类,给出具体的测试用例,实施测试。据此实验生成测试报告2。测试报告中至少包含等价类列表,测试用例及测试结论。
自选语言编写程序:输入电话号码,对电话号码的合法性进行检查,输出是否合法,不合法在屏幕上输出错误原因。
电话号码合法要求:
电话号码由三部分构成
地区码:空白或三位数字
前缀码:非"0"或”1”开头的三位数字
后缀码:四位数字
符合以上要求的为合法电话号码
确认等价类,列出等价类表:
输入条件
| 有效等价类
| 编号
| 无效等价类
| 编号
| 电话号码长度
| 7
| 1
| 小于7
| 10
| 10
| 2
| 大于7小于10
| 11
| 大于10
| 12
| 电话号码长度为7时首位字符类型
| 非0非1的数字
| 3
| 0
| 13
| 1
| 14
| 非数字的字符
| 15
| 电话号码长度为7时前缀码后两位字符类型
| 数字
| 4
| 非数字的字符
| 16
| 电话号码长度为7时后缀码字符类型
| 数字
| 5
| 非数字的字符
| 17
| 电话号码长度为10时第4位字符类型
| 非0非1的数字
| 6
| 0
| 18
| 1
| 19
| 非数字的字符
| 20
| 电话号码长度为10时前缀码后两位字符类型
| 数字
| 7
| 非数字的字符
| 21
| 电话号码长度为10时后缀码字符类型
| 数字
| 8
| 非数字的字符
| 22
| 电话号码长度为10时地区码字符类型
| 数字
| 9
| 非数字的字符
| 23
| 测试用例表:
测试用例
| 输入数据
| 预期结果
| 覆盖的等价类
| 地区码
| 前缀码
| 后缀码
| 1
| 空白
| 222
| 3333
| 输入合法
| 1,3,4,5
| 2
| 111
| 222
| 3333
| 输入合法
| 2,6,7,8,9
| 3
| 空白
| 22
| 3333
| 输入不合法,电话号码长度不合格
| 10
| 4
| 空白
| 222
| 333
| 输入不合法,电话号码长度不合格
| 11
| 5
| 111
| 2222
| 3333
| 输入不合法,电话号码长度不合格
| 12
| 6
| 空白
| 022
| 3333
| 输入不合法,前缀码有误
| 13
| 7
| 空白
| 122
| 3333
| 输入不合法,前缀码有误
| 14
| 8
| 空白
| a12
| 3333
| 输入不合法,前缀码有误
| 15
| 9
| 空白
| 2aa
| 3333
| 输入不合法,前缀码有误
| 16
| 10
| 空白
| 222
| aaaa
| 输入不合法,后缀码有误
| 17
| 11
| 111
| 022
| 3333
| 输入不合法,前缀码有误
| 18
| 12
| 111
| 122
| 3333
| 输入不合法,前缀码有误
| 19
| 13
| 111
| a12
| 3333
| 输入不合法,前缀码有误
| 20
| 14
| 111
| 2aa
| 3333
| 输入不合法,前缀码有误
| 21
| 15
| 111
| 222
| aaaa
| 输入不合法,后缀码有误
| 22
| 16
| aaa
| 222
| 3333
| 输入不合法,地区码有误
| 23
| 部分用例执行后结果演示:
演示(测试编号1):

演示(测试编号2):

演示(测试编号3):

演示(测试编号4):

演示(测试编号5):

演示(测试编号6):

演示(测试编号7):

演示(测试编号8):

演示(测试编号9):

演示(测试编号10):

演示(测试编号11):

演示(测试编号12):

演示(测试编号13):

演示(测试编号14):

演示(测试编号15):

演示(测试编号16):

实验代码:
- import java.util.Scanner;
- public class test2 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入您的电话号码:");
- String phone = sc.next();
- System.out.println("您输入的电话号码为:" + phone);
- boolean boom = judge(phone);
- if (boom == true) {
- System.out.println("您输入的电话号码是合法的");
- }
- else {
- System.out.println("您输入的电话号码是非法的");
- }
- }
- public static boolean isNumber(String str, int length) {
- for (int i = 0; i < length; i++) {
- if (!Character.isDigit(str.charAt(i))) {
- return false;
- }
- }
- return true;
- }
- public static boolean judge(String phone){
- char fourchar =phone.charAt(3);
- char firstchar = phone.charAt(0);
- int length = phone.length();
- if (isNumber(phone,phone.length())){
- if (length == 10){
- if (fourchar=='0' || fourchar=='1'){
- System.out.println("提示:前缀码应该是除了0和1开头的三位数字!");
- return false;
- }
- else{
- return true;
- }
- }
- else if (length == 7){
- if (firstchar=='0' || firstchar=='1'){
- System.out.println("提示:前缀码应该是除了0和1开头的三位数字!");
- return false;
- }
- else {
- return true;
- }
- }
- else {
- System.out.println("提示:电话号码的长度应为 7或 10位!");
- return false;
- }
- }
- else{
- if (phone.length()==10){
- if (isNumber(phone,3)==false){
- System.out.println("提示:地区码为非数字!");
- }
- else if (isNumber(phone,6)==false){
- System.out.println("提示:前缀码为非数字!");
- }
- else {
- System.out.println("提示:后缀码为非数字!");
- }
- }
- else {
- if (isNumber(phone,3)==false){
- System.out.println("提示:前缀码为非数字!");
- }
- else{
- System.out.println("提示:后缀码为非数字!");
- }
- }
- System.out.println("电话号码不应当出现除了数字以外的字符!");
- return false;
- }
- }
- }
复制代码
测试结论:
预期结果与程序执行结果一致,说明测试成功,程序无异常。写到测试用例才发现程序的条件有冗余,导致测试用例重复。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |