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

来源:互联网 发布:服装排版软件排名 编辑:程序博客网 时间:2024/06/11 04:36
JUnit4相比junit3有了很大的改善,书写方便,灵活的Anotation控制测试流程,我这里描述的是junit4中对于测试来说最基本也是最常用的一些功能,帮助大家快速掌握junit的使用。
Junit网址:http://junit.sourceforge.net/javadoc/

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 {

}


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

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

如在类上方加上:@FixMethodOrder(value=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 OrderedRunner extends BlockJUnit4ClassRunner {     
    public OrderedRunner(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 f1, FrameworkMethod f2){   
                Order o1 = f1.getAnnotation(Order.class);               
                Order o2 = f2.getAnnotation(Order.class);                  
                if (o1 == null || o2 == null)                     
                    return -1;                  
                return o1.order() - o2.order();             
                }
            });         
        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");
    } 
}

到此为止,基本上这些东西基本能满足我们自动化测试的使用了

阅读全文
0 0