源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JUnit单元测试入门必看篇

  • 时间:2022-08-30 04:28 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JUnit单元测试入门必看篇
[b]什么是单元测试[/b] 写了个类,要给别人用,会不会有bug?怎么办?测试一下。 用main方法测试好不好?不好! 不能一起运行! 大多数情况下需要人为的观察输出确定是否正确 [b]为什么要进行单元测试[/b] 重用测试,应付将来的实现的变化。 提高士气,明确知道我的东西是没问题的。 [b]JUnit4 HelloWorld[/b]  需要导入JUnit和hamcrest包 new project 建立类 建立testcase assertThat 使用hamcrest的匹配方法 [b]放弃旧的断言,使用hamcrest断言[/b] [b]a)  [/b]      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]b)    [/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" ) ); [b]c)  [/b]      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) ); [b]d)  [/b]     assertThat( map, hasEntry( "bjsxt", "bjsxt" ) ); assertThat( iterable, hasItem ( "bjsxt" ) ); assertThat( map, hasKey ( "bjsxt" ) ); assertThat( map, hasValue ( "bjsxt" ) ); [b]Failure和Error[/b] Failure是指测试失败 Error是指测试程序本身出错 [b]JUnit4 Annotation[/b] @Test: 测试方法              a)   (expected=XXException.class)               b)   (timeout=xxx)         2.@Ignore: 被忽略的测试方法         3.@Before: 每一个测试方法之前运行         4.@After: 每一个测试方法之后运行         5.@BeforeClass: 所有测试开始之前运行         6.@AfterClass: 所有测试结束之后运行 [b]运行多个测试[/b] [b]注意[/b] 遵守约定,比如:              a) 类放在test包中 b) 类名用XXXTest结尾 c) 方法用testMethod命名 [b]其他框架[/b] TestNG 以上这篇JUnit单元测试入门必看篇就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部