栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

Java练习(二十一):JUnite4的简单使用流程(Eclipse中)

Java 更新时间:发布时间: 百科书网 趣学号

目录

1. 思考:作为测试人员,通常我们拿到一些类/ar包后,要进行单元测试,运用什么框架?如何构建?

2. JUnit4介绍

2.1 概述

2.2 特性

3. JUnit4 环境搭建

4. 示例工程

4.1 创建一个带运行的常规类(或者从开发那儿拿到的待测jar包之类)

4.2 创建一个Junit Case

4.3 根据实际需求编写Junit Case类

4.4 运行测试类(单个)

5. 打包测试Suite

5.1 为什么需要打包测试?

5.2 示例工程

5.2.1 需要被测试的常规类:Calc.lava

5.2.2 测试类1:MyJUnit4Demo1.java

5.2.3 测试类2:MyJUnit4Demo2.java

5.2.4 打包Suite类:AllTests.java

5.2.5 运行打包Suite类


1. 思考:作为测试人员,通常我们拿到一些类/ar包后,要进行单元测试,运用什么框架?如何构建?

2. JUnit4介绍

2.1 概述

JUnit4是JUnit框架有史以来的最大改进,目标是利用Java5的 Annotation特性简化测试用例的缩写。

2.2 特性

1)使用junit4.x版本进行单元测试时,不用测试类继承TestCase父类。因为, junit4.x全面引入了Annotation来执行我们编写的测试。

2)junit4.x版本,引用了注解的方式进行单元测试。

3)junit4.x版本我们常用的注解:

@Before:与junit3.x中的setUp()方法功能一样,在每个测试方法之前执行。

@After:与tearDown()方法一样。

@BeforeClass:在所有方法执行之前执行

@AfterClass:在所有方法执行之后执行

补充:

JUnite4的执行顺序:@BeforeClass > @Before > @Test1 > @After > @Before > @Test2 > @After ...... > @AfterClass

@Test(timeout = xxx): 设置当前测试方法在一定时间内运行完,否则返回错误

@Test(expected = Exception.class):设置被测试的方法是否有异常抛出。抛出异常类型为: Exception.class

@Ignore:注释掉一个测试方法或者一个类,被注释的方法或类,不会被执行。

3. JUnit4 环境搭建

3.1 选中 Project > 右键 Properties > Java Build Path > Libraries > Junit,选JUnit4

 

4. 示例工程

4.1 创建一个带运行的常规类(或者从开发那儿拿到的待测jar包之类)

package com.my.junit4.demo;

public class Calc {
    public int add(int a,int b){
        return a+b;
    }
    
    public int div(int a,int b){    
        if (b==0){
            return -1;
            }    
        return a/b;
    }
}

4.2 创建一个Junit Case

new > JUnit Test Case,选择“Class Under test”可以选择要进行单元测试的类

4.3 根据实际需求编写Junit Case类

上一步会生成一个带有一定框架的测试类,在这个框架中,可以根据自己的需求,在各个注解部分添加命令,或者调用其他待测试类,以备下一步的测试。

package com.my.junit4.demo;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class MyJUnit4Demo1 {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("2 setup beforecalss");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("2 teardown aftercalss");
    }

    @Before
    public void setUp() throws Exception {
        System.out.println("2 setup");
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("2 teardown");
    }

    @Test
    public void test1() {
        System.out.println("2 test1");
        Calc calc = new Calc();
        assertEquals(5, calc.add(3,2));
    }
    
    @Test
    public void test2() {
        System.out.println("2 test2");
        Calc calc = new Calc();
        assertEquals(5, calc.div(15,3));
    }
    
    @Test
    public void test3() {
        System.out.println("2 test3");
        Calc calc = new Calc();
        assertEquals(5, calc.div(15,4));
    }
    
    @Test
    public void test4() {
        System.out.println("2 test4");
        Calc calc = new Calc();
        assertEquals(5, calc.add(5,4));
    }

}
 

4.4 运行测试类(单个)

选中3.4中创建的测试类,右键,Run as -> JUnit test

运行结果如下:

5. 打包测试Suite

5.1 为什么需要打包测试?

当有很多个测试类的时候,如果一个一个执行,比较麻烦。

JUnit提供了打包测试的功能,将所有需要运行的测试类集中起来,一次性的运行完毕,大大方便了我们的测试工作。

5.2 示例工程

假设目前有如下几个类

5.2.1 需要被测试的常规类:Calc.lava

同上

5.2.2 测试类1:MyJUnit4Demo1.java

package com.my.junit4.demo;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class MyJUnit4Demo1 {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("setup beforecalss");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("teardown aftercalss");
    }

    @Before
    public void setUp() throws Exception {
        System.out.println("setup");
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("teardown");
    }

    @Test
    public void test1() {
        System.out.println("test1");
        Calc calc = new Calc();
        assertEquals(5, calc.add(3,2));
    }
    
    @Test
    public void test2() {
        System.out.println("test2");
        Calc calc = new Calc();
        assertEquals(5, calc.div(15,3));
    }
    
    @Test
    public void test3() {
        System.out.println("test3");
        Calc calc = new Calc();
        assertEquals(5, calc.div(15,4));
    }
    
    @Test
    public void test4() {
        System.out.println("test4");
        Calc calc = new Calc();
        assertEquals(5, calc.add(5,4));
    }

}
 

5.2.3 测试类2:MyJUnit4Demo2.java

package com.my.junit4.demo;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class MyJUnit4Demo2 {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("2 setup beforecalss");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("2 teardown aftercalss");
    }

    @Before
    public void setUp() throws Exception {
        System.out.println("2 setup");
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("2 teardown");
    }

    @Test
    public void test1() {
        System.out.println("2 test1");
        Calc calc = new Calc();
        assertEquals(5, calc.add(3,2));
    }
    
    @Test
    public void test2() {
        System.out.println("2 test2");
        Calc calc = new Calc();
        assertEquals(5, calc.div(15,3));
    }
    
    @Test
    public void test3() {
        System.out.println("2 test3");
        Calc calc = new Calc();
        assertEquals(5, calc.div(15,4));
    }
    
    @Test
    public void test4() {
        System.out.println("2 test4");
        Calc calc = new Calc();
        assertEquals(5, calc.add(5,4));
    }

}
 

5.2.4 打包Suite类:AllTests.java

在所在的包上右键,new -> Other... -> JUnit -> JUnit Test Suite, 选择需要一并运行的测试类。会生成一个Suite类,具体代码如下:

package com.my.junit4.demo;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ MyJUnit4Demo1.class, MyJUnit4Demo2.class })
public class AllTests {

}
 

5.2.5 运行打包Suite类

选中4.1中创建的Suite类,右键,Run as -> JUnit test。测试结果如下:

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/275253.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号