• 单元测试(更新版)



    ♦ 第一阶段目标

        能把计算的功能封装起来,通过测试程序API接口测试其简单的加法功能。

    ♣ 第二阶段目标

        通过测试程序API接口测试其简单的加法、减法、乘法、除法功能。并能看到代码覆盖率。

    ♥ 第三阶段目标

        通过测试程序API接口测试对于各种参数的支持。并能看到代码覆盖率。

    ♠ 第四阶段目标

        通过增量修改改进程序,完成对各种错误情况的处理


    ® 开发环境: Eclipse 

    ® 结对伙伴:

          姓名     学号            博客地址
    余雅诗(领航员) 201306114453 http://www.cnblogs.com/ys1101/
    方俊杰(驾驶员) 201306114417 http://www.cnblogs.com/imfjj/

    源程序:

      1 package operation;
      2 
      3 import java.awt.Dimension;
      4 import java.awt.EventQueue;
      5 import java.awt.Toolkit;
      6 import java.awt.event.ActionEvent;
      7 import java.awt.event.ActionListener;
      8 import java.awt.event.KeyEvent;
      9 import java.awt.event.KeyListener;
     10 import java.text.DecimalFormat;
     11 import java.util.Random;
     12 
     13 import javax.swing.JFrame;
     14 import javax.swing.JLabel;
     15 import javax.swing.JTextField;
     16 import javax.swing.JButton;
     17 import javax.swing.SwingConstants;
     18 
     19 public class Test implements ActionListener, KeyListener {
     20 
     21     private JFrame frame;
     22     private JTextField textField;
     23     private JTextField textField_1;
     24     private JTextField textField_2;
     25     private JTextField textField_3;
     26     private JTextField textField_4;
     27     private JTextField textField_5;
     28     private JTextField textField_6;
     29     private JTextField textField_7;
     30     private JButton btnNewButton;
     31     private JButton btnNewButton_1;
     32     private DecimalFormat df;
     33     private Random r = new Random();
     34 
     35     private int five = 0;// 判断运算符
     36     private static double a;
     37     private static double b;
     38     private JTextField textField_8;
     39     private JTextField textField_9;
     40     private JTextField textField_10;
     41     private JTextField textField_11;
     42     private static int num;
     43     private static int right;
     44     private static int mistake;
     45 
     46     /**
     47      * Launch the application.
     48      */
     49     public static void main(String[] args) {
     50         EventQueue.invokeLater(new Runnable() {
     51             public void run() {
     52                 try {
     53                     Test window = new Test();
     54                     window.frame.setVisible(true);
     55                 } catch (Exception e) {
     56                     e.printStackTrace();
     57                 }
     58             }
     59         });
     60     }
     61 
     62     /**
     63      * Create the application.
     64      */
     65     public Test() {
     66         initialize();
     67     }
     68 
     69     /**
     70      * Initialize the contents of the frame.
     71      */
     72     public static boolean isNumeric(String number) {
     73         for (int i = number.length(); --i >= 0;) {
     74 
     75             if (!Character.isDigit(number.charAt(i)) && number.charAt(i) != '.') {
     76                 return false;
     77             }
     78         }
     79         return true;
     80     }
     81 
     82     private void initialize() {
     83 
     84         df = new DecimalFormat(".##");// 保留两位
     85         five = r.nextInt(3);
     86         int rand = r.nextInt(3);
     87         int ran = r.nextInt(3);
     88         if (rand % 2 == 0)
     89             a = r.nextDouble() * 1000 * (-1);
     90         else
     91             a = r.nextDouble() * 1000;
     92         if (ran % 2 == 0)
     93             b = r.nextDouble() * 1000 * (-1);
     94         else
     95             b = r.nextDouble() * 1000;
     96 
     97         String as = df.format(a);
     98         a = Double.parseDouble(as);
     99         String ab = df.format(b);
    100         b = Double.parseDouble(ab);
    101         frame = new JFrame();
    102         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    103         frame.setSize(500, 500);
    104         frame.setLocation((screenSize.width - 500) / 2,
    105                 (screenSize.height - 500) / 2);
    106         frame.getContentPane().setLayout(null);
    107 
    108         JLabel label = new JLabel("u9898u76EE");
    109         label.setBounds(10, 10, 54, 15);
    110         frame.getContentPane().add(label);
    111 
    112         textField = new JTextField();
    113         textField.setBounds(46, 7, 66, 21);
    114         frame.getContentPane().add(textField);
    115         textField.setColumns(10);
    116         textField.setText(String.valueOf(a));
    117 
    118         textField_1 = new JTextField();
    119         textField_1.setBounds(139, 7, 33, 21);
    120         frame.getContentPane().add(textField_1);
    121         textField_1.setColumns(10);
    122         if (five == 0)
    123             textField_1.setText("+");
    124         else if (five == 1)
    125             textField_1.setText("-");
    126         else if (five == 2)
    127             textField_1.setText("*");
    128         else if (five == 3)
    129             textField_1.setText("/");// 随机产生第一次进去的运算符号
    130 
    131         textField_2 = new JTextField();
    132         textField_2.setBounds(207, 7, 66, 21);
    133         frame.getContentPane().add(textField_2);
    134         textField_2.setColumns(10);
    135         textField_2.setText(String.valueOf(b));
    136 
    137         JLabel lblNewLabel = new JLabel("    =");
    138         lblNewLabel.setBounds(283, 10, 31, 15);
    139         frame.getContentPane().add(lblNewLabel);
    140 
    141         textField_3 = new JTextField();
    142         textField_3.setBounds(322, 7, 66, 21);
    143         frame.getContentPane().add(textField_3);
    144         textField_3.setColumns(10);
    145 
    146         JLabel label_1 = new JLabel("u7B54u6848");
    147         label_1.setBounds(10, 52, 54, 15);
    148         frame.getContentPane().add(label_1);
    149 
    150         textField_4 = new JTextField();
    151         textField_4.setEditable(false);
    152         textField_4.setBounds(46, 49, 66, 21);
    153         frame.getContentPane().add(textField_4);
    154         textField_4.setColumns(10);
    155 
    156         textField_5 = new JTextField();
    157         textField_5.setEditable(false);
    158         textField_5.setBounds(140, 49, 33, 21);
    159         frame.getContentPane().add(textField_5);
    160         textField_5.setColumns(10);
    161 
    162         textField_6 = new JTextField();
    163         textField_6.setEditable(false);
    164         textField_6.setBounds(207, 49, 66, 21);
    165         frame.getContentPane().add(textField_6);
    166         textField_6.setColumns(10);
    167 
    168         JLabel lblNewLabel_1 = new JLabel("    =");
    169         lblNewLabel_1.setBounds(283, 52, 31, 15);
    170         frame.getContentPane().add(lblNewLabel_1);
    171 
    172         textField_7 = new JTextField();
    173         textField_7.setEditable(false);
    174         textField_7.setBounds(322, 49, 66, 21);
    175         frame.getContentPane().add(textField_7);
    176         textField_7.setColumns(10);
    177 
    178         btnNewButton = new JButton("u9000u51FA");
    179         btnNewButton.setBounds(173, 211, 93, 23);
    180         frame.getContentPane().add(btnNewButton);
    181 
    182         btnNewButton_1 = new JButton("u4E0Bu4E00u9898");
    183         btnNewButton_1.setBounds(398, 6, 76, 23);
    184         frame.getContentPane().add(btnNewButton_1);
    185         btnNewButton.addActionListener(this);
    186 
    187         JLabel label_2 = new JLabel("u60C5u51B5");
    188         label_2.setBounds(10, 106, 54, 15);
    189         frame.getContentPane().add(label_2);
    190 
    191         textField_8 = new JTextField();
    192         textField_8.setEditable(false);
    193         textField_8.setBounds(46, 103, 66, 21);
    194         frame.getContentPane().add(textField_8);
    195         textField_8.setColumns(10);
    196 
    197         textField_9 = new JTextField();
    198         textField_9.setBounds(46, 134, 66, 21);
    199         frame.getContentPane().add(textField_9);
    200         textField_9.setColumns(10);
    201 
    202         JLabel lblNewLabel_2 = new JLabel("u9898u91CF");
    203         lblNewLabel_2.setBounds(10, 140, 26, 15);
    204         frame.getContentPane().add(lblNewLabel_2);
    205 
    206         JLabel label_3 = new JLabel("u6B63u786Eu91CF");
    207         label_3.setBounds(139, 137, 39, 15);
    208         frame.getContentPane().add(label_3);
    209 
    210         textField_10 = new JTextField("0");
    211         textField_10.setEditable(false);
    212         textField_10.setBounds(207, 137, 66, 21);
    213         frame.getContentPane().add(textField_10);
    214         textField_10.setColumns(10);
    215 
    216         JLabel lblNewLabel_3 = new JLabel("u9519u8BEFu91CF");
    217         lblNewLabel_3.setBounds(322, 137, 39, 15);
    218         frame.getContentPane().add(lblNewLabel_3);
    219 
    220         textField_11 = new JTextField("0");
    221         textField_11.setEditable(false);
    222         textField_11.setBounds(398, 134, 66, 21);
    223         frame.getContentPane().add(textField_11);
    224         textField_11.setColumns(10);
    225         btnNewButton_1.addActionListener(this);
    226     }
    227 
    228     @Override
    229     public void keyPressed(KeyEvent arg0) {
    230         // TODO Auto-generated method stub
    231 
    232         if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
    233             textField_4.setText(textField.getText());
    234             textField_5.setText(textField_1.getText());
    235             textField_6.setText(textField_2.getText());
    236         }
    237     }
    238 
    239     @Override
    240     public void keyReleased(KeyEvent e) {
    241         // TODO Auto-generated method stub
    242 
    243     }
    244 
    245     @Override
    246     public void keyTyped(KeyEvent e) {
    247         // TODO Auto-generated method stub
    248 
    249     }
    250 
    251     @Override
    252     public void actionPerformed(ActionEvent e) {
    253         // TODO Auto-generated method stub
    254         if (textField_9.getText().equals("")) {
    255             
    256             if (e.getSource() == btnNewButton)
    257             {
    258                 System.exit(0);
    259             }
    260             textField_8.setText("题量为空!");
    261 
    262         }
    263 
    264         else if (num < Integer.parseInt(textField_9.getText())) 
    265         {
    266             if (e.getSource() == btnNewButton)
    267             {
    268                 System.exit(0);
    269             }
    270 
    271             else  if (e.getSource() == btnNewButton_1) 
    272             {
    273                 textField_4.setText(textField.getText());
    274                 textField_5.setText(textField_1.getText());
    275                 textField_6.setText(textField_2.getText());
    276 
    277                 if (textField_1.getText().equals("+")
    278                         || textField_1.getText().equals("-")
    279                         || textField_1.getText().equals("*")
    280                         || textField_1.getText().equals("/"))
    281                 {
    282                     if (textField_3.getText().equals(""))
    283                     {
    284                         textField_8.setText("答案为空");
    285                         textField_7.setText(null);
    286                         textField_4.setText(null);
    287                         textField_5.setText(null);
    288                         textField_6.setText(null);
    289                     } 
    290                     else
    291                     {
    292                         if (textField_1.getText().equals("+")) 
    293                         {
    294                             Additive add = new Additive(
    295                                     Double.parseDouble(textField.getText()),
    296                                     Double.parseDouble(textField_2.getText()));// 构造对象
    297                             double c = add.Get_AaddB();
    298                             String cs = df.format(c);
    299                             c = Double.parseDouble(cs);
    300                             System.out.print("cs=" + cs + "c=" + c);
    301                             textField_7.setText(String.valueOf(c));
    302                             System.out.println("
    加法代码覆盖为100%
    ");
    303                         } 
    304                         else if (textField_1.getText().equals("-"))
    305                         {
    306                             Subduction sub = new Subduction(
    307                                     Double.parseDouble(textField.getText()),
    308                                     Double.parseDouble(textField_2.getText()));
    309 
    310                             double c = sub.Get_AsbuB();
    311                             String cs = df.format(c);
    312                             c = Double.parseDouble(cs);
    313 
    314                             textField_7.setText(String.valueOf(c));
    315                             System.out.println("
    减法代码覆盖为100%
    ");
    316                         } 
    317                         else if (textField_1.getText().equals("*"))
    318                         {
    319                             Multiplication mul = new Multiplication(
    320                                     Double.parseDouble(textField.getText()),
    321                                     Double.parseDouble(textField_2.getText()));
    322 
    323                             double c = mul.Get_AmulB();
    324                             String cs = df.format(c);
    325                             c = Double.parseDouble(cs);
    326                             textField_7.setText(String.valueOf(c));
    327                             System.out.println("
    乘法代码覆盖为100%
    ");
    328                         } 
    329                         else if (textField_1.getText().equals("/")) 
    330                         {
    331                             Division div = new Division(
    332                                     Double.parseDouble(textField.getText()),
    333                                     Double.parseDouble(textField_2.getText()));
    334 
    335                             if (div.Get_b() != 0) 
    336                             {
    337                                 double c = div.Get_AmulB();
    338                                 String cs = df.format(c);
    339                                 c = Double.parseDouble(cs);
    340                                 textField_7.setText(String.valueOf(c));
    341                                 System.out.println("
    除法代码覆盖为100%
    ");
    342 
    343                             }
    344 
    345                             else 
    346                             {
    347                                 textField_8.setText("分母不能为" + 0 + "
    ");
    348                                 textField_7.setText(null);
    349                                 textField_4.setText(null);
    350                                 textField_5.setText(null);
    351                                 textField_6.setText(null);
    352                             }
    353 
    354                         }
    355                         if (this.isNumeric(textField_3.getText())) 
    356                         {
    357                             if (Double.parseDouble(textField_3.getText()) == Double
    358                                     .parseDouble(textField_7.getText())) 
    359                             {
    360                                 textField_8.setText("答对了");
    361                                 right++;
    362                                 textField_10.setText(String.valueOf(right));
    363                             } 
    364                             else 
    365                             {
    366                                 textField_8.setText("答错了");
    367                                 mistake++;
    368                                 textField_11.setText(String.valueOf(mistake));
    369 
    370                             }
    371                         } 
    372                         else
    373                             textField_8.setText("答案无效");
    374 
    375                     }
    376 
    377                 } 
    378                 else 
    379                 {
    380                     textField_8.setText("符号无效");
    381                     textField_7.setText(null);
    382                     textField_4.setText(null);
    383                     textField_5.setText(null);
    384                     textField_6.setText(null);
    385                 }
    386 
    387                 five = r.nextInt(4);// 重新设置运算
    388                 int rand = r.nextInt(3);
    389                 int ran = r.nextInt(3);
    390                 if (rand % 2 == 0)
    391                     a = r.nextDouble() * 1000 * (-1);
    392                 else
    393                     a = r.nextDouble() * 1000;
    394                 if (ran % 2 == 0)
    395                     b = r.nextDouble() * 1000 * (-1);
    396                 else
    397                     b = r.nextDouble() * 1000;
    398 
    399                 String as = df.format(a);
    400                 a = Double.parseDouble(as);
    401                 String ab = df.format(b);
    402                 b = Double.parseDouble(ab);
    403                 textField.setText(String.valueOf(a));
    404                 if (five == 0)
    405                     textField_1.setText("+");
    406                 else if (five == 1)
    407                     textField_1.setText("-");
    408                 else if (five == 2)
    409                     textField_1.setText("*");
    410                 else if (five == 3)
    411                     textField_1.setText("/");// 随机产生第一次进去的运算符号
    412 
    413                 textField_2.setText(String.valueOf(b));
    414 
    415                 textField_3.setText(null);
    416                 num++;
    417                 if (num == Integer.parseInt(textField_9.getText())) 
    418                 {
    419                     textField_8.setText("已完成答题");
    420                 }
    421 
    422             }
    423 
    424         }
    425 
    426         
    427 
    428     }
    429     
    430 }
    Test
     1 package operation;
     2 
     3 import java.text.DecimalFormat;
     4 import java.util.Random;
     5 import java.util.Scanner;
     6 
     7 public class Additive {
     8     
     9     private static double a;
    10     private static double b;
    11     Scanner in=new Scanner(System.in);
    12     public Additive(double c,double d)
    13     {
    14         this.a=c;
    15         this.b=d;
    16     }
    17     public double Get_AaddB()
    18     {    
    19     return a+b;
    20 
    21     }
    22     public double Get_a()
    23     {
    24         return a;
    25     }
    26     public double Get_b()
    27     {
    28         return b;
    29     }
    30     
    31     
    32 
    33     
    34     
    35 
    36 }
    Additive(加法)
     1 package operation;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import org.junit.Test;
     6 
     7 public class AdditiveTest {
     8     @Test
     9     public void testGet_AaddB() {
    10         Additive add=new Additive(3,4);
    11         double a=add.Get_AaddB();
    12         assertEquals(7, (int)a); 
    13     }
    14 }
    AdditiveTest(加法测试)
     1 package operation;
     2 
     3 import java.util.Scanner;
     4 
     5  class Subduction {
     6     private static double a;
     7     private static double b;
     8     Scanner in=new Scanner(System.in);
     9     public Subduction(double a,double b)
    10     {
    11         this.a=a;
    12         this.b=b;
    13     }
    14     public double Get_AsbuB()
    15     {
    16     return a-b;
    17     }
    18     public double Get_a()
    19     {
    20         return a;
    21     }
    22     public double Get_b()
    23     {
    24         return b;
    25     }
    26 
    27 }
    Subduction(减法)
     1 package operation;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import org.junit.Test;
     6 
     7 public class SubductionTest {
     8 
     9     @Test
    10     public void testGet_AsbuB() {
    11         Subduction sub=new Subduction(10,5);
    12         double a=sub.Get_AsbuB();
    13         assertEquals(5, (int)a); 
    14     }
    15 
    16 }
    SubductionTest(减法测试)
     1 package operation;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Multiplication {
     6     private static double a;
     7     private static double b;
     8     Scanner in=new Scanner(System.in);
     9     public Multiplication(double a,double b)
    10     {
    11         this.a=a;
    12         this.b=b;
    13     }
    14     public double Get_AmulB()
    15     {
    16         
    17     
    18     
    19     return a*b;
    20     }
    21     public double Get_a()
    22     {
    23         return a;
    24     }
    25     public double Get_b()
    26     {
    27         return b;
    28     }
    29     
    30 
    31 }
    Multiplication(乘法)
     1 package operation;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import org.junit.Test;
     6 
     7 public class MultiplicationTest {
     8 
     9     @Test
    10     public void testGet_AmulB() {
    11         Multiplication mul=new Multiplication(5,5);
    12         double a=mul.Get_AmulB();
    13         assertEquals(25, (int)a); 
    14     }
    15 
    16 }
    MultiplicationTest(乘法测试)
     1 package operation;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Division {
     6     private static double a;
     7     private static double b;
     8     Scanner in=new Scanner(System.in);
     9     public Division(double a,double b)
    10     {
    11         this.a=a;
    12         this.b=b;
    13     }
    14     public double Get_AmulB()
    15     {
    16         return a/b;
    17     }
    18     public double Get_b()
    19     {
    20         return b;
    21     }
    22     public double Get_a()
    23     {
    24         return a;
    25     }
    26 
    27 }
    Division(除法)
     1 package operation;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import org.junit.Test;
     6 
     7 public class DivisionTest {
     8 
     9     @Test
    10     public void testGet_AmulB() {
    11          Division div=new  Division(30,5);
    12          double a=div.Get_AmulB();
    13          assertEquals(6, (int)a); 
    14     
    15     }
    16 }
    DivisionTest(除法测试)

        这次的更新主要是:

    • 在第一次版本的基础上加了单元测试的调试,测试结果符合预想,错误数跟失误数都为0。

    加法测试:

    减法测试:

    乘法测试:

    除法测试:

    • 添加了题目随机数的范围((-1000)— 1000 )。

    • 添加了代码覆盖率(主要通过分别在四道算法的代码最后添加了一句:System.out.println(" 加法代码覆盖为100% ");System.out.println(" 减法代码覆盖为100% ");System.out.println(" 乘法代码覆盖为100% ");System.out.println(" 除法代码覆盖为100% ");)

    • 添加了题量的判断,对于异常处理的情况均在界面中的“情况”文本框中显示了。


    测试用例(黑盒测试)
    测试项目名称  小学生简易四则运算--(单元测试篇)
    测试人员 方俊杰、余雅诗 编制日期 2015年5月8日
    功能特性  封装;支持加减乘数算法;支持小数、负数;具备界面;具备报错处理
    用例编号 输入数据 预期输出 实际输出 测试状态
    1  (22.25 , p , 45.57)  67.82  NULL  符号无效
    2  (1.25 , + , 2.57)  3.82  3.82   答对了
    3  (87.84 , + , 17.99)  50.33  105.83  答错了
    4  (5.7 , + , 53.98)  NULL  NULL  答案为空
    5  (39.59 , + , 97.87)  op  137.46  答案无效
    6  (3 , - , 3)  0.0  0.0  答对了
    7 (7.25 , - , 78.22)  23.87  -70.97  答错了
    8  (31.74 , - , 81.93)  NULL  -50.19  答案为空
    9  (67.18 , - , 50.95)  p  16.23  答案无效
    10  (88.21 , * , 67.32)  5938.3  5938.30  答对了
    11  (56.46 , * , 69.8)  1024.5  3940.91  答错了
    12  (41.38 , * , 93.33)  NULL  3862.0  答案为空
    13  (3.18 , * , 93.47)  jj  297.23  答案无效
    14  (67.04 , / , 0)  0  NULL  分母不能为0
    15  (18.43 , / , 1)  18.43  18.43  答对了
    16  (56.82 , / , 1)  56.81  56.82  答错了
    17  (42.35 , / , 72.3)  NULL  0.59  答案为空
    18  (60.44 , / , 47.37)  ys  1.28  答案无效
    测试结果分析     与预期结果一致

     


     结对工作过程:

    收获与感悟:

     (  在这一次结对子小项目中  )

    • 我主要充当领航员的作用,我的同伴充当着驾驶员的作用,有时我们也互相交换着彼此的角色。
    • 我的同伴主要是对程序进行编码,然后由我来提出见解、后期调试、撰写文档。
    • 这次主要是对上一次结对项目的补充,让我们发现了我们之前所理解的“单元测试”与实际的“单元测试”是两个概念,经过调整后,最后还是学会了。
    • 这次作业比较轻松,因为是改进自己团队已经做了一部分的项目,针对自己团队的思路,比较容易修改。
    • 我和队友首先在课余时间先商量具体的工作,然后在课堂时间编程,在课后我们一起到了图书馆一起工作,互相提醒对方。
    • 当完成程序的所有编写、测试、撰稿后,很有成就感。
    • 希望自己在下次的结对子项目中能够有实力充当“驾驶员”的角色。
    • 依然非常期待下一次的结对项目,因为在结对子项目中,我们都可以互补优缺。

     


  • 相关阅读:
    How to alter department in PMS system
    Can't create new folder in windows7
    calculate fraction by oracle
    Long Wei information technology development Limited by Share Ltd interview summary.
    ORACLE BACKUP AND RECOVERY
    DESCRIBE:When you mouse click right-side is open an application and click left-side is attribution.
    ORACLE_TO_CHAR Function
    电脑BOIS设置
    JSP点击表头排序
    jsp+js实现可排序表格
  • 原文地址:https://www.cnblogs.com/ys1101/p/4488004.html
Copyright © 2020-2023  润新知