Ranorex中利用code module对于测试数据的管理
我們在做一款自動化測試工具時,保持測試環境的整潔,是很重要的。也就是說,當我們自動創建了測試數據,在完成測試后,要把測試數據刪除掉,這樣才不會產生很多的垃圾數據,對測試環境造成影響。于是,測試數據的維護,就成了一個話題。
在這個項目中,我們是這樣做的,用程序自動維護測試數據,比如測試創建用戶這個功能,有以下一些測試數據:
loginname, ? userlastname
test_user1, ? TestUser1
這些字段都加了數字作為標記。測試用例是這樣設計的:
1. 用這些測試數據創建用戶
2. 創建成功后,刪除這個用戶以保持測試環境的清潔。
3. 調用自我開發的函數,修改測試數據,使得數字標記加1。
于是,當這個測試用例跑完后,外部的測試數據變成:
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 targetforeach(Ranorex.Core.Data.Row dataRow in outRowCollection){values = dataRow.Values;thetarget = dataRow[attribute].ToString();// Loop over stringsint 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");轉載于:https://blog.51cto.com/yazi0127/1211356
總結
以上是生活随笔為你收集整理的Ranorex中利用code module对于测试数据的管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: less 学习指南
- 下一篇: 如何编写 Nagios 插件