Junit(单元测试)

直接学习该链接汇总起来的,可以直接看这个:https://blog.csdn.net/caohongxing/category_11220107.html


目录:
一、一个简单的Junit单元测试
1.被测代码
2.Junit单元测试步骤
3.Junit注解含义
4.断言的方法
二、插件安装
1.覆盖率EclEmma插件安装使用
2.控制流图插件CFG生成器


一、一个简单的Junit单元测试

实现一下简单的单元测试内容

1.被测代码

import static org.junit.Assert.assertEquals;
public class Calc {
    int add(int a,int b)
    {
        int c ;
        c= a+b;
        return c;
    }
    int substract(int a,int b)
    {
        int c ;
        c= a-b;
        return c;
    }
    int mulitple(int a,int b)
    {
        int c ;
        c= a*b+1;
        return c;
    }
    int divid(int a,int b)
    {
        int c ;
        if(b == 0)
        {
            throw new ArithmeticException("fengmu can not be 0");
        }
        c= a/b;
        return c;
    }
    public static void main(String args[])
    {
        Calc c = new Calc();
        int r = c.add(2, 1);
        if(r==3)
            System.out.println("add(1, 2) 结果正确");
        else 
            System.out.println("add(1, 2) 结果错误");

        r = c.add(-2, -1);
        if(r==-3)
            System.out.println("add(-1, -2) 结果正确");
        else 
            System.out.println("add(-1, -2) 结果错误");
    }
}

2.Junit单元测试步骤

(1)创建junit测试
image.png
(2)测试配置
image.png
image.png
3)文件运行
image.png
image.png
此处截图是失败的,因为用例并没有实现
(4)举例加法测试用例编写运行
image.png
保存运行后得到(绿色代表通过)
image.png
(5)举例除法测试用例编写
image.png
image.png

3.Junit注解含义

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class branchCoverTest {
	@BeforeClass
	public static void beforemyclass()
	{	System.out.println("beforeClass:  Before Class");	}
	@AfterClass
	public static void aftermyclass()
	{System.out.println("AfterClass: after class");	}
	@Before
	public void beforemytest()
	{System.out.println("before:  before every test case");	}
	@After
	public void aftermytest()
	{System.out.println("After:  after every test case");	}
	@Test
	public void testFun3_01() {
		branchCover bc = new branchCover();
		bc.fun3(2, 0, 1);			
        System.out.println("testFun3_01用例执行");
    }
	@Test
	public void testFun3_02() {
		branchCover bc = new branchCover();
		bc.fun3(2, -1, 1);		
        System.out.println("testFun3_02用例执行");
    }
 	//@@Ignore 注解表示该测试用例暂不执行
	@Ignore
	@Test
	public void testFun3_03() {
		branchCover bc = new branchCover();
		bc.fun3(0, -1, 1);		
	}
}

在该代码中可以看到@BeforeClass、@AfterClass、@Before、@After、@Test、@Ignore注解:
@BeforeClass:在所有测试用例开始前执行,方法必须是静态且没有返回值的
@AfterClass:在所有测试用例完成后执行,方法必须是静态且没有返回值的
@Before:在当前用例执行前执行
@After:在当前用例执行后执行
@Ignore:该测试用例暂不执行
由此可见,该运行情况打印如下
image.png
详细注解含义如下:
image.png

4.断言的方法

image.png
image.png

二、插件安装

1.覆盖率EclEmma插件安装使用

(1)点击
image.png
(2)在弹出窗口上输入搜索条件 “Coverage”后,点击回车键,找到EclEmma Java Code Coverage安装
image.png
(3)运行语句覆盖
image.png
红色表示未覆盖
黄色表示部分覆盖
绿色表示覆盖
(4)查看覆盖率
image.png
image.png
得到
image.png

2.控制流图插件CFG生成器

(1)点击
image.png
(2)输入http://eclipsefcg.sourceforge.net后,回车
image.png
image.png
image.png
image.png
(3)等了好久,终于可以了,然后运行
image.png
(4)得到
image.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值