JUNIT 入门 笔记



单元测试{写了个类,,要给别人用,需要进行单元测试}

单元测试的原因:1)增强士气 2)确保类是正确的 3)后期维护花的成本占60%,单元测试可以降低后期的成本

1.用main方法做测试{不好}

package com.linux_lihuafeng.junit;

public class T {

 public int add(int x, int y)  {
  
  return x + y ;
 }
 
 //用main方法进行测试,,这种方法不好,,比如{需要测试多个函数时,,需要写多个main方法}
 public static void main(String[] args) {
  int z = new T().add(3, 5);
  System.out.println(z);
 }
}


=================================================================

package com.linux_lihuafeng.junit.test;

//静态引用,,将Assert类的方法引入进来
import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Test;

import com.linux_lihuafeng.junit.T;

public class TTest {

 @Test
 public void testAdd() {
  int z = new T().add(5 , 3);
  assertEquals(8,z);
 }

}

=================================================================

package com.linux_lihuafeng.junit.test;

//静态引用,,将Assert类的方法引入进来
import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Test;

import com.linux_lihuafeng.junit.T;

public class TTest {

 @Test
 public void testAdd() {
  int z = new T().add(5 , 3);
  assertEquals(8,z);
  assertTrue(4 > 3);
  assertTrue("z too small", z > 10);
 }

}


=================================================================


package com.linux_lihuafeng.junit.test;

//静态引用,,将Assert类的方法引入进来
import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Test;

import com.linux_lihuafeng.junit.T;

public class TTest {

 @Test
 public void testAdd() {
  int z = new T().add(5 , 3);
  assertEquals(8,z);
  assertTrue(4 > 3);
  assertTrue("z too small", z > 10);
 }

 //timeout=100要求100ms内结束
 @Test(expected=java.lang.ArithmeticException.class, timeout=100)
 public void testDivide() {
  double z =  new T().divide(8, 0);
  assertThat(z,is(4));
 }
}


=================================================================

@Ignore


=================================================================

package com.linux_lihuafeng.junit.test;

//静态引用,,将Assert类的方法引入进来
import static org.junit.Assert.*;

import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.linux_lihuafeng.junit.T;

public class TTest {

 //用于当需要搭建比较耗时间的程序时
 @BeforeClass
 public static void beforeClass() {
  System.out.println("BeforeClass");
 }
 
 
 //在所有测试函数执行之前执行
 @Before
 public void before() {
  System.out.println("before");
 }
 @Test
 public void testAdd() {
  int z = new T().add(5 , 3);
  assertEquals(8,z);
  assertTrue(4 > 3);
  assertTrue("z too small", z > 10);
 }

 @Test(expected=java.lang.ArithmeticException.class)
 public void testDivide() {
  double z =  new T().divide(8, 0);
  assertThat(z,is(4));
 }
 

 //在所有测试函数执行之后执行
 @After
 public void after() {
  System.out.println("after");
 }
 
 //用于当需要卸载掉环境时
 @AfterClass
 public static void afterClass() {
  System.out.println("BeforeClass");
 }
}


=================================================================


package com.linux_lihuafeng.junit.test;

import static org.junit.Assert.*;

import static org.hamcrest.Matchers.*;

import org.junit.Test;

public class UserTest {

 @Test
 public void testGetName() {

  assertThat(new User().getName(), is("linux_lihuafeng"));
 }

}


=================================================================

1.遵守规则,比如:
 a) 类放在test包中
 b) 类名用XXXTest结尾
 c) 方法用testMethod命名

==================================================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值