Eclipse中高效的快捷键、调试及Junit

发表于:2016-6-20 11:13

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:JasonDing    来源:51Testing软件测试网采编

  Junit测试框架
  基本使用
  编写一个新的测试类文件
  在编写的测试方法中添加注解 @Test
  在大纲(Outline)视图中右键点击要测试的方法,运行配置(Runas),对方法进行运行
  如果想对类中所有的方法进行测试,可以点击类进行测试
  比如,要对一个类进行测试
publicclassPerson{
publicvoidrun()
{
System.out.println("run!!");
}
publicvoideat()
{
System.out.println("eat!!");
}
}
  其中测试类如下
importorg.junit.Test;
//Person的测试类
publicclassPersonTest{
@Test
publicvoidtestRun(){
Personp=newPerson();
p.run();
}
@Test
publicvoidtestEat(){
Personp=newPerson();
p.eat();
}
}
  测试类的特殊的方法
@Before、@After
importorg.junit.After;
importorg.junit.Before;
importorg.junit.Test;
//Person的测试类
publicclassPersonTest{
privatePersonp;
@Before
publicvoidbefore()
{
System.out.println("before");
p=newPerson();
}
@Test
publicvoidtestRun(){
p.run();
}
@Test
publicvoidtestEat(){
p.eat();
}
@After
publicvoidafter()
{
System.out.println("after");
}
}
  这里添加了@Before、@After两个特殊的方法,这两种方法在每种方法运行的时候都会先后运行,其用途是,把初始化资源的操作写到@Before中,把释放资源的操作写到@After中。
  其打印结果是
  before
  eat!!
  after
  before
  run!!
  after
  @BeforeClass、@AfterClass
  在两种方法是在类加载和类释放的时候进行设计。
  注意,这里的标注的方法必须是静态的方法。
  断言Assert
  Assert.assertEquals("2",p.run());
  如果这个方法不符合期望的话,那么测试不通过。
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号