junit 学习(4)--参数化

来源:互联网 发布:mac开机启动项 编辑:程序博客网 时间:2024/04/29 12:44

 有关junit的参数化问题,我们通过学习都知道使用注解@Parameters来完成,可是我们用过类似qtp这样的工具后,就想能不能通过读取文件来实现参数化了。在网络上学了一些,但好像还不是很明白。我想按三种方法记录下来吧。

 ======Junit的基本参数化=======

 使用注解@Parameters

  • (1)为准备使用参数化测试的测试类指定特殊的运行器 org.junit.runners.Parameterized。
  • (2)为测试类声明几个变量,分别用于存放期望值和测试所用数据。
  • (3)为测试类声明一个带有参数的公共构造函数,并在其中为第二个环节中声明的几个变量赋值。
  • (4)为测试类声明一个使用注解 org.junit.runners.Parameterized.Parameters 修饰的,返回值为 java.util.Collection 的公共静态方法,并在此方法中初始化所有需要测试的参数对。
  • (5)编写测试方法,使用定义的变量作为参数进行测试。 

 JDemo.java 待测试类 public class JDemo  {
 int a;
 int b;
 int result;
 
 public int add(int a, int b){
  result = a+b;
  return result;
 }
}
 JDemoTest.java 测试用例

 import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

//(1)为准备使用参数化测试的测试类指定特殊的运行器 org.junit.runners.Parameterized。
@RunWith(Parameterized.class)
public class JDemoTest {   
//(2)为测试类声明几个变量,分别用于存放期望值和测试所用数据。 
 int result;
 int adddata_a;
 int adddata_b;
 
 //(3)为测试类声明一个带有参数的公共构造函数,并在其中为第二个环节中声明的几个变量赋值。 
 public JDemoTest(int result,int adddata_a,int adddata_b){
  this.adddata_a= adddata_a;
  this.adddata_b= adddata_b;
  this.result= result;
 }
 //(4)为测试类声明一个使用注解 org.junit.runners.Parameterized.Parameters 修饰的,返回值为 java.util.Collection 的公共静态方法,并在此方法中初始化所有需要测试的参数对。
 @Parameters
 public static Collection multipleValues(){
  return Arrays.asList(new Object[][]{
    {6,3,3},
    {7,3,4},
  });
 }
 
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }

 @AfterClass
 public static void tearDownAfterClass() throws Exception {
 }
 
 @Before
 public void before(){
 }

 @After
 public void after(){
 }
 @Test()
 public void testadd(){
  JDemo a = new JDemo();
  //assertEquals(6,a.add(3, 3));
  assertEquals(result,a.add(adddata_a,adddata_b));

  System.out.println(result+" "+adddata_a+" "+adddata_b);
  System.out.println("======");
 }
}


 运行结果

junit 学习(4) - 流口水的小猪 - 轨迹
 
根据运行结果,看出确实是运行了两次。但显然这样的测试数据和代码混在一起,对后期的测试不里,如果修改测试数据,就等于修改了代码,需要将测试数据分离出来。
 
=======junit的参数化--读配置文件=========(目前看来这个方法不太可行)
 JDemoTest.java

 import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;


public class JDemoTest {
   

//===========
 private static String PROPERTIES_FILE = "setting.properties";
 private static Properties props = null;

 private void initProperties() {
 // String path = JDemoTest.class.getResource("").getPath();
  String path = "C:\\";       //读取C:\setting.properties 文件
  path = path + PROPERTIES_FILE;
  System.out.print(path);
  props = new Properties();
  try {
   props.load(new FileInputStream(new File(path)));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 private String getConfigParam(String key) {
  return props.getProperty(key);
 }
 //===========
 
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
  
 }

 @AfterClass
 public static void tearDownAfterClass() throws Exception {
 }

 @Test
 public void testAdd() {
  //----------------
   if (props == null) {
      initProperties();
     }
  //----------------
   for (int i=1;i<3;i++){
  JDemo a= new JDemo();
  assertEquals(Integer.parseInt(getConfigParam("expected_"+i)),a.add(3, 3));
  
   }
 }

}

   
 
=======junit的参数化--读参数文件=========(这个方法应该才是我想要的方法,但目前还没有看到,记录一下,以后再看)
http://touchfu.iteye.com/blog/732930
 


 =======

今天又在网上找有关junit参数化的问题,发现junit在参数化方面做的确实不是很理想。根据分析,junit关注于独立的单元测试,单元测试对参数的变化要求不是很大,所以才这样的吧。同时发现网络上的TestNG,这个对参数支持的不错。并且后期selenium的参数化也要利用TestNG来完成。

经过这么一折腾,算是明白了junit参数化的问题了。当然我这里的参数化是指代码和参数的分离式的参数化。