Junit-单元测试小工具

Junit概述

JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量。

JUnit 测试框架具有以下特点:

  • JUnit是用于编写和运行测试的开源框架。
  • 提供了注释,以确定测试方法。
  • 提供断言测试预期结果。
  • 提供了测试运行的运行测试。
  • JUnit测试让您可以更快地编写代码,提高质量
  • JUnit是优雅简洁。它是不那么复杂以及不需要花费太多的时间。
  • JUnit测试可以自动运行,检查自己的结果,并提供即时反馈。没有必要通过测试结果报告来手动梳理。
  • JUnit测试可以组织成测试套件包含测试案例,甚至其他测试套件。
  • Junit显示测试进度的,如果测试是没有问题条形是绿色的,测试失败则会变成红色。

Junit maven配置

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>

注:maven项目通常在test下编写测试类

Junit断言

  • void assertEquals([String message], expected value, actual value):断言两个值相等。有多种参数类型重载,第一个参数是一个可选的字符串消息
  • void assertTrue([String message], boolean condition):断言一个条件为真
  • void assertFalse([String message],boolean condition):断言一个条件为假
  • void assertNotNull([String message], java.lang.Object object):断言一个对象不为空(null)
  • void assertNull([String message], java.lang.Object object):断言一个对象为空(null)
  • void assertSame([String message], java.lang.Object expected, java.lang.Object actual):断言,两个对象引用相同的对象
    void assertNotSame([String message], java.lang.Object unexpected, java.lang.Object actual):断言,两个对象不是引用同一个对象
    void assertArrayEquals([String message], expectedArray, resultArray):断言预期数组和结果数组相等。有多种参数类型重载。

Junit注解

注解 描述
@Test
public void method()
Test注释指示该公共无效方法它所附着可以作为一个测试用例。
@Before
public void method()
Before注释表示,该方法必须在类中的每个测试之前执行,以便执行测试某些必要的先决条件。
@BeforeClass
public static void method()
BeforeClass注释指出这是附着在静态方法必须执行一次并在类的所有测试之前。发生这种情况时一般是测试计算共享配置方法(如连接到数据库)。
@After
public void method()
After 注释指示,该方法在执行每项测试后执行(如执行每一个测试后重置某些变量,删除临时变量等)
@AfterClass
public static void method()
当需要执行所有的测试在JUnit测试用例类后执行,AfterClass注解可以使用以清理建立方法,(从数据库如断开连接)。注意:附有此批注(类似于BeforeClass)的方法必须定义为静态。
@Ignore
public static void method()
当想暂时禁用特定的测试执行可以使用忽略注释。每个被注解为@Ignore的方法将不被执行。

代码示例:

import org.junit.*;

public class CalculatorTest {
    int a = 3;
    int b = 5;
    int expectAdd = 8;
    int expectSub = -2;
    Calculator calculator = new Calculator();

    @BeforeClass
    public static void beforeClass(){
        System.out.println("@BeforeClass");
    }

    @Test
    public void addTest(){
        int res = calculator.add(a,b);
        System.out.println("add("+a+","+b+")="+res);
        Assert.assertEquals(res,expectAdd);
    }

    @Test
    public void subTest(){
        int res = calculator.sub(a,b);
        System.out.println("sub("+a+","+b+")="+res);
        Assert.assertEquals(res,expectSub);
    }

    @AfterClass
    public static void onceExecutedAfterAll() {
        System.out.println("@AfterClass");
    }

    @After
    public void executedAfterEach() {
        System.out.println("@After");
    }
}

创建套件测试

测试套件是一些测试不同类用例,可以使用@RunWith@Suite注解运行所有东西在一起。如果有很多测试类,想让它们都运行在同一时间,而不是单一地运行每个测试,这是非常有用的。

下面这段代码即同时测试TestCase1和TestCase2

package junit;

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

@RunWith(Suite.class)
@Suite.SuiteClasses({TestCase1.class,TestCase2.class})
public class TestSuite {

}

构建参数化测试

满足下列要求:

  • 该类被注解为 @RunWith(Parameterized.class).
  • 这个类有一个构造函数,存储测试数据。
  • 这个类有一个静态方法生成并返回测试数据,并注明@Parameters注解。
  • 这个类有一个测试,它需要注解@Test到方法。

代码示例:

@RunWith(Parameterized.class)
public class CalculatorTest2 {
    int oper1;
    int oper2;
    int expectAdd;
    int expectSub;
    Calculator calculator = new Calculator();

    public CalculatorTest2(int oper1, int oper2, int expectAdd,int expectSub) {
        this.oper1 = oper1;
        this.oper2 = oper2;
        this.expectAdd = expectAdd;
        this.expectSub = expectSub;
    }

    @Parameterized.Parameters
    public static Collection addedNumbers(){
        return Arrays.asList(new Integer[][]{{1,2,3,-1},{4,2,6,2},{7,3,10,10},{5,2,1,3}});
    }


    @Test
    public void addTest(){
        int res = calculator.add(oper1,oper2);
        System.out.println("add("+oper1+","+oper2+")="+res);
        Assert.assertEquals(res,expectAdd);
    }

    @Test
    public void subTest(){
        int res = calculator.sub(oper1,oper2);
        System.out.println("sub("+oper1+","+oper2+")="+res);
        Assert.assertEquals(res,expectSub);
    }
}

原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/junit-%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e5%b0%8f%e5%b7%a5%e5%85%b7/

(0)
彭晨涛彭晨涛管理者
上一篇 2020年2月11日
下一篇 2020年2月11日

发表回复

登录后才能评论