3. 解決方案
3.1 普通方案:從腳本列表生成 CQTM Test Case
方案描述
圖 7 說明了 RMT2CQ 工具是如何在普通方案中使用的。在此方案中,測試人員在測試用例準備時僅需編寫 RMT 腳本,RMT2CQ 將負責在 CQTM 中生成 Test Case 和 Configured Test Case,生成時會根據 RMT 腳本所在的文件目錄結構來組織測試計劃(Test Plan)的結構,并以 RMT 腳本的名字作為 Test Case 的標題。
圖 7. 在普通方案中使用 RMT2CQ 工具
所有需要傳入該工具的信息都可以在其用戶界面上找到,如圖 8 所示。
在“Generation”標簽頁上,用戶需要指定 RMT 腳本所在的路徑,以及將 RMT 腳本關聯到哪個層級:Test Case 或 Configured Test Case。如果用戶指定只關聯到 Test Case 層級,則該工具將根據 RMT 腳本創建 Test Case,并把它們相關聯,然后停止;如果關聯到 Configured Test Case 層級,則工具將繼續創建 Configured Test Case 并建立相應關聯。
在“Configuration”標簽頁上,用戶需要指定 master schema 的名稱,登錄所使用的用戶名和密碼,登錄到的數據庫名稱, configuration 名稱,asset registry 名稱,file location 名稱,以及 test plan 名稱。這些信息可以儲存為一個配置文件,這樣當用戶下次啟動該工具時,就無需再次輸入。
圖 8. RMT2CQ 的用戶界面
示例代碼
本小節包含了對上述方案的詳細解釋以及部分示例代碼,供讀者參考。
當 CQ 的 API 第一次在代碼中出現時,以粗體標注。
RMT2CQ 工具的基本步驟包括:
創建 CQSession 并登錄;獲取 RMT 腳本的名稱;創建 Test Plan;創建 Test Case;創建 Configured Test Case;創建 External File;將腳本與 Test Case 或 Configured Test Case 相關聯。 創建 CQSession 并登錄。此處需要傳入所登錄的 schema 和數據庫名稱,以及登錄所使用的用戶名和密碼。清單 1. 創建 CQSession 并登錄
CQSession session = new CQSession(); //pass in username, password, dbname and schemaname; session.UserLogon(username, password, dbname, schemaname);
獲取 RMT 腳本的名稱。
根據全路徑名,可以通過 Java API 獲取 RMT 腳本的名稱,此處省略具體代碼。
創建 Test Plan。我們沿用前面的例子,“FVT”和“Function1”是 Test Plan,并且前者是后者的父計劃。對于“FVT”來說,它沒有父計劃。如果這些測試計劃還未創建,則根據樹結構創建它們。順序是首先創建最上層計劃,再創建其下的子計劃。在測試計劃創建完成后,應當返回其 ID 以用于稍后創建其余的 test plan 或 test case。
清單 2. 創建 Test Plan
// query to determine if the test plan already exists // if query for the top level plan, the sql should be modified String sql = "select distinct T1.id from TMTestPlan T1, TMtestplan T2 " + "where T1.parentplan = T2.dbid and " + "(T1.dbid <> 0 and ((T2.headline = '" + parentPlan + "' and " + "T1.headline = '" + headline + "')))"; CQResultSet resultSet = session.BuildSQLQuery(sql); resultSet.EnableRecordCount(); resultSet.Execute(); if ( resultSet.GetRecordCount() > 0 ) { resultSet.MoveNext(); return resultSet.GetColumnValue(1); //already exists, no need to create } // if the test plan does not exist, create it // fetch parent plan’s ID; for the top level plan, “parentPlan” is empty String parentPlanId = ""; if ( !parentPlan.equals("") ) { String sql = "select T1.id from TMTestPlan T1 where" + " T1.headline='" + parentPlan + "'"; CQResultSet resultSet = session.BuildSQLQuery(sql); resultSet.Execute(); while ( resultSet.MoveNext() == 1 ) { parentPlanId = resultSet.GetColumnValue(1); break; } } // create test plan and return it’s ID CQEntity entity = session.BuildEntity("TMTestPlan"); entity.SetFieldValue("AssetRegistry", registryName); entity.SetFieldValue("Headline", headline); if ( !parentPlan.equals("") ) { entity.SetFieldValue("parentplan", parentPlanId); } entity.Validate(); entity.Commit(); return entity.GetFieldValue("id").GetValue();
創建 Test Case。
當測試計劃的層次被創建之后,可根據直接父計劃的 ID 來創建一個 Test Case,其標題即是在第 2 步中取得的腳本名稱。同樣,所創建的 Test Case 的 ID 也應被返回以用于 Configured Test Case 的創建和腳本的關聯。
清單 3. 創建 Test Case
// queryto determine if this test case already exists // the code is similar to the query of test plan, so omit here // if the test case does not exist, create it // the creation of Entity is similar to previous code, so only list difference here CQEntity entity = session.BuildEntity("TMTestCase"); entity.SetFieldValue("parentplan", parentPlanId); entity.SetFieldValue("Headline", headline);
創建 Configured Test Case。
當一個 TC(Test Case)被創建之后,可以在其下創建 CTC(Configured Test Case)。創建時需要 Configuration 域的值,該值是在 RMT2CQ 工具的用戶界面上選擇的。該 CTC 的 ID 同樣應被返回以用于腳本的關聯。
清單 4. 創建 Configured Test Case
// query to determine if this configured test case already exists // the code is similar to the query of test plan, so omit here // if the test case does not exist, create it CQEntity entity = session.BuildEntity("TMConfiguredTestCase"); entity.SetFieldValue("testcase", testcaseId); entity.SetFieldValue("headline", headline); entity.SetFieldValue("configuration", configuration);
創建 External File。
External File 實現腳本與 TC 或 CTC 之間的連接,因此在進行關聯前必須被創建。下面的示例代碼中,“fileLocationName”,“assetRegistry”和“rmtPath”的值是從用戶界面上選取的,externalFileName 的值為第 2 步中的腳本名稱。External File 的名稱被返回以用于腳本關聯。
清單 5. 創建 External File
// get the real path corresponding to fileLocationName // similar to previous code, only change the SQL statement String sql = "select distinct T1.scriptfileslocation from” + " tmfilelocation T1,tmassetregistry T2 where T1.assetregistry = T2.dbid and " + "(T1.dbid <> 0 and ((T2.name = '" + assetRegistry + "' and " + "T1.name = '" + fileLocationName + "')))"; … scriptFileLoc = resultSet.GetColumnValue(1); // get the full path of “rfile”. The naming rule of rfile is defined by CQ. String rfile; int index = scriptFileLoc.lastIndexOf("/"); scriptFileLoc = scriptFileLoc.substring(index + 1); int length = scriptFileLoc.length(); int length2 = rmtPath.length(); if (length2 > length) { //if rmtPath is a sub directory of scriptFileLoc rfile = "rfile://" + fileLocationName + "/" + rmtPath.substring(length + 1) + "\\" + externalFileName; } else { rfile = "rfile://" + fileLocationName + "\\" + externalFileName; } // query on this External File. Only need to change the SQL statement sql = "select T1.fldcolumn from TMExternalFile T1 where T1.fldcolumn = '" + rfile + "'"; // if this External File does not exist, create it session.SetNameValue("ExternalFileCreation", "true"); CQEntity entity = session.BuildEntity("TMExternalFile"); entity.SetFieldValue("File", rfile); entity.SetFieldValue("FileLocation", assetRegistry + " " + fileLocationName);
將腳本與 Test Case 相關聯。
然后,通過 External File 和相應的 TC 的 ID,就可將腳本與 TC 相關聯。此處腳本類型被設為“RMT”,它也可被設為其他腳本類型,例如“RFT”。
清單 6. 將腳本與 Test Case 相關聯
CQEntity entity = session.GetEntity("TMTestCase", tcId); entity.EditEntity("modify"); entity.SetFieldValue("DefaultScript", externalFile); entity.SetFieldValue("DefaultScriptType", "RMT"); entity.Validate(); entity.Commit();
將腳本與 Configured Test Case 相關聯。
用同樣的方法可以將腳本與 CTC 相關聯。
清單 7. 將腳本與 Configured Test Case 相關聯
CQEntity entity = session.GetEntity("TMConfiguredTestCase", ctcId); entity.EditEntity("modify"); entity.SetFieldValue("Script", externalFile); entity.SetFieldValue("TestType", "RMT"); entity.Validate(); entity.Commit();
3.2 高級方案:從 Checklist 導入信息
方案描述
文章來源于領測軟件測試網 http://www.kjueaiud.com/