軟體測試第三周之測試多個輸入的合法性

來源:互聯網
上載者:User

標籤:

這一次我主要用的是C#中的Regex來測試使用者輸入的字串是否合法。

這是我的UI介面:

  1.   三個輸入框允許使用者同時進行輸入
  2.   輸入後摁確定鍵即可輸出測試結果,摁下取消鍵即可重新進行輸入
  3.   輸出的測試資訊顯示在右側的輸出文字框中

測試案例:

第一步:等價類別型的劃分

有效等價類別 無效等價類別
長度:1到6 長度:0或者大於7
字元:a-z,A-Z,0-9  字元:英文/數字以外字元,控制字元,標點符號

第二步:測試案例的產生

  
編號 輸入字元 期望輸出
1 A 有效輸入
2 Z 有效輸入
3 55136s 有效輸入
4 zAGsdf 有效輸入
5 null 無效輸入
6 @ 無效輸入
7 空格 無效數入
8 *@#$%^ 無效輸入
9 @123 無效輸入
10 12345# 無效輸入
11 uyueiuwy 無效輸入

測試結果:

下面是代碼部分:

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 button1_Click(object sender, EventArgs e)
        {
            String[] str = new String[3];
            
            str[0] = textBox1.Text;
            str[1] = textBox2.Text;
            str[2] = textBox3.Text;

            Boolean res1=System.Text.RegularExpressions.Regex.IsMatch(str[0], @"^[a-zA-Z\d]{1,6}$");
            Boolean res2 = System.Text.RegularExpressions.Regex.IsMatch(str[1], @"^[a-zA-Z\d]{1,6}$");
            Boolean res3 = System.Text.RegularExpressions.Regex.IsMatch(str[2], @"^[a-zA-Z\d]{1,6}$");
            if (res1 && res2 && res3)
            {
                label4.Text = "輸入正確!";
            }
            else
            {
                label4.Text = "輸入不合法,請重新輸入!";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            label4.Text = "請先輸入";
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}

代碼中的核心部分就是Regex判段輸入字串是否合法的部分,我使用的Regex是:

@"^[a-zA-Z\d]{1,6}$"

即判斷每一位是否為字母或者數字,且出現的次數為1-6次。

 

軟體測試第三周之測試多個輸入的合法性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.