原文:http://cmmi9.blogbus.com/logs/2110514.html
1. 基于测试驱动开发(TDD)的思想,代码开发的方式有如下三种:一是先根据设计把框架写好,该留的接口留。然后再写测试代码,最后写开发代码。二是在编写代码之前,把所有的代码测试代码写好。三是先写完代码,后测试。个人推荐第一种方式。
2. 单元测试的方法为每个类建立对应的测试类,为每个函数(很简单的除外)建立相应的测试函数,建议主张以函作作为测试单元的单位。单元测试不应过分强调面向对象,因为局部的代码依然是结构化的。
3. 关于单元测试的覆盖度问题:首先选择测试的范围,范围的确定是要保证能找到Bug。然后建立相应的测试覆盖度要达到的最小百分比,以决定什么时候停止覆盖度分析。关于覆盖度,它可以分析哪些语句或是分支没有测试到,从而新增测试用例。目前使用了DJUnit的单元测试覆盖度分析。使用时,用RUN AS..
4. 关于单元测试工具JUnit的使用
(一)JUnit的特征
1. 使用断言方法判断期望值和实际值的差异,返回布尔值;
2. 测试驱动设备使用共同的初始化变量或方实例;
3. 支持图形交互模式和文本支持格式;
(二)框架组成
1.对测试目标进行测试的方法和过程集合,称为测试用例TestCase;
2.测试用例的集合,可容纳多个TestCase,称为测试包TestSuite;
3.测试结果的描述与记录TestResult;
4.测试过程中的事件监听者TestListener;
5.每个测试方法所发生的与预期不一致的状态的描述,称为测试失败元素TestFailure;
(三)抽象类
1.TestCase是Test的接口的实现,构造函数TestCase(String 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.test。JUnit会自动识别。
(七)JUnit的判断方法
assertEquals(Object a,Object b);
(八)实例
1.测试分支的实现
源代码:
- public class Choose {
- public Choose() {
- super();
- // TODO Auto-generated constructor stub
- }
- public String c(int i) throws Exception{
- int aaa=i;
- if (aaa>0) {
- return ">";
- }
- if (aaa<0) {
- return "<";
- }
- if (aaa==0) {
- return "=";
- }
- else
- return "null";
- }}
- 测试代码:
- //判断测试覆盖率
- //判断测试分支
- import junit.framework.TestCase;
- public class TestChoose extends TestCase {
- public Choose c;
- public int i1,i2,i3;
- String abc;
- //测试中的三个值
- String test;
- //调用Choose类c方法后的返回值
- public static void main(String[] args) {
- }
- public TestChoose(String name) {
- super(name);
- }
- protected void setUp() throws Exception {
- super.setUp();
- this.c=new Choose();
- i1=1;
- i2=0;
- i3=-1;
- test=new String();
- abc=new String("1");
- }
- public void testc() {
- try {
- test=c.c(i1);
- assertEquals(">",test);
- test=c.c(i2);
- assertEquals("=",test);
- test=c.c(i3);
- assertEquals("<",test);
- //fail("没有异常");
- }
- catch(Exception e){
- }
- }
- }
- 2.数据流的实现
- 源代码A:
- //判断数据流
- public class A {
- B b=new B();
- String s=new String();
- public A() {
- super();
- // TODO Auto-generated constructor stub
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- }
- public String pass(int i)
- {
- s=b.check(i);
- return s;
- }
- }
- 源代码B:
- //判断数据流
- public class B {
- public B() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- }
- public String check(int i) {
- try {
- if (i>0) return ">";
- if (i==0) return "=";
- if (i<0) return "<";
- }
- catch (Exception e){
- }
- return null;
- }
- }
- 测试代码:
- import junit.framework.TestCase;
- public class ATest extends TestCase {
- public A a;
- String a1,a2,a3;
- public static void main(String[] args) {
- }
- public ATest(String name) {
- super(name);
- }
- protected void setUp() throws Exception {
- super.setUp();
- a=new A();
- a1=new String(">");
- a2=new String("=");
- a3=new String("<");
- }
- protected void tearDown() throws Exception {
- super.tearDown();
- }
- public void testpass() {
- assertEquals(a1,this.a.pass(1));
- System.out.println(this.a.pass(1));
- assertEquals(a2,this.a.pass(0));
- System.out.println(this.a.pass(0));
- assertEquals(a3,this.a.pass(-1));
- System.out.println(this.a.pass(-1));
- }}
- 4. 测试异常
- 源代码:
- //白盒边界测试及异常流测试
- public class Add {
- int iqq=0;
- String iString=new String();
- String s=new String("X");
- public Add() {
- super();
- // TODO Auto-generated constructor stub
- }
- public int intAdd(int a) throws Exception{
- if (a < 0)
- {
- throw new Exception("aaaa");
- }
- this.iqq=a+10;
- return this.iqq;
- }
- public String intString(String b) throws Exception {
- return s+b;
- }
- /**
- * @param args
- */
- }
- 单元测试代码:
- import junit.framework.Test;
- import junit.framework.TestCase;
- import junit.framework.TestSuite;
- //白盒边界测试及异常流测试
- public class TestAdd extends TestCase {
- public int int1,int2,int3,int4,int5;
- Add a;
- String a1,b1,c1,d1,e1;
- public static void main(String[] args) {
- }
- public TestAdd(String name) {
- super(name);
- }
- protected void setUp() throws Exception {
- super.setUp();
- a=new Add();
- int1=10;
- int2=11;
- int3=100010;
- int4=-10;
- int5=1;
- a1=new String("X1");
- b1=new String("X2");
- c1=new String("Xaaa");
- d1=new String("Xbbb");
- e1=new String("X0");
- }
- protected void tearDown() throws Exception {
- super.tearDown();
- }
- public void testintAdd() throws Exception{
- this.assertEquals(int1,a.intAdd(0));
- this.assertEquals(int2,a.intAdd(1));
- this.assertEquals(int3,a.intAdd(100000));
- try
- {
- this.assertEquals(int4,a.intAdd(-20));
- fail("发生错误");
- }
- catch(Exception e)
- {
- }
- try {
- this.assertEquals(int5,a.intAdd(-9));
- fail();
- }
- catch(Exception e1)
- {
- }
- }
- public static Test suite(){
- return new TestSuite(TestAdd.class);
- }
- public void testintString() throws Exception{
- this.assertEquals(a1,a.intString("1"));
- this.assertEquals(b1,a.intString("2"));
- this.assertEquals(c1,a.intString("aaa"));
- this.assertEquals(d1,a.intString("bbb"));
- this.assertEquals(e1,a.intString("0"));
- }
- }
(九)DJUnit的单元测试覆盖度