JUnit4学习总结

现在一般使用的是Junit4。JUnit4比3方便很多,可以使用注解的方式

即写一个测试类。然后在某个方法上添加@Test即可。

@Test的方法不能有返回类型。即为void。而且不能有参数,但是方法名字可以自己定义

BeforeClassAfterClass在一个Test类的所有测试方法执行前后各执行且只执行一次

@Ignore忽略的测试方法

使用该注解的必须使用静态方法

@BeforeClass 比如常用来初始化一些文件流,对象之类的

@AfterClass 比如常用来关闭一些文件流之类的。

 

 

执行顺序

@BeforeClass

@Before使用了该元数据的方法在每个测试方法执行之前都要执行一次。

@Test表示这是一个用来测试的方法

@After使用了该元数据的方法在每个测试方法执行之后要执行一次。

@AfterClass

 

 

import org.junit.Test;

public class TestJunit{

   

@Test

    public void testAdd(){

       

        Calculator calculator = new Calculator();

       

        assertEquals(4,calculator.add(1, 3));

       

        assertEquals(25, calculator.mutply(5, 5));

}

 

}

 

Calculator类的结构

 

public class Calculator{

 

public int add(int num1,int num2){

      return num1 + num2;

}

 

}

 

高级应用

1.      参数化测试

JUnit4中参数化测试要点:

1.
测试类必须由Parameterized测试运行器修饰

2.
准备数据。数据的准备需要在一个方法中进行,该方法需要满足一定的要求:

1
)该方法必须由Parameters注解修饰
2
)该方法必须为public static
3
)该方法必须返回Collection类型
4
)该方法的名字不做要求
5
)该方法没有参数

package com.test.junit;

import java.util.ArrayList;
import java.util.Collection;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ParameterTest {

    private String callName;
   
    private String answerName;

    public static SayHello sayHello;
    
    @BeforeClass
    public static void beforeClass(){
       
        sayHello = new SayHello();
    }
   
    public ParameterTest(String callName,String answerName){
       
        super();
       
        this.callName = callName;
       
        this.answerName = answerName;
    }

    @Parameters
    public static Collection getName(){
       
        Collection collection = new ArrayList();
       
        collection.add(new String[]{null,""});
       
        collection.add(new String[]{"",null});
       
        collection.add(new String[]{"sinly","tom"});
       
        collection.add(new String[]{"ly","sinly"});
       
        return collection;
    }
    
    @Test
    public void testCall(){
       
        System.out.println("测试call方法");
       
        sayHello.call(callName,answerName);
    }
    
    @Test
    public void testSayHello(){

        System.out.println("测试sayHello方法");
       
        sayHello.sayHello(callName);
    }
}

 

 

2.      理论机制

1、指定运行runnerTheories.class

2、参数集用@DataPoint注解,且必须是public static类型

3、具体测试方法不使用@Test注解,而是@Theory注解

 

http://blog.csdn.net/cskgnt/article/details/8537431

运行器使用的是

@RunWith(Theories.class)

要测试的数据必须是public static 类型的,而且要使用@DatePoint注解。

要测试的方法使用@Theory注解

而且测试方法的参数类型起到了过滤作用,如果定义为String类型的,则int类型的不会被测试到。

例如

import static org.junit.Assert.*;

import org.junit.experimental.theories.DataPoint;

import org.junit.experimental.theories.Theories;

import org.junit.experimental.theories.Theory;

import org.junit.runner.RunWith;

 

@RunWith(Theories.class)

public class TestBoolean {

 

      @DataPoint

      public static boolean paramA = true;

      @DataPoint

      public static boolean paramB = false;

     

      @Theory

      public void testCase(boolean param) {

             assertTrue(param);

      }

 

}

3.      嵌套测试

 

测试结果分析

1.      正常程序运行正常,无异常

2.      失败程序运行正常,但是结果与断言不一样

3.      异常程序运行过程中出现了异常

 

嵌套测试

有的时候,测试的类比较多,如果每个都去跑一遍的话,会比较麻烦,JUint提供了一个嵌套测试,也常说打包测试。我们只需要把要测试的类放在某个类中,使用Suite运行器,并且使用注解SuiteClasses就可以把要测试的类直接添加进去。以数组的形式。

到时只需要运行这个类就可以测试很多类了。当然这个可以层层嵌套,比如一个模块写一个,然后再最外层吧需要的模块给添加进去即可。我们运行的也就是最外层的这个,当出现问题时,我们再去看是哪个模块出问题了即可。

package com.test.junit;

import org.junit.runner.RunWith;

import org.junit.runners.Suite;

@RunWith(Suite.class)

@SuiteClasses({ParameterTest.class})

publicclass SayHelloTest {

 

}

 

JUnit的类结构
JUnit有四个重要的类:TestSuite、TestCase、TestResult、TestRunner。前三个类属于Framework包, 后一个类在不同的环境下是不同的。这里使用的是文本测试环境,所以用的是 junit.textui.TestRunner。各个类的职责如下:
1.TestResult,负责收集TestCase所执行的结果,它将结果分为两类,客户可预测的Failure和没有预测的Error。同时负责将测试结果转发到TestListener(该接口由TestRunner继承)处理;
2.TestRunner,客户对象调用的起点,负责对整个测试流程的跟踪。能够显示返回的测试结果,并且报告测试的进度。
3.TestSuite, 负责包装和运行所有的TestCase。
4.TestCase, 客户测试类所要继承的类,负责测试时对客户类进行初始化,以及测试方法调用。
另外还有两个重要的接口:Test和TestListener。
1.Test, 包含两个方法:run() 和countTestCases(),它是对测试动作特征的提取。
2.TestListener, 包含四个方法:addError()、addFailure()、startTest()和endTest(),它是对测试结果的处理以及测试驱动过程的动作特征的提取。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值