java結合testng,利用mysql數據庫做數據源的數據驅動實例


上一篇我們介紹用如何用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<Map<String, String>> getDataList(String jdbcUrl, String sql) {
        List<Map<String, String>> paramList = new ArrayList<Map<String, String>>();
        Map<String, String> param = new HashMap<>();
        Statement stmt = null;
        try {
            // 注冊 JDBC 驅動
            Class.forName(driverClassName);
            // 打開鏈接
            conn = DriverManager.getConnection(jdbcUrl, username, password);
            // 執行查詢
            stmt = conn.createStatement();
            ResultSet rs = null;
            rs = stmt.executeQuery(sql);
            String columns[] = {"username", "passWord", "remark"};
            // 展開結果集數據庫
            while (rs.next()) {
                Map<String, String> map = new LinkedHashMap<String, String>();
                for (int i = 0; i < columns.length; i++) {
                    String cellData = rs.getString(columns[i]);
                    map.put(columns[i], cellData);
                }
                paramList.add(map);
            }
            // 完成后關閉
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            // 處理 JDBC 錯誤
            System.out.println("處理 JDBC 錯誤!");
        } catch (Exception e) {
            // 處理 Class.forName 錯誤
            System.out.println("處理 Class.forName 錯誤");
        } finally {
            // 關閉資源
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
        return paramList;
    }

    @DataProvider
    public Object[][] dbDataMethod() {
        String sql = "SELECT * FROM `account`;";
        List<Map<String, String>> result = getDataList(url, sql);
        Object[][] files = new Object[result.size()][];
        for (int i = 0; i < result.size(); i++) {
            files[i] = new Object[]{result.get(i)};
        }
        return files;
    }
}

再通過測試文件來測試一下:

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.xml
rongrong    123456    3
xiaoqiang    123654    4
gates    112121    1
gates    112121    2

===============================================
Default Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================

Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8

Process finished with exit code 0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM