csv技術測試框架

2021-06-13 04:00:55 字數 3221 閱讀 6333

單元測試遵守3a原則(arrange, act, assert)即準備,執行,驗證。

在準備測試資料時,若是使用硬編碼進行new 物件,修改資料等操作。會遇到幾個問題:測試資料的維護性不好,不方便管理,不夠直觀,資料可重用性低,對比欄位多時不方便驗證。因此測試資料需要從硬編碼中分離出來,使用文件管理。 csv簡潔方便,二進位制,支援多種格式編輯,支援excel,libreoffic編輯,資料版本易於維護。

在assert驗證期間,當乙個物件的屬性很多時,assert.assertequals()過程中部分人為了麻煩,只是驗證部分重要資料,這會造成驗證資料的不完整性。為了避免重複這種比較操作,需要把此部分提取出來,作為模組執行。

csv測試框架實現:

csv格式(title列,必需與資料庫字段相同,實體物件屬性相似)

unittest  id  account  amount

before   1  張三   20

after   1  張三 30

before測試準備資料,after執行後進行的驗證資料

核心類testdatacontext:測試資料載體

public string tablename;

public string idcolumn;

public string dbtype;

public string csvpath;

public string csvcharset;

public string classnamearray;

public string columnnamearray;

public map> beforecsvdatatestmap;

public map> aftercsvdatatestmap;

testdatamanager:測試資料的載入維護管理

loadtestdatacsv(string csvpath, testdatacontext datacontext) ;

putdatatodb(queryrunner queryrunner, testdatacontext datacontext, long... idlist) 

assertdatabyid(queryrunner queryrunner, testdatacontext datacontext, convertmetaandcachekey convertmeta, boolean changeexpect, setskipassertset, long... idlist)

skipassertset:過濾不需要比較的屬性字段

assertconvertmanager:處理do,csv,map之間的轉換,初始化準備資料。把所有資料轉換成map物件來進行處理。

preparecsvcolumntoclassfield(string table, string columnarray, class<?> clazz) 初始化關聯csv的列與物件屬性。

mapbuildcolumntofieldmap(string columnarray, map> fieldclassmap)

for (string column : columnarray)

columnfieldmap.put(column, fieldname);

}return columnfieldmap;

}private map> getfieldclassmap(class<?> clazz) catch (introspectionexception e) ", e);

}propertydescriptor props = bean.getpropertydescriptors();

for (propertydescriptor prop : props)

fieldclassmap.put(prop.getname(), prop.getpropertytype());}}

cacheforfieldclassmap.put(classname, fieldclassmap);

return fieldclassmap;

}public mapconverttoassertmapfromdo(object obj)物件轉換成map《屬性,物件屬性值》

public mapconverttoassertmapfromcsv(mapdatamap, string tablename, class<?> classfordo) csv轉換成map《屬性,csv值》

public t builddataobjectfromcsv(mapdatamap, t dataobj, string tablename)

assertutilsassertequals工具類

public static void assertmap(mapcsvmap, mapdomap, boolean changeexpect, setskipassertset)

for (map.entryentry : csvmap.entryset())

if (!changeexpect) else }}

以上是粗略**。

membertest:

arrange:

@postconstruct

public void init()

}public void test_updateamount_增加使用者金額(){

long id = 1l;

dbqueryutil.update("delete from member where id=" + id);

datamanager.putdatatodb(dbqueryutil, datacontext, id);

//act:

//執行增加方法 

//assert

testmanager.assertdbbyid(dbqueryrunner, datacontext, datacontext.dataaftertest, cm, id, skipfieldlist);

整個測試方法相當簡潔,沒有很多重複的assertequals,驗證了所有字段,保證測試完整性。

流程:testdatamanager(載入csv) --> assertconvertmanager(初始化,並快取到map) -->assertconvertmanager(資料之間的轉換) --> assertutils(轉換成map,進行assert.assertequals)

CI框架 匯出csv檔案

引入 this load dbutil this load helper file this load helper download 函式 function download 編碼問題 在database目錄下的db utility.php檔案 部分原始檔 foreach query result...

Angular 技術框架

angularjs是為了克服html在構建應用上的不足而設計的。html是一門很好的為靜態文字展示設計的宣告式語言,但要構建web應用的話它就顯得乏力了。所以我做了一些工作 你也可以覺得是小花招 來讓瀏覽器做我想要的事。通常,我們是通過以下技術來解決靜態網頁技術在構建動態應用上的不足 類庫 類庫是一...

測試框架 Pytest框架(一)

目錄 pytest的安裝與執行 安裝 執行執行方式及命令 ide中執行 命令列終端執行 生成報告 報告中的亂碼處理 檢查點 斷言 基於python語言的自動化常用的框架有三種,unittest pytest rf。前兩款更多的用於白盒單元測試,rf可以很好的做系統測試使用。安裝pytest pip ...