用java等价类划分方法和边界值方法设计测试用例并提交代码。 用户注册 登 录 名:______(由数字或字母组成,以字母开头,长度4-16位) 年 龄:______(0~150之间的正整数) 学 历:______(专科、本科、硕士、博士) 出生 日期:______(格式:YYYY-MM-DD,如:1900-01-01)
时间: 2024-03-16 09:47:37 浏览: 78
等价类划分,设计测试用例
5星 · 资源好评率100%
以下是用Java实现的等价类划分方法和边界值方法设计测试用例的代码:
```java
import java.util.ArrayList;
import java.util.List;
public class TestCases {
// Name
private static final String VALID_NAME = "Tom1234";
private static final String VALID_NAME_2 = "Alice";
private static final String VALID_NAME_3 = "1234abcd5678efgh";
private static final String INVALID_NAME = "t1234";
private static final String INVALID_NAME_2 = "abcdefghijklmnopq";
private static final String INVALID_NAME_3 = "1234abcd5678efghijklmno";
private static final int MIN_NAME_LENGTH = 4;
private static final int MAX_NAME_LENGTH = 16;
// Age
private static final int VALID_AGE = 18;
private static final int VALID_AGE_2 = 30;
private static final int VALID_AGE_3 = 150;
private static final int INVALID_AGE = -1;
private static final int INVALID_AGE_2 = 151;
private static final double INVALID_AGE_3 = 20.5;
private static final String INVALID_AGE_4 = "abc";
private static final int MIN_AGE = 0;
private static final int MAX_AGE = 150;
// Education
private static final String VALID_EDUCATION = "专科";
private static final String VALID_EDUCATION_2 = "本科";
private static final String VALID_EDUCATION_3 = "硕士";
private static final String VALID_EDUCATION_4 = "博士";
private static final String INVALID_EDUCATION = "研究生";
private static final String INVALID_EDUCATION_2 = "大专";
private static final String INVALID_EDUCATION_3 = "bachelor";
private static final String INVALID_EDUCATION_4 = "master";
// Date of Birth
private static final String VALID_DATE = "1990-01-01";
private static final String VALID_DATE_2 = "2000-12-31";
private static final String VALID_DATE_3 = "2010-02-28";
private static final String INVALID_DATE = "1990/01/01";
private static final String INVALID_DATE_2 = "2000-13-01";
private static final String INVALID_DATE_3 = "2019-02-31";
private static final String MIN_DATE = "1900-01-01";
private static final String MAX_DATE = "2150-12-31";
/*
* Equivalent Class Testing
*/
public static List<String> testEquivalentClasses() {
List<String> testCases = new ArrayList<>();
// Name
testCases.add(VALID_NAME); // valid
testCases.add(VALID_NAME_2); // valid
testCases.add(VALID_NAME_3); // valid
testCases.add(INVALID_NAME); // invalid
testCases.add(INVALID_NAME_2); // invalid
testCases.add(INVALID_NAME_3); // invalid
// Age
testCases.add(String.valueOf(VALID_AGE)); // valid
testCases.add(String.valueOf(VALID_AGE_2)); // valid
testCases.add(String.valueOf(VALID_AGE_3)); // valid
testCases.add(String.valueOf(INVALID_AGE)); // invalid
testCases.add(String.valueOf(INVALID_AGE_2)); // invalid
testCases.add(String.valueOf(INVALID_AGE_3)); // invalid
testCases.add(INVALID_AGE_4); // invalid
// Education
testCases.add(VALID_EDUCATION); // valid
testCases.add(VALID_EDUCATION_2); // valid
testCases.add(VALID_EDUCATION_3); // valid
testCases.add(VALID_EDUCATION_4); // valid
testCases.add(INVALID_EDUCATION); // invalid
testCases.add(INVALID_EDUCATION_2); // invalid
testCases.add(INVALID_EDUCATION_3); // invalid
testCases.add(INVALID_EDUCATION_4); // invalid
// Date of Birth
testCases.add(VALID_DATE); // valid
testCases.add(VALID_DATE_2); // valid
testCases.add(VALID_DATE_3); // valid
testCases.add(INVALID_DATE); // invalid
testCases.add(INVALID_DATE_2); // invalid
testCases.add(INVALID_DATE_3); // invalid
return testCases;
}
/*
* Boundary Value Testing
*/
public static List<String> testBoundaryValues() {
List<String> testCases = new ArrayList<>();
// Name
testCases.add("Tom1"); // minimum valid
testCases.add("abcd"); // minimum valid
testCases.add("Tom123456789012345"); // maximum valid
testCases.add("abcdefghijklmnop"); // maximum valid
// Age
testCases.add(String.valueOf(MIN_AGE)); // minimum valid
testCases.add(String.valueOf(MIN_AGE + 1)); // minimum valid
testCases.add(String.valueOf(MIN_AGE + 2)); // minimum valid
testCases.add(String.valueOf(MAX_AGE - 2)); // maximum valid
testCases.add(String.valueOf(MAX_AGE - 1)); // maximum valid
testCases.add(String.valueOf(MAX_AGE)); // maximum valid
// Education
testCases.add("专科"); // minimum valid
testCases.add("博士"); // maximum valid
// Date of Birth
testCases.add(MIN_DATE); // minimum valid
testCases.add(MAX_DATE); // maximum valid
return testCases;
}
/*
* Main method to test the test cases
*/
public static void main(String[] args) {
System.out.println("Equivalent Class Test Cases:");
List<String> eqTestCases = testEquivalentClasses();
for (String testCase : eqTestCases) {
System.out.println(testCase);
}
System.out.println("\nBoundary Value Test Cases:");
List<String> bvTestCases = testBoundaryValues();
for (String testCase : bvTestCases) {
System.out.println(testCase);
}
}
}
```
上述代码实现了等价类划分法和边界值法的测试用例生成,在main方法中可以分别调用testEquivalentClasses和testBoundaryValues方法来生成等价类和边界值的测试用例。运行该程序后,将会输出生成的测试用例。
阅读全文