测试与分装5.1-5.2

由于上次实验5.1基本完成了阶段1(封装),阶段2(加减乘除简单测试),所以本次实验我们着重实现阶段3的参数检测,

 

测试用例

测试项目名称

 四则运算简单测试

测试人员

冯美欣(http://www.cnblogs.com/maxx/)

吴舒婷(http://www.cnblogs.com/wst-2015/)

 

 

编制日期

2015年5月8日

功能特性

 对这四则运算(参数)进行测试

黑盒测试:

操作数个数

输入类型

输入数据

预期输出

实际输出

测试状态

2

1正确的数值

 (4,4,+)

8

 8

 正确

3

2带非法字符的数值

 (2,32,3w,-,+)

 出现异常

 出现异常

 正确

 2

3加减乘除

 (16,8,/)

 2

 2

 正确

2

4非法运算符

 (2,4,g)

 非法字符

 非法字符

 正确

3

5除法分母为0

 (3,1,0,-,/)

 出现提示

 除数不能为0,式子无效

 正确

白盒测试:

操作数个数

测试用例

用例说明

覆盖的代码

测试结果

3

(2,1,1,-,+)

 数值的输入

  Input(15-49)               

运行成功无故障

1

 

操作数的个数

  Input(50-76)

运行成功

 2

(3,5,w)

非法运算符

 Input(92-117)

能判断非法字符

3

(27,3,0)

除数为0

Input(77-91)

能运行测试,测试停止

5

(4,5,1,1,2,-,*+,-)

 是否运行算法

 CalTest (Cal)

运行成功无故障

 4

 (2,3,4,3,+,-,*)  用户输入是否有效  CalTest(judge)  运行成功无故障

代码覆盖率

100%      
package 片段;
public class MainTest {
    static String[] astr=new String[10];
    static String[] oper=new String[5];
     static float[] a=new float[10];
    public static void main(String[] args) {
        CalTest t1=new CalTest();
        
      
        Input.input(astr,oper,a);//操作数字符数组、运算符字符数组、操作数转float的数组
        Input.formula(a,oper);//显示出式子
        Input.CherkZero(oper, astr);
        t1.judge(a,oper);//判断
    }
}
MainTest
package 片段;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Scanner;




public class Input {    
    static Scanner in=new Scanner(System.in);
    static DecimalFormat decimal = new DecimalFormat("#.##");
    static int num;
    
    static void input(String astr1[],String oper1[],float a[])//输入数据
    {
        System.out.println("选择多少个操作数:(2~5)");
        try
        {
            boolean value=false;
            while(value==false){
                num=in.nextInt();//操作数
                if(num<2||num>5)
                    System.out.println("	超出范围!!请重新输入:");
                else value=true;
            }
        }    
        catch(Exception e){
            System.out.println("输入不合符合要求!!");
        }
        
        //输入操作数
        for(int i=0;i<num;i++)
        {
            
            System.out.println("第"+(i+1)+"个操作数:");
            astr1[i]=in.next();
            a[i]=checknum(astr1[i]);    
        }
        
        //输入运算符
        for(int i=0;i<num-1;i++)
        {
            
            System.out.println("第"+(i+1)+"个运算符:");
            oper1[i]=in.next();
            oper1[i]=checkoper(astr1[i+1],oper1[i]);
        }
    }
    
    static float checknum(String astr1)//检查操作数
    {
         float a = 0;
        for(int i=0;i<num;i++)
        {
            boolean value=false;
            while (value==false){
            try {
                
             a=Float.parseFloat(astr1); //可以转换为整型;
             if(a>=-100&&a<=100) 
                  value=true;
             else{
                 System.out.println("操作符不在运算范围内!!重新输入");
                 astr1=in.next();
             }
            } catch (Exception g) {
               System.out.println("操作数错误,重新输入:");   
               astr1=in.next();
               
                 
             }
        }    
        }
        return a;
    }
    static void CherkZero(String oper1[],String a1[])
    {
        for(int i=0;i<Input.num-1;i++){
            if(oper1[i].equals("/"))
            {
                float atest=Float.parseFloat(a1[i+1]);
                if(atest==0)
                {
        
                    System.out.println("
除数不能为0,式子无效");
                    System.exit(0);
                }
            }
        }
    }
    static String checkoper(String a1,String oper1)//检查运算符
    {
        //检查运算符
        for(int i=0;i<num-1;i++)
        {
            boolean value=false;
            while(value==false){
                if( oper1.equals("+")|| oper1.equals("-")
                ||oper1.equals("*")||oper1.equals("/"))
                {
                    value=true;
                    
                }
                else
                
               {
                    System.out.println("输入的运算符不合法");
                    oper1=in.next();
                    
                    
               }
                
            }
        }
        return oper1;
    }
    
    static void formula(float a1[],String oper1[])//显示出式子方程式
    {
        String s1=new String();
        
        String S=new String();
        System.out.print("输入的式子为:");
        for(int i=0;i<num;i++){
            if(i<(num-1))
            {
                if(a1[i]<0)
                    s1=String.valueOf("("+a1[i]+")");
                else
                    s1=String.valueOf(a1[i]);
                S=S+s1+oper1[i];
            }    
            else
            {
                if(a1[i]<0)
                    s1=String.valueOf("("+a1[i]+")");
                else
                    s1=String.valueOf(a1[i]);
                S=S+s1;
            }
        }
        System.out.print(S+"=");
    }
}
Input
package 片段;

import java.text.DecimalFormat;
import java.util.Scanner;

public class CalTest {
    static Scanner in=new Scanner(System.in);
    static DecimalFormat decimal = new DecimalFormat("#.##");
    public static void main(String[] args) {
        // TODO 自动生成的方法存根

    }

    static void judge(float a[],String oper[])//判断
    {
        System.out.println("
请输入你的答案~~");
        String userans=in.next();
        float user=solvesuerans(userans);    
         
        
        float staticanswer=Cal(a,oper);
        
        if( user==staticanswer)
            System.out.println("回答正确!");
        else
            System.out.println("回答错误!答案为:"+staticanswer);
    }

    static float solvesuerans(String userans)//处理用户输入的答案,return 用户输入的答案
    {
        float cs[]=new float[2];
        float user = 0;
         try {
             user=Float.parseFloat(userans);
           } 
          catch (Exception g) {
             
              String[] ss = new String[2];
                ss = userans.split("/");
                try{
                cs[0] = Float.valueOf(ss[0]);
                cs[1] = Float.valueOf(ss[1]);
                //System.out.println(cs[0]+"    "+cs[1]);
                user =  cs[0] / cs[1];
                //System.out.println(answer);
                String str=decimal.format(user);
                 user=Float.parseFloat(str);     
                }catch(Exception e)
                {
                    System.out.println("异常!!输入的答案无效!!");
                }
        }
        
        return user;
    }

    static float Cal(float a[],String oper[]){  //计算出结果,return出一个正确答案
    int sign; // 累加运算时的符号
    float left, right;// 保存蹭结果
    float answer;
    // 计算标准答案
    left = 0;
    right =a[0];
    sign = 1;
    
    for (int g = 0; g <Input. num - 1; g++) {
        if(oper[g].equals("+")){
            left = left + sign * right;
            sign = 1;
            right = a[g + 1];
            
        }
        else if(oper[g].equals("-")){
            left = left + sign * right;
            sign = -1;
            right = a[g + 1];
        }
        else if(oper[g].equals("*")){
            right = right * a[g + 1];
        }
        else if(oper[g].equals("/")){
            right = right / a[g + 1];
        }
        
    }
    String staticanser = decimal.format(left + sign* right);
    
    answer=Float.parseFloat(staticanser); //可以转换为整型;
    
    return answer;
    }

}
CalTest

 以上是主程序代码。

 测试代码

package 片段;

import static org.junit.Assert.*;

import org.junit.Test;

public class InputTest {

    @Test
    public void testInput() {
        Input i= new Input();
        i.input(MainTest.astr,MainTest.oper,MainTest.a);
    }
    
    
    @Test
    public void testformulaCherkZero() {
        Input i= new Input();
        i.CherkZero(MainTest.oper, MainTest.astr);
    }
    
    @Test
    public void testformula() {
        Input i= new Input();
        i.formula(MainTest.a,MainTest.oper);
    }
        
    @Test
    public void testjudge() {
        CalTest c1=new CalTest();
        c1.judge(MainTest.a, MainTest.oper);
    }

    
}
InputTest

为何会出现测试的顺序不对呢

当除数为0时,结束程序了,测试结束为灰色,合理吗?

由于有急事,没能完善测试用例,今晚回来后,一定会好好完善,望老师海涵!

原文地址:https://www.cnblogs.com/wst-2015/p/4488034.html