1 Junit——用于编写运行可重复发自动化测试的开源测试框架 2 这里用Eclipse IDE 3 其实无论是Eclipse仍是Intellij IDEA都是差很少的 4
5 使用步骤: 6 1.new a project,then 7 Build Path 8 Add Library 9 Junit 4
10
11 2.编写好将要测试的类及方法 12 3.建立另外一个类,用于测试(通常都须要建一个和src并行的文件夹,由于不要把测试文件和源文件放在一块儿) 13
14 测试类注解介绍 15 @Test+方法: 16 指示一个测试用例 17
18 @Before+方法: 19 表示该方法必须在每一个测试以前运行,一遍测试某些先决条件(注意:每一个测试用例以前) 20
21 @BeforeClass+方法: 22 指示附着的静态方法,在全部的测试以前必须运行一次,发生这种状况时通常是测试计算共享配置方法如链接到数据库(注意:是整个类执行以后) 23
24 @After+方法: 25 测试后执行的内容,好比重置某些必要的变量、删除某些变量等必要操做。(注意:每一个测试用例以后) 26
27 @AfterClass+方法: 28 在全部测试类执行完以后执行,此方法为静态(注意:是整个类执行以后) 29
30 @Ignore+方法: 31 不执行该方法 32
33 注意:必需要将@BeforeClass、@AfterClass声明为静态 34
35
36 须要导入的包: 37 org.junit.*
38 static org.junit.Assert.*; //静态的
39
40
41 断言 42 1.void assertEquals(String 可选message,expectedValue,actualValue) 43 2.void assertTrue(String 可选message,boolean value) 44 void assertFalse(String 可选message,boolean value) 45 3.void assertNotNull(String 可选message,Object o) 46 void assertNull(String 可选message,Object o) 47 4.void assertSame(String 可选message,Object expect,Object actual) 48 void assertNotSame(String 可选message,Object expect,Object actual) 49 5.void assertArrayEquals(String mes,expectedArr, actualArr) 50
51
52
53 套件测试 54 ——捆绑几个单元测试用例而且一块儿执行它们 55 使用注解: 56 @RunWith 57 @Suite(中文:一套) 58
59 使用: 60 1.创建须要捆绑测试的全部测试用例(多个不一样的测试类):Testi(i=1,2,3...) 61 2.创建新类:TestSuite: 62 import org.junit.runner.RunWith; 63 import org.junit.runner.Suite; 64 @RunWith(Suite.class) 65 @Suite.SuiteClass({Test1.class,Test2.class,,,}) 66 //使用{},将须要捆绑的类.class用逗号分隔,造成数组。 67 //注意:上面两条注解是在主类以前的,未在其中。由于它们注释的是类,而不是方法
68 public class Main{ 69 //something
70 } 71
72 package Junit; 73
74 import org.junit.runner.RunWith; 75 import org.junit.runners.Suite; 76
77 @RunWith(Suite.class) 78 @Suite.SuiteClasses({ 79 FuncTest.class, 80 FuncTest2.class, 81 AtExplaination.class
82 }) 83
84 public class SuiteTest { 85 } 86
87
88
89 时间测试 90 若是一个测试用例在指定的毫秒级别时间里为执行完,咱们能够人为认定它为失败 91 ————timeout参数、@Test, 92 即@Test(timeout=n) 93
94 package Junit; 95 import org.junit.Test; 96
97 public class Timeout { 98 @Test(timeout = 100) 99 public void timeTest(){ 100 while (true){ 101
102 } 103 } 104 } 105
106
107
108 异常测试——@Test(expected = AClass.class) 109 eg:@Test(expected=ArithmeticException.class) 110
111
112
113 参数化测试 114 将一系列参数用于一个测试类的测试 115 1.该类注解为@RunWith(Parameterized.class) 116 2.该类有一个构造方法,用于存储测试数据 117 3.该类有一个静态方法生成并返回测试数据,必须有注解@Parameters (注意:必须是静态) 118 4.有一个测试用例@Test 119
120 package Junit; 121
122 import org.junit.Test; 123 import org.junit.runner.RunWith; 124 import org.junit.runners.Parameterized; 125 import static org.junit.Assert.*; 126 import java.util.Arrays; 127 import java.util.Collection; 128
129 @RunWith(Parameterized.class) 130 public class ParameterizedTest { 131 int expected; 132 int first; 133 int second; 134 public ParameterizedTest(int expected,int first,int second){ 135 this.expected =expected; 136 this.first =first; 137 this.second = second; 138 } 139
140 @Parameterized.Parameters 141 public static Collection add(){ 142 return Arrays.asList(new Integer[][]{{5,2,3},{9,4,5},{8,3,5},{1,2,1}});//为何用int不行
143 } 144 @Test 145 public void sum(){ 146 assertEquals(expected,add(first,second)); 147 } 148
149 public static int add(int a,int b){ 150 return a+b; 151 } 152
153 } 154
155
156 测试规则。。。