JUnit是一个开放源代码的Java测试框架,用于编写和运行可重复的测试。他是用于单元测试框架体系xUnit的一个实例(用于java语言)。它包括以下特性:
1、用于测试期望结果的断言(Assertion)
2、用于共享共同测试数据的测试工具
3、用于方便的组织和运行测试的测试套件
4、图形和文本的测试运行器
public class peron {
public void run(){
System.out.println("run");
}
public void eat(){
System.out.println("eat");
}
}
package test1;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class demo2 {
//在每一个测试方法运行前运行
@Before
public void before(){
//写入初始化方法
System.out.println("before");
}
@Test
public void testrun(){
peron p = new peron();
p.run();
}
@Test
public void testeat(){
peron p = new peron();
p.eat();
}
//在每一个测试方法运行后运行
@After
public void after(){
System.out.println("after");
}
}
package test1;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class demo3 {
//在测试方法运行前运行
@BeforeClass
public static void beforeclass(){
//写入初始化方法
System.out.println("beforeclass");
}
@Test
public void testrun(){
peron p = new peron();
p.run();
}
@Test
public void testeat(){
peron p = new peron();
p.eat();
}
//在测试方法运行后运行
@AfterClass
public static void afterclass(){
System.out.println("afterclass");
}
}
可以实现断言功能,将程序放出来的结果与期望的结果做比较
提供了很多方法断言功能
package test1;
public class peron {
public String run(){
System.out.println("run");
return "1";
}
public void eat(){
System.out.println("eat");
}
}
package test1;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class demo2 {
//在每一个测试方法运行前运行
@Before
public void before(){
//写入初始化方法
System.out.println("before");
}
@Test
public void testrun(){
peron p = new peron();
//断言
Assert.assertEquals("1", p.run());
}
@Test
public void testeat(){
peron p = new peron();
p.eat();
}
//在每一个测试方法运行后运行
@After
public void after(){
System.out.println("after");
}
}