单元测试{写了个类,,要给别人用,需要进行单元测试}
单元测试的原因: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命名
==================================================================
 
                   
                   
                   
                   
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   316
					316
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            