当前位置:网站首页>Junit单元测试
Junit单元测试
2022-06-26 16:30:00 【&volume】
目录
1.测试分类:
(1)黑盒测试:
不需要写代码,给输入值,看程序是否输出期望的值
大部分都在使用黑盒测试,就是根据你写的代码进行测试
(2)白盒测试:
需要写代码,关注程序的具体的执行流程
2.Junit的使用:
Junit是白盒测试
(1)@Test的使用
1.定义一个类
先在类中定义方法
package cn.junit;
/*
* 计算器类
* */
public class Calcuator {
/*
* 加法
*/
public int add(int a,int b)
{
return a+b;
}
/*
减法
*/
public int sub(int a,int b)
{
return a-b;
}
}
2.再定义一个测试类
package cn.test;
import cn.junit.Calcuator;
import org.junit.Test;
public class CalculatorTest {
/*
测试add方法
*/
@Test
public void testAdd()
{
//创建计算器对象
Calcuator c=new Calcuator();
//调用方法
int ad = c.add(1, 2);
System.out.println(ad);
}
}
刚加上@Test 因为没有导包是会爆红的
点一下旁边小灯泡
就会自动导包
3.断言操作
我们测试过程中往往是不要输出的,而会进行断言操作
Assert.assertEquals(断言值,输出值);
package cn.test;
import cn.junit.Calcuator;
import org.junit.Assert;
import org.junit.Test;
public class CalculatorTest {
/*
测试add方法
*/
@Test
public void testAdd()
{
//创建计算器对象
Calcuator c=new Calcuator();
//调用方法
int ad = c.add(1, 2);
Assert.assertEquals(3,ad);
// System.out.println(ad);
}
}
如果两值不同,则会爆红出错
断言失败样例:
断言成功样例:
(2)@Before和@After
[email protected]
@Before的存在是为了让代码简洁,去重复化操作
@Before在每个@Test注解前都要先执行的代码进行区块化,进行@Before注解
注解后会在@Test前先执行
[email protected]
@After的存在与@Before是相同的,都是为了使代码简洁,去重复操作
@After是在每个@Test注解后都会执行的代码进行区块化,进行@After注解
注解后会在@Test注解后自动执行
3.代码测试
package cn.test;
import cn.junit.Calcuator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class CalculatorTest {
@Before
public void testinit()
{
System.out.println("init...");
}
@After
public void close()
{
System.out.println("close.....");
}
/*
测试add方法
*/
@Test
public void testAdd()
{
//创建计算器对象
Calcuator c=new Calcuator();
//调用方法
int ad = c.add(1, 2);
//Assert.assertEquals(3,ad);
System.out.println("testadd进行时");
// System.out.println(ad);
}
/*
* 测试sub方法
* */
@Test
public void testsub()
{
Calcuator c=new Calcuator();
int sub = c.sub(1, 2);
System.out.println("testsub进行时");
//System.out.println(sub);
}
}
4.测试结果
(1)测试add方法
(2)测试sub方法
3.Junit总结
(1).定义测试方法时返回值要void类型
(2).参数列表为空参
(3).判定结果 如果出现红色则说明测试失败
如果出现绿色说明测试成功
(4).一般我们会使用断言操作来处理结果
Assert.assertEquals(断言值,输出值);
(5)[email protected]修饰方法会在测试方法之前执行
(6)[email protected]修饰方法会在测试方法执行之后执行(无论测试是否成功都会执行);
边栏推荐
- SAP OData development tutorial - from getting started to improving (including segw, rap and CDP)
- 国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知
- Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser
- 用Attention和微调BERT进行自然语言推断-PyTorch
- 6 custom layer
- Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl
- [Blue Bridge Cup training 100 questions] scratch distinguishing prime numbers and composite numbers Blue Bridge Cup scratch competition special prediction programming question intensive training simul
- TCP拥塞控制详解 | 1. 概述
- 清华“神奇药水”登Nature:逆转干细胞分化,比诺奖成果更进一步,网友:不靠精子卵子就能创造生命了?!...
- 内存分区模型
猜你喜欢
Supplement the short board - Open Source im project openim about initialization / login / friend interface document introduction
Dialogue with the senior management of Chang'an Mazda, new products will be released in Q4, and space and intelligence will lead the Japanese system
李飞飞团队将ViT用在机器人身上,规划推理最高提速512倍,还cue了何恺明的MAE...
Natural language inference with attention and fine tuning Bert pytorch
Knowing these commands allows you to master shell's own tools
IAR engineering adapts gd32 chip
无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!...
pybullet机器人仿真环境搭建 5.机器人位姿可视化
【力扣刷题】二分查找:4. 寻找两个正序数组的中位数
# 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
随机推荐
R语言广义线性模型函数GLM、glm函数构建逻辑回归模型(Logistic regression)、分析模型是否过离散(Overdispersion)、使用残差偏差与二项式模型中的剩余自由度的比率评估
对话长安马自达高层,全新产品将在Q4发布,空间与智能领跑日系
架构实战营毕业设计
[Blue Bridge Cup training 100 questions] scratch distinguishing prime numbers and composite numbers Blue Bridge Cup scratch competition special prediction programming question intensive training simul
SAP OData development tutorial - from getting started to improving (including segw, rap and CDP)
若依如何实现接口限流?
proxy
r329(MAIX-II-A(M2A)资料汇总
Scala 基础 (二):变量和数据类型
Acid of redis
Cuckoo filter for Chang'an chain transaction
内存分区模型
R language plot visualization: plot visualizes the normalized histogram, adds the density curve KDE to the histogram, and uses geom at the bottom edge of the histogram_ Adding edge whisker graph with
stm32h7b0替代h750程序导致单片机挂掉无法烧录程序问题
长安链交易防重之布谷鸟过滤器
Stm32h7b0 replaces the h750 program, causing the MCU to hang up and unable to burn the program
100+数据科学面试问题和答案总结 - 基础知识和数据分析
首例猪心移植细节全面披露:患者体内发现人类疱疹病毒,死后心脏重量翻倍,心肌细胞纤维化丨团队最新论文...
板卡的分级调试经验
理想路径问题