Junit 学习笔记
Junit 学习笔记
1. 编写测试用例时需要注意
- 测试方法上必须使用
@Test进行修饰 - 测试方法必须使用
public void进行修饰,不能带任何参数 - 新建一个车源代码目录来存放我们的测试代码
- 测试类的包应该和被测试类保持一致
- 测试单元中的每个方法必须可以独立测试,测试方向间不能有任何依赖
- 测试类使用
Test作为类名的后缀(不是必须) - 测试方法使用
Test作为方法名的前缀(不是必须)
2. 出现结果分析
Failure一般由单元测试使用的断言方法判断失败所引起,这表示测试点发现了问题,就是说程序输出的结果和我们预期的不一样。error是由代码异常引起的,它可以产生于测试代码本身的错误,也可以是被测试代码中的一个隐藏 bug- 测试用例不是用来证明你是对的,而是用来证明你没有错(即测试用例用来达到想要的预期结果,但对于逻辑错误无能为力)。
3. Junit 运行流程
举个例子:
public class JunitFlowTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("this is beforeClass...");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("this is afterClass...");
}
@Before
public void setUp() throws Exception {
System.out.println("this is before...");
}
@After
public void tearDown() throws Exception {
System.out.println("this is after");
}
@Test
public void test1() {
System.out.println("this is test1...");
}
}
输出结果如下:
---- IntelliJ IDEA coverage runner ----
sampling ...
include patterns:
com\.test\.util\..*
exclude patterns:this is beforeClass...
this is before...
this is test1...
this is after
this is afterClass...
Process finished with exit code 0
解释如下:
@BeforeClass修饰的方法会在所有方法被调用前被执行,而且该方法是静态的,所以当测试类被加载后接着就会运行它,而且在内存中它只回存在一份实例,它比较适合加载配置文件@AfterClass所修饰的方法通常用来对资源的清理,如关闭数据库的连接@Before和@After会在每个测试方法的前后各执行一次
4. Junit 常用注解
@Test:将一个普通的方法修饰成为一个测试方法@Test(expected=XX.class):用来捕获异常@Test(timeout=毫秒):到时间后停止测试(用来测试一些循环很久的语句)
@BeforeClass:它会在所有的方法运行前被执行,static 修饰@AfterClass:它会在所有的方法运行结束后被执行,static 修饰@Before:会在每一个测试方法被运行前执行一次@After:会在每一个测试方法运行后被执行一次@Ignore:所修饰的测试方法会被测试运行器忽略@RunWith:可以更改测试运行器org.junit.runner.Runner
举个例子:
public class AnnotationTest {
@Test(expected = ArithmeticException.class)
public void testDivide() {
assertEquals(3, new Calculate().divide(6, 0));
}
@Test(timeout = 2000)
@Ignore
public void testWhile() {
while (true) {
System.out.println("run forever...");
}
}
@Test(timeout = 3000)
public void testReadFile() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
5. Junit 测试套件的使用
测试套件就是组织测试类一起运行的。
步骤:
- 写一个作为测试套件的入口类,这个类里不包含其他的方法
- 更改测试运行器
Suite.class - 将要测试的类作为数组传入到
Suite.SuiteClasses({})
例子:
@RunWith(Suite.class)
@Suite.SuiteClasses({TaskTest1.class, TaskTest2.class, TaskTest3.class})
public class SuiteClasses {
}
6. Junit 参数化设置
步骤
- 更多默认的测试运行器为
RunWith(Parameterized.class) - 声明变量来存放预期值和结果值
- 声明一个返回值为 Collection 的公共静态方法,并使用
@Parameters进行修饰 - 为测试类声明一个带有参数的公共构造函数,并在其中为之声明变量赋值
例子
ParameterTest.java:
@RunWith(Parameterized.class)
public class ParameterTest {
int expected = 0;
int input1 = 0;
int input2 = 0;
@Parameterized.Parameters
public static Collection<Object[]> t() {
return Arrays.asList(new Object[][] {
{3, 1, 2},
{4, 2, 2}
});
}
public ParameterTest(int expected, int input1, int input2) {
this.expected = expected;
this.input1 = input1;
this.input2 = input2;
}
@Test
public void add() {
assertEquals(expected, new Calculate().add(input1, input2));
}
}
Junit 学习笔记的更多相关文章
- junit学习笔记
junit编程规范 测试方法上必须使用@Test进行修饰 测试方法必须使用public void 进行修饰,不能带任何的参数 新建一个源代码目录 测试类的包应该和被测试类保持一致 测试单元中的每个方法 ...
- junit学习笔记(二):hamcrest和TestSuit
1. hamcrest hamcrest可以有效增加junit的测试能力,用一些对通俗语言来进行测试. Hamcrest 是一个测试的框架,它提供了一套通用的匹配符 Matcher,灵活使用这些匹配符 ...
- JUnit学习笔记-0-JUnit启动类
[说明]:本文基于JUnit4.13版本代码,JDK1.8.0_151环境,使用工具为Eclipse,版本为Oxygen.1a Release (4.7.1a) [图示]: [正文]:JUnit4.1 ...
- Junit学习笔记之五:MockMVC
原文:https://blog.csdn.net/xiao_xuwen/article/details/52890730 随着RESTful Web Service的流行,测试对外的Service是否 ...
- 《Java学习笔记(第8版)》学习指导
<Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...
- 积极主动敲代码,使用Junit学习Java程序设计
积极主动敲代码,使用JUnit学习Java 早起看到周筠老师在知乎的回答软件专业成绩很好但是实际能力很差怎么办?,很有感触. 从读大学算起,我敲过不下100本程序设计图书的代码,我的学习经验带来我的程 ...
- [原创]java WEB学习笔记109:Spring学习---spring中事物管理
博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好 ...
- [原创]java WEB学习笔记109:Spring学习---spring对JDBC的支持:使用 JdbcTemplate 查询数据库,简化 JDBC 模板查询,在 JDBC 模板中使用具名参数两种实现
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Android自动化学习笔记:编写MonkeyRunner脚本的几种方式
---------------------------------------------------------------------------------------------------- ...
随机推荐
- go struct 自定义标签获取
package main import ( "fmt" "reflect" ) type Test struct { Id int `json:"us ...
- ArcGIS数据建模 (模型构建器modelbuilder) 培训视频 5章28小节587分钟视频 51GIS网站上线
网址:http://www.51gis.com.cn/kecheng.html?id=358
- GIS 空间分析案例教程-坐标高斯投影正反算
GIS 空间分析案例教程-坐标高斯投影正反算 商务科技合作:向日葵,135-4855__4328,xiexiaokui#qq.com 特点: 1. 地理处理工具,可以与任何arcgis 工具和语言集成 ...
- Python 中的 -> 是什么意思
函数标注通常用于 类型提示:例如以下函数预期接受两个 int 参数并预期返回一个 int 值:```def sum_two_numbers(a: int, b: int) -> int:retu ...
- 安卓P2P开源项目
https://github.com/LinYaoTian/P2PChat 一个基于局域网的 Android P2P 聊天系统 https://github.com/ddssingsong/webrt ...
- Android插件化(六): OpenAtlasの改写aapt以防止资源ID冲突
Android插件化(六): OpenAtlasの改写aapt以防止资源ID冲突 转 https://www.300168.com/yidong/show-2791.html 核心提示:引言And ...
- 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_04-用户认证技术方案-SpringSecurityOauth2
2.3 Spring security Oauth2认证解决方案 本项目采用 Spring security + Oauth2完成用户认证及用户授权,Spring security 是一个强大的和高度 ...
- 123457123457#0#-----com.threeapp.renzhepaoku01----儿童跑酷游戏(忍者版)
com.threeapp.renzhepaoku01----儿童跑酷游戏(忍者版)
- python读取csv文件、excel文件并封装成dict类型的list,直接看代码
# coding=UTF-8import csvimport xlrd class ReaderFile(): """ 读取csv文件 filePath:文件路径 &qu ...
- (八)利用apache组件进行文件上传下载
一.文件上传 文件上传,即服务器端得到并处理用户上传的文件,这个文件存放在request里,也就是需要对request进行处理. 1.1 编写html文件 <!DOCTYPE html> ...