標籤:
前景概要:這篇文章主要針對上一篇題目做出一些擴充性的修改,從而導致等價類別等價類別的變化。這邊文章實現了增加至3個editbox的等價類別劃分方法,從而加強了我對於等價類別劃分的理解。
1、題目內容:編寫3個editBox,要求EditBox 允許1到6個英文字元或數字,按OK結束
有效等價類別: 長度:1到6 字元:a-z,A-Z,0-9。
無效等價類別 長度:0,7 字元:英文/數字以外字元,控制字元,標點符號。
2、關於等價類別的一些必要知識我就不再贅述,上篇文章有提過。
3、劃分等價類別
| 編號 |
有效等價類別 |
編號 |
無效等價類別 |
| 1 |
長度為1-6 |
6 |
null |
| 2 |
數字0-9 |
7 |
長度大於6 |
| 3 |
小寫字母a-z |
8 |
英文/數字以外字元,控制字元,標點符號 |
| 4 |
大寫字母A-Z |
9 |
N0.1正確,N0.1正確,N0.1錯誤 |
| 5 |
正確輸入個數為3 |
10 |
N0.1正確,N0.1錯誤,N0.1正確 |
| |
|
11 |
N0.1正確,N0.1錯誤,N0.1錯誤 |
| |
|
12 |
N0.1錯誤,N0.1正確,N0.1正確 |
| |
|
13 |
N0.1錯誤,N0.1正確,N0.1錯誤 |
| |
|
14 |
N0.1錯誤,N0.1錯誤,N0.1正確 |
| |
|
15 |
N0.1錯誤,N0.1錯誤,N0.1錯誤 |
4、測試案例
| 編號 |
測試案例 |
覆蓋等價類別 |
期望輸出 |
| 1 |
3a4B5c,3a4B5c,3a4B5c |
1,2,3,4.5 |
No.0:ok No.1:ok No.2:ok |
| 2 |
123,123,null |
6,9 |
No.0:ok No.1:ok No.2:輸入不可為空 |
| 3 |
123,123,1234567 |
7,9 |
No.0:ok No.1:ok No.2:輸入字元長度過長 |
| 4 |
123,123,[email protected]#¥ |
8,9 |
No.0:ok No.1:ok No.2:只能輸入英文字母或者數字 |
| 5 |
123,[email protected]#¥,123 |
10 |
No.0:ok No.1:只能輸入英文字母或者數字 No.2:ok |
| |
123,[email protected]#¥,[email protected]#¥ |
11 |
No.0:ok No.1:只能輸入英文字母或者數字 No.2:只能輸入英文字母或者數字 |
| 7 |
[email protected]#¥, 123,123 |
12 |
No.0:只能輸入英文字母或者數字 No.1:ok No.2:ok |
| 8 |
[email protected]#¥, 123,1234567 |
13 |
No.0:只能輸入英文字母或者數字 No.1:ok No.2:輸入字元長度過長 |
| 9 |
[email protected]#¥, 1234567,123 |
14 |
No.0:只能輸入英文字母或者數字 No.1:輸入字元長度過長 No.2:ok |
| 10 |
[email protected]#¥, 1234567,null |
15 |
No.0:只能輸入英文字母或者數字 No.1:輸入字元長度過長 No.2:輸入不可為空 |
5、編寫代碼
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class test3 {
public final static boolean RIGHT_TO_LEFT = false;
static JButton jb = new JButton("OK");
static JTextField tf = new JTextField("");
static JTextField tf2 = new JTextField("");
static JTextField tf3 = new JTextField("");
static JTextArea ta = new JTextArea("");
public static void addComponentsToPane(Container contentPane) {
if (RIGHT_TO_LEFT) {
contentPane.setComponentOrientation(
ComponentOrientation.RIGHT_TO_LEFT);
}
// Any number of rows and 2 columns
contentPane.setLayout(new GridLayout(6,1));
contentPane.add(new JLabel("Enter your name"));
contentPane.add(tf);
contentPane.add(tf2);
contentPane.add(tf3);
contentPane.add(jb);
contentPane.add(ta);
//jb.setMnemonic(KeyEvent.VK_I); //Set ShortCut Keys
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String[] test = new String[3];
test[0] = tf.getText();
test[1] = tf2.getText();
test[2] = tf3.getText();
ta.setText("");
for (int i = 0; i < 3; i++) {
if (test[i].matches("[A-Za-z0-9]{1,6}"))
ta.setText(ta.getText()+"No."+i+":ok"+"\n");
else if (test[i].length() <= 0)
ta.setText(ta.getText()+"No."+i+":輸入不可為空"+"\n");
else if (test[i].length() > 6)
ta.setText(ta.getText()+"No."+i+":輸入字元長度過長"+"\n");
else
ta.setText(ta.getText()+"No."+i+":只能輸入英文字母或者數字"+"\n");
}
}
});
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("GridLayout Source Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane and components in GridLayout
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
以上java代碼編譯環境是window8 64位系統,編譯器是eclipse。
6、測試結果
吻合期望的結果,證明測試成功。
7、總結:通過這次的軟體測試實際操作,更加深刻地理解等價類別劃分,以及更加瞭解軟體測試的重要性。隨著軟體體系的逐步增大我們與之對應的軟體測試也應該隨之應變,達到消耗盡量少的資源已完成所有我們想要檢測的部分。
8、歡迎大家對我寫的部落格批評指正。
關於軟體測試(3):基於上一篇等價類別劃分的擴充應用