Junit

来自单元测试学习文档

为什么要进行单元测试

重用测试,应付将来的实现的变化。

提高士气,明确知道我的东西是没问题的。

JUnit4 HelloWorld

  1. new project
  2. 建立类
  3. 建立testcase
  4. assertThat
  5. 使用hamcrest的匹配方法

放弃旧的断言,使用hamcrest断言

a) 更自然

  1. 示例

a) assertThat( n, allOf( greaterThan(1), lessThan(15) ) );
assertThat( n, anyOf( greaterThan(16), lessThan(8) ) );
assertThat( n, anything() );
assertThat( str, is( "bjsxt" ) );
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" ) );

FailureError

  1. Failure是指测试失败
  2. Error是指测试程序本身出错
  3. @Test: 测试方法

JUnit4 Annotation

a) (expected=XXException.class)

b) (timeout=xxx)

  1. @Ignore: 被忽略的测试方法
  2. @Before: 每一个测试方法之前运行
  3. @After: 每一个测试方法之后运行
  4. @BeforeClass: 所有测试开始之前运行
  5. @AfterClass: 所有测试结束之后运行
  6. 遵守约定,比如:

运行多个测试

注意

a) 类放在test包中

b) 类名用XXXTest结尾

c) 方法用testMethod命名

其他框架

TestNG

转载于:https://www.cnblogs.com/dfguo/p/6888706.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值