您的位置:首页 > 其它

selenium webdriver 学习总结-JUnit4 入门(三)

2016-06-20 10:57 471 查看
JUnit4相比junit3有了很大的改善,书写方便,灵活的Anotation控制测试流程,我这里描述的是junit4中对于测试来说最基本也是最常用的一些功能,帮助大家快速掌握junit的使用。
1、常用的注解,代码示例:
package test.demo;

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.BeforeClass;

import org.junit.Test;

public class JunitDemo {

 private String str;

 @BeforeClass   //使用该注解的方法,表示在实例化整个类之前执行一次

 public static void beforeClass(){

 }

 @AfterClass    //使用该注解的方法,表示在实例化整个类之后执行一次

 public static void afterClass(){

 }

 @Before      //使用该注解的方法,表示在执行Test方法之前执行一次

 public void setUp(){

 }

 @After      //使用该注解的方法,表示在执行Test方法之后执行一次

 public void tearDown(){

 }

 @Test       //使用该注解的方法,表示要执行的测试方法

 public void test(){}

 @Ignore("this test has not implemented ")

 @Test  //使用Ignore表示此测试方法忽略不执行,也可以指定string消息,指示原因

 public void hello(){}

    @Test(expected=MyException.class)   //Test中的expected选项是期望有一个MyException异常抛出,如果没有抛出执行失败

    public void Demo() throws MyException{

     throw new MyException("hello exception");

    }

    @Test(timeout=5000)   //Test中timeout选项是限时执行,以毫秒为单位,如果超出还没有执行完则报timeout错误

    public void timeout(){

     int i=0;

     while(i < 3){

      try {

    Thread.sleep(1000);

   } catch (InterruptedException e) {

    e.printStackTrace();

   }

      i++;

      System.out.println(str);

     }

    }

}

class MyException extends Exception{

 private static final long serialVersionUID = 1L;

 public MyException(String message){

  super(message);

 }

}

 
2、以上是junit最基本也是最常用的构建测试运行的方法,下边讲解一下,测试中的Runner运行器

我们把测试代码交给Junit框架后,是如何运行你的代码的,其实就是Runner的作用,当我们没有明确指定一个Runner的时候,系统会默认使用一个名叫BlockJUnit4ClassRunner的运行器来驱动测试运行,以上代码等同于

@RunWith(BlockJUnit4ClassRunner.class)

public class JunitDemo{.......}

要想指定一个runner,需要使用@RunWith标注,并把你所指定的Runner作为参数传递给他,另外,@RunWith只能用来修饰类

 

下边我已示例来介绍一个比较常用的运行器,这个运行器来帮助我们完成参数化测试的作用

@RunWith(Parameterized.class) //使用此注解来驱动测试具有参数化功能

public class JunitDemo {

 private String name;

 private String password;

 

 @Parameters   //此注解标注的方法,返回参数化数据,注意返回类型是Collection

 public static Collection data(){

  return Arrays.asList(

     new String[][]{{"name1","passwd1"},{"name2","passwd2"},{"name3","passwd3"}}

    );

 }

 public JunitDemo(String name,String password){  //通过构造方法对参数初始化

  this.name = name;

  this.password = password;

 }

 

 @Test

 public void testParam(){

  System.out.println(this.name+":"+this.password);

 }

}

3、下边我要介绍的是,如何打包指定需要执行测试的类,两种实现方法:

方法一:类似于Junit3的写法

public class TestSuite{

public static Test suite(){

TestSuite suite = new TestSuite("testSuite name");

suite.addTest(new JUnit4TestAdapter(Test1.class));

suite.addTest(new JUnit4TestAdapter(Test2.class));

suite.addTest(new JUnit4TestAdapter(TestSuite.class));

return suite

}

}

方法二:使用annotation

package com.yanxiu;

import org.junit.runner.RunWith;

import org.junit.runners.Suite;

@RunWith(Suite.class)

@Suite.SuiteClasses({
DoHomework.class,
LoginTakeLesson.class

})

public class TestSuit {

}

方法三、使用category进行分类执行测试

代码示例:

1、首先定义两个类别,用于分类的标识,接口、类均可

package com.test.categories;

interface FastTests {}
interface SlowTests {}

2、编写测试类,并使用annotation Category引用以上两个分类

public class A {
@Test
public void a1() {
fail();
}

@Category(SlowTests.class)
@Test
public void a2() {}
}

@Category( { SlowTests.class, FastTests.class })
public class B {
@Test
public void b1() {}
}
3、根据定义的分类,将以上的测试类进行分组,运行测试时,执行此文件即可
@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { A.class, B.class })
public static class SlowTestSuite {}

4、自定义junit测试方法的执行顺序,两种方法:

a、junit4.11以后的版本简单的使用@FixMethodOrder(MethodSorters.NAME_ASCENDING)注解所要执行的类即可,此时的执行顺序是依据测试方法名在英文字母中的顺序而定的

b、自定义排序操作,以代码示例:

1、新建一个 Order接口

package OrderJunitTestMethod;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME) 

public @interface Order {

    public int order(); 

}

2、实现排序操作

package OrderJunitTestMethod;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import org.junit.runners.BlockJUnit4ClassRunner;

import org.junit.runners.model.FrameworkMethod;

import org.junit.runners.model.InitializationError;

public class OrderRunner extends BlockJUnit4ClassRunner {

public OrderRunner(Class<?> klass) throws InitializationError {
super(klass);
}

@Override
protected List<FrameworkMethod> computeTestMethods() {
List<FrameworkMethod> list = super.computeTestMethods();
Collections.sort(list, new Comparator<FrameworkMethod>() {
public int compare(FrameworkMethod o1, FrameworkMethod o2) {
Order o1 = o1.getAnnotation(Order.class);
Order o2 = o2.getAnnotation(Order.class);
if (o1 == null || o2 == null)
return 0;
return o1.value() - o2.value();
}
});
return list;
}

}

3、junit自定义顺序执行示例

package OrderJunitTestMethod;

import org.junit.Test;

import org.junit.runner.RunWith;

@RunWith(OrderedRunner.class) 

public class SomeTest{

    @Test     

    @Order(order=2)

    public void testUpdateArticle(){

        System.out.println("2");

    } 

    @Test     

    @Order(order=1)     

    public void testInsertArticle(){        

        System.out.println("1");

    }      

    @Test     

    @Order(order=3)    

    public void testDeleteArticle(){

        System.out.println("3");

    } 

}

到此为止,基本上这些东西基本能满足我们自动化测试的使用了,有不清楚的地方欢迎加入qq群提问!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: