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 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");




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章