您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源單元測(cè)試工具 > DBunit
selenium與dbunit結(jié)合進(jìn)行與數(shù)據(jù)庫(kù)相關(guān)的測(cè)試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/12/23 11:58:37 ] 推薦標(biāo)簽:

    public void AssertionData(String actualTableName, String getActualTableSQL, String XMLFilePath,
            String expectedTableName) throws Exception {
        ITable actualTable = null;
        //從數(shù)據(jù)庫(kù)取得測(cè)試實(shí)際數(shù)據(jù)
        if (null == getActualTableSQL) {
            //從表中取出所有字段
            IDataSet databaseDataSet = this.getConnection().createDataSet();
            actualTable = databaseDataSet.getTable(actualTableName);
        } else {
            //根據(jù)SQL文取得字段
            actualTable = this.getConnection().createQueryTable(actualTableName, getActualTableSQL);
        }
        //從XML取得測(cè)試期望數(shù)據(jù)
        IDataSet expectedDataSet = new FlatXmlDataSet(new File(XMLFilePath));
        ITable expectedTable = expectedDataSet.getTable(expectedTableName);
        //斷言測(cè)試實(shí)際數(shù)據(jù)和測(cè)試期望數(shù)據(jù)
        Assertion.assertEquals(expectedTable, actualTable);
    }

創(chuàng)建測(cè)試用例的測(cè)試腳本過程如下:
(1)       //測(cè)試前,裝載初始數(shù)據(jù)
  dbAction.dbActionBeforeTest("testdata\Initial.xml"); 
(2)       //斷言測(cè)試實(shí)際數(shù)據(jù)和測(cè)試期望數(shù)據(jù)
        String actualTableName = "TSYS_USER";
        String getActualTableSQL = "SELECT USER_ID,USER_NAME,USER_PWD,USER_TYPE,USER_STATUS,
LOCK_STATUS,CREATE_DATE,MODIFY_DATE,PASS_MODIFY_DATE,REMARK,ORG_ID FROM JRESTESTER.TSYS_USER WHERE USER_ID='00001'";
        String XMLFilePath = dbAction.setFilePath("testdata\Expected.xml");
        String expectedTableName = "TSYS_USER";
        dbAction.AssertionData(actualTableName, getActualTableSQL, XMLFilePath, expectedTableName);
(3)      //  測(cè)試后,設(shè)置數(shù)據(jù)庫(kù)數(shù)據(jù)
  dbAction.dbActionAfterTest();

4.其中Initial.xml內(nèi)容為:
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<TSYS_USER USER_ID="00001" USER_NAME="test00001" USER_PWD="7e5096f694565d5549f03a360cdb2a84" USER_TYPE="0" USER_STATUS="0" LOCK_STATUS="1" CREATE_DATE="20110926" MODIFY_DATE="20111008" PASS_MODIFY_DATE="20110928"  REMARK="sdf" ORG_ID="0_000000"/>
</dataset>

Expected.xml內(nèi)容為:
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
  <TSYS_USER USER_ID="00001" USER_NAME="user2" USER_PWD="7e5096f694565d5549f03a360cdb2a84" USER_TYPE="0" USER_STATUS="0" LOCK_STATUS="1" CREATE_DATE="20110926" MODIFY_DATE="20111008" PASS_MODIFY_DATE="20110928" REMARK="sdf" ORG_ID="0_000000"/>
</dataset>
其中TSYS_USER是用到的表的名字,后面的是表中的字段名字。

創(chuàng)建xml文件時(shí),可以把數(shù)據(jù)庫(kù)表里的數(shù)據(jù)導(dǎo)出成該xml:
    public static void main(String[] args) throws Exception{  
        Class.forName("oracle.jdbc.driver.OracleDriver");  
        Connection jdbcConnection = DriverManager.getConnection("jdbcracle:thin192.168.71.52:1521:JRESSTUDIO",
                "jrestester", "jrestester"); 
        IDatabaseConnection connection = new DatabaseConnection(jdbcConnection,"JRESTESTER");  
        QueryDataSet dataSet = new QueryDataSet(connection);  
        //將整個(gè)TSYS_USER表里的數(shù)據(jù)導(dǎo)出到xml文件里  
        //dataSet.addTable("TSYS_USER");  
        //將TSYS_USER表里符合條件的數(shù)據(jù)導(dǎo)出到xml文件里  
        dataSet.addTable("TSYS_USER","select * FROM JRESTESTER.TSYS_USER WHERE USER_ID='00001'");  
        //導(dǎo)出到dbunit.xml文件里  
        FlatXmlDataSet.write(dataSet,new FileOutputStream("dbunit.xml"));  
    }

上一頁12下一頁
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd