上一篇我们介绍用如何用yaml结合testng做数据驱动,就又想来个数据库的参数化
备注:@DataProvider的返回值类型只能是Object[][]与Iterator<Object>[]
思路:
解析sql查询返回结果,然后把解析出来的list转换成Object[][]类型的数据,且结合在@DataProvider中。
然后把解析出来的list转换成Object[][]类型的数据,且结合在@DataProvider中。
DbDataHeleper.java
import org.testng.annotations.DataProvider;import java.sql.*;import java.util.*;/** * 数据库操作工具 * * @author longrong.lang */public class DbDataHeleper { static Connection conn = null; public static String driverClassName = "com.mysql.jdbc.Driver"; public static String url = "jdbc:mysql://127.0.0.1:3306/demo"; public static String username = "root"; public static String password = "root"; /** * 执行sql * * @param jdbcUrl 数据库配置连接 * @param sql sql语句 * @return */ public static List
再通过测试文件来测试一下:
TestDbData.java
import org.testng.annotations.Test;import java.util.Map;public class TestDbData extends DbDataHeleper { @Test(dataProvider = "dbDataMethod") public void testmethod1(Map param) { System.out.println(param.get("username") + "\t" + param.get("passWord") + "\t" + param.get("remark")); }}
运行效果:
[TestNG] Running: C:\Users\Administrator\.IntelliJIdea2018.2\system\temp-testng-customsuite.xmlrongrong 123456 3xiaoqiang 123654 4gates 112121 1gates 112121 2===============================================Default SuiteTotal tests run: 4, Failures: 0, Skips: 0===============================================Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8Process finished with exit code 0