咱们在作一款自动化测试工具时,保持测试环境的整洁,是很重要的。也就是说,当咱们自动建立了测试数据,在完成测试后,要把测试数据删除掉,这样才不会产生不少的垃圾数据,对测试环境形成影响。因而,测试数据的维护,就成了一个话题。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");