软件测试第三周作业——测试多个输入的合法性

这次作业,我用JavaFX来写程序测试输入的合法性

这是我的输入界面截图,

三个框同时输入,

按submit即可测试三个输入的合法性,

最后在控制台显示测试结果

测试用例:

等价类的划分:

有效等价类:长度1-6,仅含有a-z,A-Z,0-9的字符

无效等价类:长度0或大于7,含有但并不一定都是除a-z,A-Z,0-9以外的字符,控制字符、标点符号等。

以下是测试用例

第一次测试:

输入:  er43t

     dgddsgdfg

     fds!#

输出:  

     OK
     所有输入长度应为1-6
     所有输入字符应为a-z,A-Z,0-9

截图:

第2次测试:

输入:  null

     4234

     #$$%%

输出:  

     所有输入长度应为1-6

     OK

     所有输入字符应为a-z,A-Z,0-9

截图:

第3次测试:

输入:  24324243

     adfs

     空格

输出:  

     所有输入长度应为1-6

     OK
     所有输入字符应为a-z,A-Z,0-9

截图:

代码如下:

package softwtest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class test extends Application{

  public static boolean isRegularRptCode(String rptCode,String regEx) {
    Pattern p1 = Pattern.compile(regEx);
    Matcher m1 = p1.matcher(rptCode);
    boolean rs1 = m1.matches();
    return rs1;
  }

  public static void main(String[ ] args) {
    test.launch( args );
  }
  public void start( Stage Stage ) {
    Stage.setTitle( "test" );
    AnchorPane root = new AnchorPane();
    Stage.setScene(new Scene(root, 500, 250));

    Text name1= new Text("姓名");
    Text name2= new Text("密码");
    Text name3= new Text("验证码");

    final TextField []tf = new TextField[3];

    tf[0] = new TextField();
    tf[1] = new TextField();
    tf[2] = new TextField();

    Button btn = new Button( );
    btn.setText("submit");


    AnchorPane.setTopAnchor(name1, 50.0);
    AnchorPane.setLeftAnchor(name1, 50.0);

    AnchorPane.setTopAnchor(name2, 100.0);
    AnchorPane.setLeftAnchor(name2, 50.0);

    AnchorPane.setTopAnchor(name3, 150.0);
    AnchorPane.setLeftAnchor(name3, 50.0);

    AnchorPane.setTopAnchor(tf[0], 50.0);
    AnchorPane.setLeftAnchor(tf[0], 105.0);

    AnchorPane.setTopAnchor(tf[1], 100.0);
    AnchorPane.setLeftAnchor(tf[1], 105.0);

    AnchorPane.setTopAnchor(tf[2], 150.0);
    AnchorPane.setLeftAnchor(tf[2], 105.0);

    AnchorPane.setTopAnchor(btn, 200.0);
    AnchorPane.setLeftAnchor(btn, 210.0);

    btn.setOnAction( new EventHandler<ActionEvent>( ) {
      public void handle(ActionEvent actEvt) {
        final String []namex = new String[3];
        for(int i=0;i<3;i++){
          namex[i] = tf[i].getText();
          if(namex[i].length()<1||namex[i].length()>6){
            System.out.println("所有输入长度应为1-6");
          }
          else if(!isRegularRptCode(namex[i],"[a-z,A-Z,0-9]*")){
            System.out.println("所有输入字符应为a-z,A-Z,0-9");
          }
          else{
            System.out.println("OK");
          }
        }
      }
    } );

    root.getChildren().add(btn);

    root.getChildren().add(name1);
    root.getChildren().add(name2);
    root.getChildren().add(name3);

    root.getChildren().add(tf[0]);
    root.getChildren().add(tf[1]);
    root.getChildren().add(tf[2]);

    Stage.show( );
  }
}

原文地址:https://www.cnblogs.com/tianzh/p/4375746.html