junit断言和junit注释assert
JUnit - 使用断言
断言
所有的断言都包含在 Assert 类中
public class Assert extends java.lang.Object
这个类提供了很多有用的断言方法来编写测试用例。只有失败的断言才会被记录。Assert 类中的一些常用的方法列式如下:
序号 | 方法和描述 |
1 | void assertEquals(boolean expected, boolean actual) 检查两个变量或者等式是否平衡 |
2 |
void assertTrue(boolean expected, boolean actual) 检查条件为真 |
3 |
void assertFalse(boolean condition) 检查条件为假 |
4 |
void assertNotNull(Object object) 检查对象不为空 |
5 |
void assertNull(Object object) 检查对象为空 |
6 |
void assertSame(boolean condition) assertSame() 方法检查两个相关对象是否指向同一个对象 |
7 |
void assertNotSame(boolean condition) assertNotSame() 方法检查两个相关对象是否不指向同一个对象 |
8 |
void assertArrayEquals(expectedArray, resultArray) assertArrayEquals() 方法检查两个数组是否相等 |
待测试类
/**
*
* @author Administrator
* junit测试用例类
*/
public class Demo { /**
* 获取两数之和
* @param numOne int.
* @param numTwo int.
* @return int numOne+numTwo.
*/
public int getAdd(int numOne,int numTwo) {
return numOne+numTwo;
} /**
* 获取两数比较的真假
* @param numOne int.
* @param numTwo int.
* @return boolean numOne>numTwo.
*/
public boolean getDifference(int numOne,int numTwo) {
return numOne>numTwo;
} /**
* 获取一个字符串
* @return String.
*/
public String getString() {
return "不提也罢";
}
}
junit测试类
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;//静态导入类的所有静态方法
import org.junit.Test;
/*junit测试类*/
public class DemoTest {
Demo demo = new Demo(); @Test
public void testGetAdd() {
int add = demo.getAdd(2, 3);
assertThat(add,is(5));//变量是否等于指定值
assertThat(add, not(4));//变量是否不等于指定值
} @Test
public void testGetDifference() {
boolean difference = demo.getDifference(3, 2);
assertTrue(difference);//判断真假
} @Test
public void testGetString() {
String string = demo.getString();
//测试变量是否包含指定字符
assertThat(string, containsString("也"));
//测试变量是否已指定字符串开头
assertThat(string, startsWith("不"));
//测试变量是否以指定字符串结尾
assertThat(string, endsWith("罢"));
//测试变量是否等于指定字符串
assertThat(string, equalTo("不提也罢"));
}
}
结果通过,无错误。如图:
注释
注释就好像你可以在你的代码中添加并且在方法或者类中应用的元标签。JUnit 中的这些注释为我们提供了测试方法的相关信息,哪些方法将会在测试方法前后应用,哪些方法将会在所有方法前后应用,哪些方法将会在执行中被忽略。
JUnit 中的注释的列表以及他们的含义:
序号 | 注释和描述 |
1 | @Test 这个注释说明依附在 JUnit 的 public void 方法可以作为一个测试案例。 |
2 | @Before 有些测试在运行前需要创造几个相似的对象。在 public void 方法加该注释是因为该方法需要在 test 方法前运行。 |
3 | @After 如果你将外部资源在 Before 方法中分配,那么你需要在测试运行后释放他们。在 public void 方法加该注释是因为该方法需要在 test 方法后运行。 |
4 | @BeforeClass 在 public void 方法加该注释是因为该方法需要在类中所有方法前运行。 |
5 | @AfterClass 它将会使方法在所有测试结束后执行。这个可以用来进行清理活动。 |
6 | @Ignore 这个注释是用来忽略有关不需要执行的测试的。 |
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 ClassDemoTest { //在类加载前,只执行一次
@BeforeClass
public static void beforeClass() {
System.out.println("类加载前");
} //在类加载后,只执行一次
@AfterClass
public static void afterClass() {
System.out.println("类加载后");
} //在每一个测试方法执行前执行一次
@Before
public void before() {
System.out.println("测试方法执行前");
} //在每一个测试方法执行前执行一次
@After
public void after() {
System.out.println("测试方法执行后");
} //测试单元1
@Test
public void testCase1() {
System.out.println("测试单元1");
} //测试单元2
@Test
public void testCase2() {
System.out.println("测试单元2");
}
@Ignore
public void testIgnore() {
System.out.println("Ignore");
} }
执行结果如下:
junit断言和junit注释assert的更多相关文章
- The <classpath> or <modulepath> for <junit> must include junit.jar if not in Ant's own classpath
The <classpath> or <modulepath> for <junit> must include junit.jar if not in Ant's ...
- pytest测试框架 -- assert断言和fixture固件
一.断言 (1)使用assert语句进行断言 # test_run.py @pytest.mark.assert def test_assert(self): r = requests.get(&qu ...
- JUnit实战(2) - JUnit核心(使用Suite来组合测试)
创建Java Project项目:ch02-internals MasterTestSuite.java package com.manning.junitbook.ch02.internals; i ...
- JUnit实战(1) - JUnit起步(Parameterized参数化测试)
创建Java Project项目,项目名称:ch01-jumpstart Calculator.java public class Calculator { public double add(dou ...
- junit学习之junit的基本介绍
Junit目前在一些大的公司或者相对规范的软件中使用的比较多,相当多的小公司并没有把单元测试看的太重要.在大点的公司开发人员每天上班后,第一件事情就是从svn上把自己负责的代码checkout下来,然 ...
- junit 常用注解 + junit 断言详解
@Test: 在junit3中,是通过对测试类和测试方法的命名来确定是否是测试,且所有的测试类必须继承junit的测试基类.在junit4中,定义一个测试方法变得简单很多,只需要在方法前加上@Test ...
- [JUnit] Introduce to Junit and it annotations
Check the get started guid https://junit.org/junit5/docs/current/user-guide/#overview-getting-help p ...
- Junit 学习1 junit的简单使用
package junit; import java.sql.Connection; import java.sql.SQLException; import org.junit.Test; impo ...
- Spring是如何整合JUnit的?JUnit源码关联延伸阅读
上一篇我们回答了之前在梳理流程时遇到的一些问题,并思考了为什么要这么设计. 本篇是<如何高效阅读源码>专题的第十二篇,通过项目之间的联系来进行扩展阅读,通过项目与项目之间的联系更好的理解项 ...
随机推荐
- Linux下启动Oracle服务和监听程序
$ su – oracle $ sqlplus / nolog sql> conn / as sysdba sql> startup #启动Oracle,需 ...
- 从一组数找第K大元素
最近做面试题,经常与到一个问题,如何高效的从一组数中找到第K大的元素. 其实我们最容易想到的肯定是蛮力法. 1. 我们可以对这个乱序数组按照从大到小先行排序,然后取出前k大,总的时间复杂度为O(n*l ...
- day05数据类型,数字类型,字符串类型,字符串的操作方法,列表类型的操作方法,可变类型与不可变类型
复习 ''' 流程控制 1.顺序结构.分支结构.循环结构 2.if分支结构 if 条件: 代码块 elif 条件: 代码块 else: 代码块 # 可以被if转换为False:0 | '' | Non ...
- Unicode编码问题 如:\u529e\u7406\u9996\u6c7d\u52a0\u6cb9
python 遇到\u529e\u7406\u9996\u6c7d\u52a0\u6cb9 解决方法: a = "\u529e\u7406\u9996\u6c7d\u52a0" b ...
- Webpack与其他打包工具的区别
webpack是一个模块化加载器兼打包工具,它同时支持AMD.CMD等加载规范.与其他模块化加载器相比,它有这些优势: 1.代码分割 webpack支持两种依赖加载:同步和异步.同步的依赖会在编译时直 ...
- 安卓constraintLayout中app:srcCompat设置的图片显示不出来
使用 app:srcCompat 的时候 引入的图片显示不出来的解决方案 首先查看的你的Activity 继承的是那个Activity 如果是继承AppcompatActivity 使用 Image ...
- [C#]统计文本文件txt中的行数(快速读取)
快速统计文本文件中的行数( StreamReader.ReadLine() ): 测试代码如下: //读取txt文件中总行数的方法 public static int requestMethod(St ...
- postman(六):详解在Pre-request Script中如何执行请求
上一篇借着如何在不同接口之间传递数据,简单说了下在postman编写脚本发送请求,这里再详细介绍一下如何在Pre-request Script和Tests标签中编写脚本.因为我目前研究的也不是很深,对 ...
- python读取xml文件中的坐标点
用labelImg工具制作好xml文件后,需要读取其中img路径和坐标点,生成一个label.txt <annotation> <folder>big</folder&g ...
- 6 款最棒的 Go 语言 Web 框架简介
地址: https://studygolang.com/articles/11897?fr=sidebar