Junit 测试基础

/**
 * 1、测试函数以     @Test  注解, 函数名可以根据测试内容自定义但返回值必须是void,不能有参数
 * 2、assertEquals(arg0,arg1); 用来判断期待值是否和实际结果相等, 第一个参数 写期待结果,第二个参数写实际结果。juni会自动对比返回测试结果
 * 3、@Ignore 表示忽略此函数  一般在写程序之前我们应该做好规划,即哪个程序是干什莫的,如果测试时 程序还没写好  可以先用个@Ignore注解,测试时结果会提示你有几个测试被忽略,在程序写好后只需把注解去掉,进行测试便可以
 * 4、 fixture(固定代码段)就是在某些阶段必然被调用的代码  可以做一些初始化工作  或者选择 将 测试类 进行初始化  来防止 测试之间的相互影响  如果在计算器中有静态变量 res我们就可以通过 @before或@after 把res清零
 * 5、@BeforeClass 和 @AfterClass 如果在测试一个类对较大的文件进行操作,测试每个方法都对文件进行读取,将会耗费大量时间,显然不切实际。这时我们希望在所有测试之前读一次文件,在所有测试结束后是放文件,这时便可以使用@BeforeClass和 @AfterClass  每个测试类只能有一个方法被标注为@BeforeClass 或 @AfterClass,并且该方法必须是Public和Static的。
 * 6、@Test(timeout=1000)限时测试   在函数中我们会用到循环,但是如果失误出现死循环  那这个测试会花费大量时间而得不到结果, 为了避免这种情况 我们可以对函数测试进行限时,超时后强行中止次方法的测试
 * 7、@Test(expected = ArithmeticException.class)测试异常,java中常会编写函数有异常抛出,如果你觉得一个函数应该抛出异常,但是没有抛出,显然需要被检测到。我们需要使用@Test的excepted属性,将我们要检验的异常值传递给她,这样junit就能自动帮我们检测是否抛出异常
 * 8、@RunWith(TestClassRunner.class)(运行器用来修饰类而不是函数) 当我们将代码交给Junit之后框架如何来运行你的代码————通过Runner  在Junit中有许多Runner 负责调用你的测试代码, Junit 中有默认的Runner 在特殊情况下我们需要调用 特定的Runner
 * 9、 @RunWith(Parameterized.class)  指定此运行器可以进行参数化测试 对于每个方法的测试我们通常会使用多组数据来测试,  这时我们不需要测试一组修改一下代码 ,也不需要创建多个测试类 只需要改变  Runner
 * 10、@RunWith(Suite.class) 打包测试   在一个项目中只写一个测试类是不可能的,我们会写很多的测试类,可这些测试类必须一个一个的执行,也是比较麻烦的事。鉴于此,junit为我们提供了打包测试的功能,将所有需要运行的测试类集中起来,一次性的运行完毕,大大方便了我们的测试工作。
 */

编写calculator1

1 public class Calculator1 {
2  public int add(int n1,int n2){
3     return n1+n2;
4 }
5 public int minus(int n1 ,int n2) {
6     return n1-n2;
7 }
8 }

 编写calculator1的测试类

 1 package junittest;
 2 /**
 3  * import static org.junit.Assert.*;
 4  * 采用静态导入 
 5  * 检测结果用的assertEquals(4,res);函数是来自于类Assert的静态方法
 6  * 使用静态导入之后在调用时就不必使用Assert.assertEquals(4,res);
 7  */
 8 import static org.junit.Assert.*;
 9 
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Ignore;
13 import org.junit.Test;
14 public class Calculator1Test {
15     public Calculator1 calculator = new Calculator1();
16 /**
17  * 在测试方法之前执行
18  * @throws Exception
19  */
20     @Before
21     public void setUp() throws Exception {
22         System.out.println("Calculator1 方法测试开始");
23     }
24 /**
25  * 在测试方法之后执行
26  * @throws Exception
27  */
28     @After
29     public void tearDown() throws Exception {
30         System.out.println("Calculator1 方法测试结束");
31     }
32 /**
33  * 测试Add 函数名可以自定义但返回值必须是void,不能有参数  (junit4 新加入)
34  */
35     @Test
36     public void Add() {
37         int res = calculator.add(2, 3);
38         assertEquals(4,res);
39     }
40     /**
41      * 此方法尚未写好 标记@Ignore
42      */
43     @Test
44     @Ignore
45     public void Minus() {        
46 
47     }
48 
49 }

 编写calculator2

 1 import org.junit.Test;
 2 
 3 public class Calculator2 {
 4     @Test(timeout = 1000)
 5 public void mult(int n1, int n2){
 6     for(;;);
 7 }
 8 public int divi(int n1, int n2){    
 9     return n1/n2;
10 } 
11 }

 编写calculator2测试类

 1 package junittest;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.junit.AfterClass;
 6 import org.junit.BeforeClass;
 7 import org.junit.Test;
 8 
 9 public class Calculator2Test {
10 public Calculator2 calculator2 = new Calculator2();
11     /**
12      * 标记在整个测试类前执行
13      * @throws Exception
14      */
15     @BeforeClass
16     public static void setUpBeforeClass() throws Exception {
17         System.out.println("Calculator2测试开始");
18     }
19     /**
20      * 标记在整个测试类后执行
21      * @throws Exception
22      */
23     @AfterClass
24     public static void tearDownAfterClass() throws Exception {
25         System.out.println("Calculator2测试结束");
26     }
27     /**
28      * 内置死循环  会超时 超时测试会失败
29      */
30     @Test(timeout = 1000)
31     public void testMult() {
32         calculator2.mult(1, 2);
33     }
34     /**
35      * 除数不能为0 预期抛出错误  如果不抛出错误 测试失败
36      */
37     @Test(expected = ArithmeticException.class)
38     public void testDivi() {
39         calculator2.divi(5, 0);
40     }
41 }

编写打包测试类

1 import org.junit.runner.RunWith;
2 import org.junit.runners.Suite;
3 import org.junit.runners.Suite.SuiteClasses;
4 
5 @RunWith(Suite.class)
6 @Suite.SuiteClasses({Calculator1Test.class,Calculator2Test.class})
7 public class AllTest {
8 
9 }

 编写参数化测试用例:

 1 import static org.junit.Assert.*;
 2 
 3 import java.util.Arrays;
 4 import java.util.Collection;
 5 
 6 import junittest.Calculator1;
 7 
 8 import org.junit.Test;
 9 import org.junit.runner.RunWith;
10 import org.junit.runners.Parameterized;
11 import org.junit.runners.Parameterized.Parameters;
12 /**
13  *     参数化测试
14  * @author Administrator
15  *
16  */
17 @RunWith(Parameterized.class)
18 public class Parametrictest {
19 public Parametrictest(int n1, int n2, int result) {
20         super();
21         this.n1 = n1;
22         this.n2 = n2;
23         this.result = result;
24     }
25 private static Calculator1 calculator1 = new Calculator1();
26 private int n1;
27 private int n2;
28 private int result;
29 @Parameters
30 public static Collection data(){
31     return Arrays.asList( new Object[][]{
32             {2,4,6},
33             {0,0,0},
34             {-3,9,6}
35     });
36 }
37 /**
38  * 以add为例进行测试
39  */
40 @Test
41 public void add(){
42     int res = calculator1.add(n1, n2);
43     assertEquals(result,res);
44     }
45 }
原文地址:https://www.cnblogs.com/the-wang/p/7732334.html