Junit的入门知识

真正编程有一段时间了,但是一直都没有想过去学习Junit,也没有想过它的真正用处是什么。最近进入公司,觉得有必要去看看这些大路边上的知识了,看了一下尚学堂的视频。

什么是单元测试

Junit是Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了。

写了个类,要给别人用,会不会有bug?怎么办?

答:测试一下。

main方法测试好不好?

答:不好!

1. 不能一起运行!

2. 大多数情况下需要人为的观察输出确定是否正确

为什么要进行单元测试

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

 

JUnit4 Student(用Myeclipse编码)

1. new project

建立一个普通的工程项目。

2. 建立普通类(new ---->class<Student>,给它两个方法getName 和 getAge )

package com.huangwen.junit4;

public class Student {
 
 public String getName(){
  return "Tom";
 }
 
 public int getAge(){
  return 18; 
 }
 
 public static void main(String[] args){
  String stuName = new Student().getName();
  int stuAge = new Student().getAge();
  System.out.println(stuName + " " + stuAge);
 }
}

 

3. 建立testcase

 3.1 新建一个Test包( com.hwsdfg.junit4.test),

 3.2 建立TTest测试类

      3.2.1 new---->JUnit Test Case

       3.2.2 在添加页面中  选中的是JUnit 4,并且在class under test中找到自己需要的测试类,以及方法<选择了两个方法>(手动输入的好),然后就自动生成了一个类和方法。

package com.huangwen.junit4.test;

import static org.junit.Assert.*;

import org.junit.Test;

import com.huangwen.junit4.Student;

public class StudentTest {

 @Test
 public void testGetName() {
  fail("Not yet implemented");
 }

 @Test
 public void testGetAge() {
  fail("Not yet implemented");
 }

}


4.修改需要测试的方法

package com.huangwen.junit4.test;

import static org.junit.Assert.*;

import org.junit.Test;

import com.huangwen.junit4.Student;


public class StudentTest {

 @Test
 public void testGetName() {
  String z = new Student().getName();
//  assertEquals(expected, actual);
  assertEquals("Tom", z);
 }

 @Test
 public void testGetAge() {
  int age = new Student().getAge();
//  assertEquals(expected, actual);
  assertEquals(18, age);
 }

}


5.测试(右键 StudentTest类,Run As  --> Junit Test )

查看结果,如果是绿色的就说明正确。

 

放弃旧的断言,使用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" ) );
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" ) );

 

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命名

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值