Junit4
来源:互联网 发布:python 执行adb shell 编辑:程序博客网 时间:2024/04/30 04:19
什么是单元测试
写了个类,要给别人用,会不会有bug?怎么办?测试一下。
用main方法测试好不好?不好!
1. 不能一起运行!
2. 大多数情况下需要人为的观察输出确定是否正确
为什么要进行单元测试
重用测试,应付将来的实现的变化。
提高士气,明确知道我的东西是没问题的。
JUnit4 HelloWorld
1. new project
2. 建立类
3. 建立testcase
放弃旧的断言,使用hamcrest断言
1. assertThat
2. 使用hamcrest的匹配方法
a) 更自然
3. 示例
a) assertThat( n, allOf( greaterThan(1), lessThan(15) ) );
assertThat( n, anyOf( greaterThan(16), lessThan(8) ) );
assertThat( n, anything() );
assertThat( str, is( "bjsxt" ) );a
assertThat( str, not( "bjxxt" ) );b) assertThat( str, containsString( "bjsxt" ) );
assertThat( str, endsWith("bjsxt" ) );
assertThat( str, startsWith( "bjsxt" ) );
assertThat( n, equalTo( nExpected ) );
assertThat( str, equalToIgnoringCase( "bjsxt" ) );
assertThat( str, equalToIgnoringWhiteSpace( "bjsxt" ) );c) assertThat( d, closeTo( 3.0, 0.3 ) );
assertThat( d, greaterThan(3.0) );
assertThat( d, lessThan (10.0) );
assertThat( d, greaterThanOrEqualTo (5.0) );
assertThat( d, lessThanOrEqualTo (16.0) );
d) assertThat( map, hasEntry( "bjsxt", "bjsxt" ) );
assertThat( iterable, hasItem ( "bjsxt" ) );
assertThat( map, hasKey ( "bjsxt" ) );
assertThat( map, hasValue ( "bjsxt" ) );Failure和Error
1. Failure是指测试失败
2. Error是指测试程序本身出错
JUnit4 Annotation
1. @Test: 测试方法
a) (expected=XXException.class)
b) (timeout=xxx)
2. @Ignore: 被忽略的测试方法
3. @Before: 每一个测试方法之前运行
4. @After: 每一个测试方法之后运行
5. @BeforeClass: 所有测试开始之前运行
6. @AfterClass: 所有测试结束之后运行
运行多个测试
注意
1. 遵守约定,比如:
a) 类放在test包中
b) 类名用XXXTest结尾
c) 方法用testMethod命名
其他框架
TestNG
写了个类,要给别人用,会不会有bug?怎么办?测试一下。
用main方法测试好不好?不好!
1. 不能一起运行!
2. 大多数情况下需要人为的观察输出确定是否正确
为什么要进行单元测试
重用测试,应付将来的实现的变化。
提高士气,明确知道我的东西是没问题的。
JUnit4 HelloWorld
1. new project
2. 建立类
3. 建立testcase
放弃旧的断言,使用hamcrest断言
1. assertThat
2. 使用hamcrest的匹配方法
a) 更自然
3. 示例
a) assertThat( n, allOf( greaterThan(1), lessThan(15) ) );
assertThat( n, anyOf( greaterThan(16), lessThan(8) ) );
assertThat( n, anything() );
assertThat( str, is( "bjsxt" ) );a
assertThat( str, not( "bjxxt" ) );b) assertThat( str, containsString( "bjsxt" ) );
assertThat( str, endsWith("bjsxt" ) );
assertThat( str, startsWith( "bjsxt" ) );
assertThat( n, equalTo( nExpected ) );
assertThat( str, equalToIgnoringCase( "bjsxt" ) );
assertThat( str, equalToIgnoringWhiteSpace( "bjsxt" ) );c) assertThat( d, closeTo( 3.0, 0.3 ) );
assertThat( d, greaterThan(3.0) );
assertThat( d, lessThan (10.0) );
assertThat( d, greaterThanOrEqualTo (5.0) );
assertThat( d, lessThanOrEqualTo (16.0) );
d) assertThat( map, hasEntry( "bjsxt", "bjsxt" ) );
assertThat( iterable, hasItem ( "bjsxt" ) );
assertThat( map, hasKey ( "bjsxt" ) );
assertThat( map, hasValue ( "bjsxt" ) );Failure和Error
1. Failure是指测试失败
2. Error是指测试程序本身出错
JUnit4 Annotation
1. @Test: 测试方法
a) (expected=XXException.class)
b) (timeout=xxx)
2. @Ignore: 被忽略的测试方法
3. @Before: 每一个测试方法之前运行
4. @After: 每一个测试方法之后运行
5. @BeforeClass: 所有测试开始之前运行
6. @AfterClass: 所有测试结束之后运行
运行多个测试
注意
1. 遵守约定,比如:
a) 类放在test包中
b) 类名用XXXTest结尾
c) 方法用testMethod命名
其他框架
TestNG
0 0
- JUnit4
- JUnit4
- junit4
- junit4
- JUnit4
- junit4
- JUnit4
- JUNIT4
- Junit4
- JUnit4
- junit4
- Junit4
- Junit4
- Junit4
- JUnit4
- JUnit4
- junit4
- JUnit4
- 0-1背包Big Event in HDU
- 关于SVN+sersync实时同步到远程服务器的思路
- 数据库(oracle)表解锁
- CF #278 div2 D Strip 线段树+优先队列
- poj3260 混合背包
- Junit4
- 导出、导入模块:《二 -- 针对【文件夹节点】》
- nyoj 21 三个水杯
- Hduoj1210【数学】
- UML关系图
- 一、类、对象、传值引用、$this关键字、静态属性和静态方法、self、构造方法、析构方法
- gcc/g++使用第三方库时添加头文件路径和库文件路径的方法
- JAVA 简单clone和深度clone(理论部分借鉴)
- 应用截图