单元测试学习之二-JAVA的单元测试软件JUnit及覆盖度分析软件DJUnit

原文:http://cmmi9.blogbus.com/logs/2110514.html

 

1.       基于测试驱动开发(TDD)的思想,代码开发的方式有如下三种:一是先根据设计把框架写好,该留的接口留。然后再写测试代码,最后写开发代码。二是在编写代码之前,把所有的代码测试代码写好。三是先写完代码,后测试。个人推荐第一种方式。

2.       单元测试的方法为每个类建立对应的测试类,为每个函数(很简单的除外)建立相应的测试函数,建议主张以函作作为测试单元的单位。单元测试不应过分强调面向对象,因为局部的代码依然是结构化的。

3.       关于单元测试的覆盖度问题:首先选择测试的范围,范围的确定是要保证能找到Bug。然后建立相应的测试覆盖度要达到的最小百分比,以决定什么时候停止覆盖度分析。关于覆盖度,它可以分析哪些语句或是分支没有测试到,从而新增测试用例。目前使用了DJUnit的单元测试覆盖度分析。使用时,用RUN AS.. http://cmmi9.blogbus.com/files/1143002801.gif

4.       关于单元测试工具JUnit的使用

(一)JUnit的特征 

1.       使用断言方法判断期望值和实际值的差异,返回布尔值;

2.       测试驱动设备使用共同的初始化变量或方实例;

3.       支持图形交互模式和文本支持格式;

  (二)框架组成

        1.对测试目标进行测试的方法和过程集合,称为测试用例TestCase

        2.测试用例的集合,可容纳多个TestCase,称为测试包TestSuite

        3.测试结果的描述与记录TestResult

        4.测试过程中的事件监听者TestListener

        5.每个测试方法所发生的与预期不一致的状态的描述,称为测试失败元素TestFailure

  (三)抽象类

        1.TestCaseTest的接口的实现,构造函数TestCaseString name),由于每个TestCase在创建时都有一个名称,若某测试失败了,便可识别哪个测试失败了。

        2.SetUp()方法集中初始化测试所需的所有变量和实例,testDown()是每个测试方法结束后取消初始的方法。

        3.开发人员只须继承TestCase类,完成run方法即可,测试结果放在TestResult中。

   () 测试公约

        1.测试用例必须是公有类(public)

        2.测试用例必须继承TestCase

        3. 测试用例中的方法必须是public

        4. 测试方法必须声明为void

        5. 测试方法的前置名称必须为test

        6. 测试方法不许有任何参数传递

        7. 当新加入一个插件时,把插件文件考入到插件目录中,然后用eclipse –clean启动。Eclipse会自动检查所有的插件。

  (五)如何测试异常

        1.原程序会有抛出异常,或者有重新输入等方法(throw)

        2.对于这类异常流的测试方法有

          Try{

              使用异常数据调用源代码函数

              Fail(“失败”);            }

          Catch(Exception)  {}

()源代码与测试代码如何分在不同目录

   采用相同的包,不同的路径方式。如源代码用的是com.source包。而测试代码采用com.testJUnit会自动识别。

(七)JUnit的判断方法

     assertEquals(Object a,Object b);

 ()实例

 1.测试分支的实现

   源代码:

 

  1. public class Choose {
  2.        public Choose() {
  3.              super();
  4.               // TODO Auto-generated constructor stub
  5.        }
  6.        public String  c(int i) throws Exception{
  7.               int aaa=i;
  8.        if (aaa>0) {
  9.               return ">";
  10.        }
  11.        if (aaa<0) {
  12.               return "<";
  13.        }
  14.        if (aaa==0) {
  15.               return "=";
  16.        }
  17.        else 
  18.               return "null";
  19.        }}
  20. 测试代码:
  21. //判断测试覆盖率
  22. //判断测试分支
  23. import junit.framework.TestCase;
  24. public class TestChoose extends TestCase {
  25.     public Choose c;
  26.     public int i1,i2,i3;
  27.     String abc;
  28.     //测试中的三个值
  29.     String test;
  30.     //调用Choose类c方法后的返回值
  31.        
  32.        public static void main(String[] args) {
  33.        }
  34.        public TestChoose(String name) {
  35.               super(name);       
  36.        }
  37.        protected void setUp() throws Exception {
  38.               super.setUp();
  39.               this.c=new Choose();
  40.               i1=1;
  41.               i2=0;
  42.               i3=-1;
  43.               test=new String();
  44.               abc=new String("1");
  45.        }
  46.        public void testc() {
  47.               
  48.               try {
  49.                      test=c.c(i1);
  50.               assertEquals(">",test);
  51.               test=c.c(i2);
  52.               assertEquals("=",test);
  53.               test=c.c(i3);
  54.               assertEquals("<",test);
  55.                   //fail("没有异常");
  56.        }
  57.               catch(Exception e){
  58.                      
  59.               }
  60.        }
  61. }
  62. 2.数据流的实现
  63. 源代码A:
  64. //判断数据流
  65. public class A {
  66.        B b=new B();
  67.        String s=new String();
  68.        public A() {
  69.               super();
  70.               // TODO Auto-generated constructor stub
  71.        }
  72.        public static void main(String[] args) { 
  73.               // TODO Auto-generated method stub
  74.        }
  75.        public String pass(int i)
  76.        {
  77.      s=b.check(i);
  78.      return s;
  79.        }
  80. }
  81. 源代码B:
  82. //判断数据流
  83. public class B {
  84.        public B() {
  85.               super();
  86.               // TODO Auto-generated constructor stub
  87.        }
  88.        /**
  89.         * @param args
  90.         */
  91.        public static void main(String[] args) {
  92.               // TODO Auto-generated method stub
  93.        }
  94.        public String check(int i) {
  95.               try {
  96.               if (i>0return ">";
  97.               if (i==0return "=";
  98.               if (i<0return "<";
  99.        }
  100.               
  101.        catch (Exception e){
  102.        
  103.        }
  104.               return null;
  105.        }
  106. }
  107. 测试代码:
  108. import junit.framework.TestCase;
  109. public class ATest extends TestCase {
  110.         public A a;
  111.         String a1,a2,a3;
  112.        public static void main(String[] args) {
  113.        }
  114.        public ATest(String name) {
  115.               super(name);
  116.        }
  117.        protected void setUp() throws Exception {
  118.               super.setUp();
  119.               a=new A();
  120.               a1=new String(">");
  121.               a2=new String("=");
  122.               a3=new String("<");
  123.        }
  124.        protected void tearDown() throws Exception {
  125.               super.tearDown();
  126.        }
  127.        public void testpass() {
  128.        assertEquals(a1,this.a.pass(1));
  129.        System.out.println(this.a.pass(1));
  130.        assertEquals(a2,this.a.pass(0));
  131.        System.out.println(this.a.pass(0));
  132.        assertEquals(a3,this.a.pass(-1));
  133.        System.out.println(this.a.pass(-1));
  134.        
  135.        }}
  136. 4.       测试异常
  137. 源代码:
  138. //白盒边界测试及异常流测试
  139. public class Add {
  140.  int iqq=0;
  141.  String iString=new String();
  142.  String s=new String("X");
  143. public Add() {
  144.         super();
  145.         // TODO Auto-generated constructor stub
  146. }
  147.    public int intAdd(int a) throws Exception{
  148.    if (a < 0)
  149.    {
  150.            throw new Exception("aaaa");
  151.    }
  152.    this.iqq=a+10;
  153.    return this.iqq;
  154.    }
  155.   public String intString(String b) throws Exception {
  156.   return s+b;
  157.   }
  158. /**
  159.  * @param args
  160.  */
  161. }
  162. 单元测试代码:
  163. import junit.framework.Test;
  164. import junit.framework.TestCase;
  165. import junit.framework.TestSuite;
  166. //白盒边界测试及异常流测试
  167. public class TestAdd extends TestCase {
  168. public int int1,int2,int3,int4,int5;
  169. Add a;
  170. String a1,b1,c1,d1,e1;
  171. public static void main(String[] args) {
  172. }
  173. public TestAdd(String name) {
  174.         super(name);
  175. }
  176. protected void setUp() throws Exception {
  177.         super.setUp();
  178.         a=new Add();
  179.         int1=10;
  180.         int2=11;
  181.         int3=100010;
  182.         int4=-10;
  183.         int5=1;
  184.         a1=new String("X1");
  185.         b1=new String("X2");
  186.         c1=new String("Xaaa");
  187.         d1=new String("Xbbb");
  188.         e1=new String("X0");
  189.         
  190. }
  191. protected void tearDown() throws Exception {
  192.         super.tearDown();
  193. }
  194.     public void testintAdd() throws Exception{
  195.       this.assertEquals(int1,a.intAdd(0));
  196.       this.assertEquals(int2,a.intAdd(1)); 
  197.       this.assertEquals(int3,a.intAdd(100000));
  198.       try
  199.       {
  200.       
  201.       this.assertEquals(int4,a.intAdd(-20));
  202.       fail("发生错误");
  203.       }
  204.       catch(Exception e)
  205.       {
  206.       
  207.       }
  208.       
  209.       try {
  210.       this.assertEquals(int5,a.intAdd(-9));
  211.       fail();
  212.       }
  213.       catch(Exception e1)
  214.       {
  215.       }
  216.       }
  217.     
  218.     public static Test suite(){
  219.     return new TestSuite(TestAdd.class);
  220.     }
  221.     public void testintString() throws Exception{
  222.     this.assertEquals(a1,a.intString("1"));     
  223.     this.assertEquals(b1,a.intString("2"));    
  224.     this.assertEquals(c1,a.intString("aaa"));  
  225.     this.assertEquals(d1,a.intString("bbb")); 
  226.     this.assertEquals(e1,a.intString("0"));     
  227.        
  228.     }
  229. }
  230.   

   (九)DJUnit的单元测试覆盖度

http://cmmi9.blogbus.com/files/1143003400.gif

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值