实验五、单元测试
一、实验目的
1)掌握单元测试的方法
2) 学习XUnit测试原理及框架;
3)掌握使用测试框架进行单元测试的方法和过程。
二、实验内容与要求
1、了解单元测试的原理与框架
1.1 单元测试原理
单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体含义,如C语言中单元指一个函数,Java里单元指一个类,图形化的软件中可以指一个窗口或一个菜单等。总的来说,单元就是人为规定的最小的被测功能模块。单元测试是在软件开发过程中要进行的最低级别的测试活动,软件的独立单元将在与程序的其他部分相隔离的情况下进行测试。单元测试是由程序员自己来完成,最终受益的也是程序员自己。可以这么说,程序员有责任编写功能代码,同时也就有责任为自己的代码编写单元测试。执行单元测试,就是为了证明这段代码的行为和我们期望的一致。
单元测试的内容包括
模块接口测试、局部数据结构测试、路径测试、错误处理测试、边界测试
(1)模块接口测试
模块接口测试是单元测试的基础。只有在数据能正确流入、流出模块的前提下,其他测试才有意义。模块接口测试也是集成测试的重点,这里进行的测试主要是为后面打好基础。测试接口正确与否应该考虑下列因素:
-输入的实际参数与形式参数的个数是否相同
-输入的实际参数与形式参数的属性是否匹配
-输入的实际参数与形式参数的量纲是否一致
-调用其他模块时所给实际参数的个数是否与被调模块的形参个数相同;
-调用其他模块时所给实际参数的属性是否与被调模块的形参属性匹配;
-调用其他模块时所给实际参数的量纲是否与被调模块的形参量纲一致;
-调用预定义函数时所用参数的个数、属性和次序是否正确;
-是否存在与当前入口点无关的参数引用;
-是否修改了只读型参数;
-对全程变量的定义各模块是否一致;
-是否把某些约束作为参数传递。
如果模块功能包括外部输入输出,还应该考虑下列因素:
-文件属性是否正确;
-OPEN/CLOSE语句是否正确;
-格式说明与输入输出语句是否匹配;
-缓冲区大小与记录长度是否匹配;
-文件使用前是否已经打开;
-是否处理了文件尾;
-是否处理了输入/输出错误;
-输出信息中是否有文字性错误。
-局部数据结构测试;
-边界条件测试;
-模块中所有独立执行通路测试;
(2)局部数据结构测试
检查局部数据结构是为了保证临时存储在模块内的数据在程序执行过程中完整、正确,局部功能是整个功能运行的基础。重点是一些函数是否正确执行,内部是否运行正确。局部数据结构往往是错误的根源,应仔细设计测试用例,力求发现下面几类错误:
-不合适或不相容的类型说明;
-变量无初值;
-变量初始化或省缺值有错;
-不正确的变量名(拼错或不正确地截断);
-出现上溢、下溢和地址异常。
(3)边界条件测试
边界条件测试是单元测试中最重要的一项任务。众所周知,软件经常在边界上失效,采用边界值分析技术,针对边界值及其左、右设计测试用例,很有可能发现新的错误。边界条件测试是一项基础测试,也是后面系统测试中的功能测试的重点,边界测试执行的较好,可以大大提高程序健壮性。
(4)独立路径测试
在模块中应对每一条独立执行路径进行测试,单元测试的基本任务是保证模块中每条语句至少执行一次。测试目的主要是为了发现因错误计算、不正确的比较和不适当的控制流造成的错误。具体做法就是程序员逐条调试语句。常见的错误包括:
-误解或用错了算符优先级;
-混合类型运算;
-变量初值错;
-精度不够;
-表达式符号错。
(5)错误处理测试
检查模块的错误处理功能是否包含有错误或缺陷。例如,是否拒绝不合理的输入;出错的描述是否难以理解、是否对错误定位有误、是否出错原因报告有误、是否对错误条件的处理不正确;在对错误处理之前错误条件是否已经引起系统的干预等。
通常单元测试在编码阶段进行。在源程序代码编制完成,经过评审和验证,确认没有语法错误之后,就开始进行单元测试的测试用例设计。利用设计文档,设计可以验证程序功能、找出程序错误的多个测试用例。对于每一组输入,应有预期的正确结果。
1.2 测试框架
xUnit是各种代码驱动测试框架的统称,这些框架可以测试 软件的不同内容(单元),比如函数和类。xUnit框架的主要优点是,它提供了一个自动化测试的解决方案。可以避免多次编写重复的测试代码。
底层是xUnit的framwork,xUnit的类库,提供了对外的功能方法、工具类、api等
TestCase(具体的测试用例)去使用framwork
TestCase执行后会有TestResult
使用TestSuite控制TestCase的组合
TestRunner执行器,负责执行case
TestListener过程监听,监听case成功失败以及数据结果,输出到结果报告中。
Unit测试框架包括四个要素:
(1)测试目标(对象)
一组认定被测对象或被测程序单元测试成功的预定条件或预期结果的设定。Fixture就是被测试的目标,可以是一个函数、一组对象或一个对象。 测试人员在测试前应了解被测试的对象的功能或行为。
(2)测试集
测试集是一组测试用例,这些测试用例要求有相同的测试Fixture,以保证这些测试不会出现管理上的混乱。
(3)测试执行
单个单元测试的执行可以按下面的方式进行:
第一步 编写 setUp() 函数,目的是:建立针对被测试单元的独立测试环境;举个例子,这可能包含创建临时或代理的数据库、目录,再或者启动一个服务器进程。
第二步 编写所有测试用例的测试体或者测试程序;
第三步 编写tearDown()函数,目的是:无论测试成功还是失败,都将环境进行清理,以免影响后续的测试;
(4)断言
断言实际上就是验证被测程序在测试中的行为或状态的一个函数或者宏。断言的失败会引发异常,终止测试的执行。
1.3 面向特定语言的,基于xUnit框架的自动化测试框架
Junit : 主要测试用Java语言编写的代码
CPPunit:主要测试用C++语言编写的代码
unittest , PyUnit:主要测试用python语言编写的代码
MiniUnit: 主要用于测试C语言编写的代码
三、实验过程
本次实验,我使用的编程语言是Java,编程软件是eclise,插件是Junit。
1源码:
package GameOfLife; import java.awt.*; import javax.swing.*; public class World extends JPanel implements Runnable{/*此部分java方法用于遵守生命游戏的规则*/ private final int rows;/*行*/ private final int columns; private final CellStatus[][] generation1;/*用于迭代的几个数组*/ private final CellStatus[][] generation2; private CellStatus[][] currentGeneration; private CellStatus[][] nextGeneration; private volatile boolean isChanging = false; public World(int rows, int columns) { this.rows=rows; this.columns=columns; generation1=new CellStatus[rows][columns]; for(int i=0;i<rows;i++) { for(int j=0;j<columns;j++) { generation1[i][j]=CellStatus.Dead;/*首先,将所有的点定义成死亡点*/ } } generation2=new CellStatus[rows][columns]; for(int i=0;i<rows;i++) { for(int j=0;j<columns;j++) { generation2[i][j]=CellStatus.Dead; } } currentGeneration=generation1; nextGeneration=generation2; } public void run() { while(true) { synchronized(this) { while(isChanging) { try { this.wait(); }catch(InterruptedException e) { e.printStackTrace(); } } repaint(); sleep(1);/*定义睡眠时间*/ for(int i=0;i<rows;i++) { for(int j=0;j<columns;j++) { evolve(i,j); } } CellStatus[][]temp=null; temp=currentGeneration; currentGeneration=nextGeneration;/*迭代结果*/ nextGeneration=temp; for(int i=0;i<rows;i++) { for(int j=0;j<columns;j++) { nextGeneration[i][j]=CellStatus.Dead; } } } } } public void paintComponent(Graphics g)/*根据矩阵的值进行绘图*/ { super.paintComponent(g); for(int i=0;i<rows;i++) { for(int j=0;j<columns;j++) { if(currentGeneration[i][j]==CellStatus.Active) { g.fillRect(j*20, i*20, 20, 20);/*若迭代位置对应为 active则填充为黑色*/ } else { g.drawRect(j*20, i*20, 20, 20);/*死亡部分用矩形边框*/ } } } } public void setArrow() { setShape(arrow); } public void setSquare() { setShape(square); } public void setwd() { setShape(wd); } private void setShape(int [][]shape) { isChanging=true; int arrowsRows=shape.length; int arrowsColumns=shape[0].length; int minimumRows=(arrowsRows<rows)?arrowsRows:rows; int minimunColumns=(arrowsColumns<columns)?arrowsColumns:columns; synchronized(this) { for(int i=0;i<rows;i++) { for(int j=0;j<columns;j++) { currentGeneration[i][j]=CellStatus.Dead; } } for(int i=0;i<minimumRows;i++) { for(int j=0;j<minimunColumns;j++) { if(shape[i][j]==1) { currentGeneration[i][j]=CellStatus.Active; } } } isChanging=false; this.notifyAll(); } } public void evolve(int x,int y)/*衍化的具体规则实现*/ { int activeSurroundingCell=0; if(isVaildCell(x-1,y-1)&&(currentGeneration[x-1][y-1]==CellStatus.Active)) activeSurroundingCell++; if(isVaildCell(x,y-1)&&(currentGeneration[x][y-1]==CellStatus.Active)) activeSurroundingCell++; if(isVaildCell(x+1,y-1)&&(currentGeneration[x+1][y-1]==CellStatus.Active)) activeSurroundingCell++; if(isVaildCell(x+1,y)&&(currentGeneration[x+1][y]==CellStatus.Active)) activeSurroundingCell++; if(isVaildCell(x+1,y+1)&&(currentGeneration[x+1][y+1]==CellStatus.Active)) activeSurroundingCell++; if(isVaildCell(x,y+1)&&(currentGeneration[x][y+1]==CellStatus.Active)) activeSurroundingCell++; if(isVaildCell(x-1,y+1)&&(currentGeneration[x-1][y+1]==CellStatus.Active)) activeSurroundingCell++; if(isVaildCell(x-1,y)&&(currentGeneration[x-1][y]==CellStatus.Active)) activeSurroundingCell++; // if(activeSurroundingCell==3) { nextGeneration[x][y]=CellStatus.Active; } else if(activeSurroundingCell==2) { nextGeneration[x][y]=currentGeneration[x][y]; } else { nextGeneration[x][y]=CellStatus.Dead; } } private boolean isVaildCell(int x,int y) { if((x>=0)&&(x<rows)&&(y>=0)&&(y<columns)) { return true; } else { return false; } } private void sleep(int x) { try { Thread.sleep(1000*x); } catch (InterruptedException e) { e.printStackTrace(); } } static enum CellStatus { Active, Dead; } private static final int[][] arrow = {//40*50 自定义的图案 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} }; } package GameOfLife; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LifeGame extends JFrame{/*界面设计*/ private final World world; public LifeGame(int rows,int columns) /*创建线程,游戏开始*/ { world=new World(rows, columns); new Thread(world).start(); add(world); } public static void main(String[]args) { LifeGame frame=new LifeGame(40, 50); JMenuBar menu=new JMenuBar(); frame.setJMenuBar(menu); JMenu options =new JMenu("选择图形");/*从自定义的图案中选择一个进行游戏*/ menu.add(options);/*菜单主界面添加options*/ JMenuItem arrow=options.add("Arrow"); arrow.addActionListener(frame.new ArrowActionListener());/*自定义图案,放到options中*/ JMenuItem square=options.add("Square"); square.addActionListener(frame.new SquareActionListener()); JMenuItem wd=options.add("wd"); wd.addActionListener(frame.new wdActionListener()); JMenu help=new JMenu("Help"); menu.add(help); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(1007,859); frame.setTitle("生命游戏"); frame.setVisible(true); frame.setVisible(true); frame.setResizable(false);/*界面设计的参数 从上到下依次为 将窗口设置为中央, 添加关闭选项,设置窗口大小,标题 设置刷新时不闪烁 设置为不可自由调整大小 */ } class ArrowActionListener implements ActionListener { /*arrow继承侦听器*/ public void actionPerformed(ActionEvent e) { world.setArrow(); } } class SquareActionListener implements ActionListener /*square继承侦听器*/ { public void actionPerformed(ActionEvent e) { world.setSquare(); } } class wdActionListener implements ActionListener /*wd继承侦听器*/ { public void actionPerformed(ActionEvent e) { world.setwd(); } } }
- 测试用例设计:
测试目标:测试row与columns在(30,50)与(1000,1200)之间能否工作
实验结果:测试通过率100%
(2)lifegame测试
- 错误处理测试
出现JUnit报initializationError错误
原因:@Test
public void 。。。。(){}
如果括号内有参数,有一定可能会报这种错误
解决办法:取出扩号内参数
- 选择的测试框架介绍、安装过程
选择Libraries,点击Add variable按钮,_4.11.0.v201303080030,结果如下图:
4.
测试代码:
(1):实验代码:
package GameOfLife; import static org.junit.Assert.*; import java.awt.Graphics; import javax.swing.JPanel; import org.junit.Before; import org.junit.Test; import GameOfLife.World.CellStatus; public class WorldTest { @Before public void setUp() throws Exception { } @Test public void testWorld() {int columns=30;int rows=50; final CellStatus[][] generation1;/*用于迭代的几个数组*/ final CellStatus[][] generation2; generation1=new CellStatus[rows][columns]; for(int i=0;i<rows;i++) { for(int j=0;j<columns;j++) { generation1[i][j]=CellStatus.Dead;/*首先,将所有的点定义成死亡点*/ } } generation2=new CellStatus[rows][columns]; for(int i=0;i<rows;i++) { for(int j=0;j<columns;j++) { generation2[i][j]=CellStatus.Dead; } } CellStatus[][] currentGeneration; CellStatus[][] nextGeneration; currentGeneration=generation1; nextGeneration=generation2; } }
(2)
@Test public void testLifeGame() { World world; int rows=30, columns=50; world=new World(rows, columns); new Thread(world).start(); add(world); } private void add(World world) { // TODO Auto-generated method stub } @Test public void testMain() { LifeGame frame=new LifeGame(30, 60); }
(3)
:public void testWorld(int columns,int rows) { final CellStatus[][] generation1;/*用于迭代的几个数组*/ final CellStatus[][] generation2; generation1=new CellStatus[rows][columns]; for(int i=0;i<rows;i++) { for(int j=0;j<columns;j++) { generation1[i][j]=CellStatus.Dead;/*首先,将所有的点定义成死亡点*/ } } generation2=new CellStatus[rows][columns]; for(int i=0;i<rows;i++) { for(int j=0;j<columns;j++) { generation2[i][j]=CellStatus.Dead; } } CellStatus[][] currentGeneration; CellStatus[][] nextGeneration; currentGeneration=generation1; nextGeneration=generation2; } @Test public void testRun() { fail("Not yet implemented"); } @Test public void testPaintComponentGraphics() { fail("Not yet implemented"); } @Test public void testSetArrow() { fail("Not yet implemented"); } @Test public void testSetSquare() { fail("Not yet implemented"); } @Test public void testSetwd() { fail("Not yet implemented"); } @Test public void testEvolve() { fail("Not yet implemented"); } }
- 测试结果与分析:
测试均通过,除在多组test类共同作用下,类括号里引入变量不能成功。
- 提交代码:
四.思考题:
比较以下二个工匠的做法,你认为哪种好?结合编码和单元测试,谈谈你的认识。
●工匠一:先拉上一根水平线,砌每一块砖时,都与这根水平线进行比较,使得每
块砖都保持水平。
●工匠二:先将一-排砖都砌完,然后拉上一根水平线,看看哪些砖有问题,再进行调
整。
答:两种方法都有可取之处,第一种方法在做之前就衡量好方向级精确度。适合目标明确的开发项目。耗费时间屋里人力明显较少。
第二种方式胜在当项目变化大,不明确的项目。可以及时调控。
五小结
本次实验主要是做了单元测试的工作,使用了一些单元测试的办法来查找程序可能出现的bug,而且经过这次测试,我明白了模块化设计的作用,不仅仅是方便维护检查,也方便测试,更有利于程序的稳定。单元测试写的时候尽量把测试场景测试完全,比如必须做除法运算的时候要考虑到除数为零,测试的场景越是全面,代码的质量越高。