QQ群:136924235
论坛:http://bbs.shareku.com
webdriver可以结合junit中的Parameterized运行器完成数据驱动的目的,数据驱动的方式很多,可以结合csv文件,excel文件,jdbc等,下面我将结合csv,jdbc来展示如何完成数据驱动测试。一、先给大家介绍一下如何使用Parameterized运行器,两种方式实现。
1、第一种方式,通过构造方法初始化测试数据
代码示例:
package junit.parameters;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays;
import java.util.Collection;
@RunWith(value = Parameterized.class)
public class ParamNoConstructor {
@Parameter(value = 0)
// value 的值指定该属性对应getData返回数组中的序数
public String username; // 此处须声明为public
@Parameter(value = 1)
public String password;
@Parameters
public static Collection getData() {
return Arrays.asList(new String[][] { { "ray", "123" }, { "venus", "123" },{ "jupiter", "123" }, { "mercury", "123" } });
}
@Test
public void test() {
System.out.println(this.username + " " + this.password);
}
}
通过以上两个例子大家应该对Parameterized有了充分的认识
二、使用csv文件完成数据驱动
首先,我们定义一个DBUtils的数据驱动类,并实现csv驱动的方法,代码示例如下:
csv文件路径:src/test/resources/userInfo.csv,该文件中的文本内容如下:
username,password
ray,123
venus,456
jupiter,789
mercury,369
mars,258
saturn,147
DBUtils类:
package junit.parameters;
import org.apache.commons.lang3.StringUtils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class DBUtils {
@SuppressWarnings("resource")
public static List driverCSV(String path) {
List list = new ArrayList();
InputStream in = null;
BufferedReader buf = null;
try {
in = new FileInputStream(path);
buf = new BufferedReader(new InputStreamReader(in));
String temp;
String info[];
int count = 0;
while (!(temp = StringUtils.trimToEmpty(buf.readLine())).equals("") ) {
if (count == 0) {
count = 1;
continue;
}
info = temp.split(",");
list.add(info);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
测试类:
package junit.parameters;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.Collection;
import junit.parameters.DBUtils;
@RunWith(value = Parameterized.class)
public class TestCSVDriver {
private String username;
private String password;
public TestCSVDriver(String username, String password) {
this.username = username;
this.password = password;
}
@Parameters
public static Collection getData() {
return DBUtils.driverCSV("src/test/resources/userInfo.csv");
}
@Test
public void test() {
System.out.println(this.username + " " + this.password);
}
}
三、使用JDBC完成数据驱动测试
这里我们假定一个User的数据库,数据库中有名为t_user的用户信息表,表中的内容与以上csv文件中定义的相同
1、首先配置mysql-connector-java-*.jar到环境变量中
2、
DBUtils类:
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DBUtils {
public static List driverJDBC(String URL, String user, String password) {
List list = new ArrayList();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select username,password from t_user where id<=?";
try {
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection(URL, user, password);
ps = con.prepareStatement(sql);
ps.setInt(1, 5);
rs = ps.executeQuery();
while (rs.next()) {
String[] tmp = new String[2];
tmp[0] = rs.getString("username");
tmp[1] = rs.getString("password"); list.add(tmp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close(); ps.close(); con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
测试类:
package junit.parameters;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.Collection;
import junit.parameters.DBUtils;
@RunWith(value = Parameterized.class)
public class TestJDBCDriver {
private String username;
private String password;
public TestJDBCDriver(String username, String password) {
this.username = username;
this.password = password;
}
@Parameters
public static Collection getData() {
return DBUtils.driverJDBC("jdbc:mysql://localhost:3306/User", "ray","123456");
}
@Test
public void test() {
System.out.println(this.username + " " + this.password);
}
}