咱們在作一款自動化測試工具時,保持測試環境的整潔,是很重要的。也就是說,當咱們自動建立了測試數據,在完成測試後,要把測試數據刪除掉,這樣纔不會產生不少的垃圾數據,對測試環境形成影響。因而,測試數據的維護,就成了一個話題。c#
在這個項目中,咱們是這樣作的,用程序自動維護測試數據,好比測試建立用戶這個功能,有如下一些測試數據:ide
loginname, userlastname
函數
test_user1, TestUser1
工具
這些字段都加了數字做爲標記。測試用例是這樣設計的:oop
1. 用這些測試數據建立用戶測試
2. 建立成功後,刪除這個用戶以保持測試環境的清潔。設計
3. 調用自我開發的函數,修改測試數據,使得數字標記加1。code
因而,當這個測試用例跑完後,外部的測試數據變成:對象
loginname, userlastname
開發
test_user2, TestUser2
這是思路,關於第一點和第二點,在Ranorex中寫一些recording module就能夠作到。我描述一下,第三點。
要實現這個功能,首先,在Renorex裏面,建立一個code module,好比,IncreaseTestData.cs,而後寫一個方法,這個module的代碼以下:
public void increAttribute(string attribute, string testdatapath) { // Load test data CsvDataConnector csvConnector = new Ranorex.Core.Data.CsvDataConnector("CSVConnector",testdatapath,true); csvConnector.SeparatorChar = ','; ColumnCollection outColCollection; RowCollection outRowCollection; RowCollection resultRowCollection; csvConnector.LoadData(out outColCollection, out outRowCollection); string[] values = null; string thetarget = null; resultRowCollection.Clear(); // Find the target foreach(Ranorex.Core.Data.Row dataRow in outRowCollection) { values = dataRow.Values; thetarget = dataRow[attribute].ToString(); // Loop over strings int i; for (i = 0; i < values.Length; i++) { if (values[i].Contains(thetarget) || values[i].Equals(thetarget)) { if (!Regex.IsMatch(values[i], ".*[0-9]+$", RegexOptions.Compiled)) { values[i] = values[i] + "0"; break; } break; } } string theincrement = Regex.Match(values[i], @"(\d+)$").Value; long incrementvalue = Convert.ToInt64(theincrement); incrementvalue++; char[] charsToTrim = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' '}; thetarget = thetarget.TrimEnd(charsToTrim); string newvalue = thetarget + incrementvalue.ToString(); values[i] = newvalue; resultRowCollection.Add(values); } csvConnector.StoreData(outColCollection, resultRowCollection); }
而後,咱們在test case中,聲明這個類的對象,(每一個code module,在Ranorex中,都當作一個類),而後調用這個方法。
好比,在test case裏面,添加一個recording modul, "Increase",在Increase.usercode.cs裏面,寫下以下代碼:
IncreaseTestData incre=new IncreaseTestData(); incre.increAttribute("loginname","TC1_CreateUser\\TC1_CreateUser.csv"); incre.increAttribute("userlastname","TC1_CreateUser\\TC1_CreateUser.csv");