您的位置:首页 > 其它

JUnit测试框架

2013-05-15 18:01 369 查看
------- android培训java培训、期待与您交流!
----------
JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了。

public class Person
{
public void eat()
{
System.out.println("eat");
}
public void run()
{
System.out.println("run");
}
}


@Test 测试功能的方法

@Before 关联资源和创建资源的方法

@After 关闭和释放资源的方法

@Before和@After方法在 每一个Test运行时,@Before先运行和@After后运行

public class Demo0
{
Person p =null;
@Before
public void Before()//创建资源
{
p = new Person();
System.out.println("new Person");
}
//测试功能
@Test
public void testEat()
{
p.eat();
}
@Test
public void testRun()
{
p.run();
}
//释放资源
@After
public void After()
{
p = null;
System.out.println("p=null");
}
}


@BeforeClass
和Befoer相类似,不过是静态,类加载时即运行

@AfterClass和After相类似,不过也是静态的,类释放时同时运行。

public class Demo01
{
static Person p =null;
@BeforeClass
public static void Before()//创建资源
{
p = new Person();
System.out.println("new Person");
}
//测试功能
@Test
public void testEat()
{
p.eat();
}
@Test
public void testRun()
{
p.run();
}
//释放资源
@AfterClass
public static void After()
{
p = null;
System.out.println("p=null");
}
}


断言:Assert类(用于判断实际值是否是期望值,在JUnit小窗口显示)

static Assert.assertXXX(experctrd,actual)

actual通常是调用某个方法的返回值与expected来进行比较。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: