您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > Nunit
Nunit單元測試演練
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2013/3/29 16:13:15 ] 推薦標簽:

      學以致用,單元測試的工具越來越多,可是想找一篇如何單元測試的文章卻很難。所以偶來寫一點自己的心得,也是一步步摸索著。

      先從測試一個方法開始,偶隨便找了一個小算法。這個方法看來正合適:不能太簡單,比如一加一等于二;也不能太復(fù)雜,比如牽涉到數(shù)據(jù)庫操作。

      這個算法的功能是取得小于Max的N個隨機不重復(fù)正整數(shù),代碼如下:

public static List<Int32> GetRandomNum(int Max, int N)
{
    List<int> Source = new List<int>();
    List<int> Result = new List<int>();
    for (int i = 0; i < Max; i++) Source.Add(i + 1);
    for (int n = 0; n < N; n++)
    {
        int r = new Random().Next(0, Source.Count);
        Result.Add(Source[r]); Source.RemoveAt(r);
    }
    Result.Sort();
    return Result;
}

      算法很好理解。好久前寫的,現(xiàn)在翻出來,一眼看出有問題。不過有點問題正好,看看單元測試能否找出來。

      先添一個項目,引用NUnit.Framework,裝好NUnit后在Net組件里有,免安裝版的在解壓后的目錄里找。

      新建一個Public類用于測試,叫Test,生成隨機數(shù)的方法的類叫Program,F(xiàn)在無所謂,正式項目里命名得規(guī)范一點。Test類加[TestFixture]特性,添加一個Public函數(shù)叫做TestGetRandomNum,加上[Test]特性,這樣NUnit能認出這個方法是用于測試的方法了。還給該函數(shù)加了一個[Category("GetRandomNum")],因為一個功能可能要寫幾個函數(shù)來測試,這個特性表明方法屬于測試GetRandom的方法組。 

      還有幾個常用的特性: [TestFixtureUp] [TestFixtureDown] [TearDown] [Setup] [Ignore] [Explicit],據(jù)說以后了解這幾個差不多了。
     
      接著,第一個測試方法該測試什么呢?以偶寫代碼的一點經(jīng)驗,每個函數(shù)都會把參數(shù)驗證放在前面,參數(shù)無效不往下走了,拋異常什么的該干嘛干嘛,那測試也先從參數(shù)測起吧。 現(xiàn)在分析一下參數(shù)異常下的輸出:若N<1,不管Max值多少都返回空的List;若N>=1,且Max<N,由于Source集合數(shù)量不夠,應(yīng)該會引發(fā)IndexOutOfRangeException。

      寫一個測試方法用于測試這兩種異常輸出,應(yīng)該寫兩個測試方法好一點。不過剛?cè)胧,一個兩個不必分了,寫出來好。代碼如下:

 1       public void TestGetRandomNum3()
 2        {
 3            int result1 = 0, result2 = 0, result3 = 0;
 4            //N<1的case
 5            int[,] case1 = { { 0, 0 }, { 9999, 0 }, { -888, 0 }, { 1111, -888 }, { 0, -1 }, { -100, -99 } };
 6            for (int i = 0; i < 6; i++)
 7            {
 8                int Max = case1[i, 0];
 9                int N = case1[i, 1];
10                List<int> list = Program.GetRandomNum(Max, N);
11                if( list.Count==0) result1 ++;
12            }
13            //N>=1,Max<N
14            int[,] case2 = { { 0, 5 }, { 0, 1 }, { -10, 1 }, { -10, 30 }, { 10, 20 } };
15            for (int j = 0; j < 5; j++)
16            {
17                int Max = case2[j, 0];
18                int N = case2[j, 1];
19                try
20                {
21                    List<int> list = Program.GetRandomNum(Max, N);
22                }
23                catch (IndexOutOfRangeException ex)
24                {
25                    result2++;
26                }
27            }
28            //M=N>=1
29            int[,] case3 = { { 1, 1 }, { 10, 10 } };
30            for (int k = 0; k < 2; k++)
31            {
32                int Max = case3[k, 0];
33                int N = case3[k, 1];
34                List<int> list = Program.GetRandomNum(Max, N);
35                bool isMatch = true;
36                for (int n = 0; n < list.Count; n++)
37                {
38                    if (n != list[n]) { isMatch = false; break; }
39                }
40                if (isMatch && list.Count == N) result3++;
41            }
42
43            Assert.AreEqual(result1, 6);
44            Assert.AreEqual(result2, 5);
45            Assert.AreEqual(result3, 2);
46        }


      運行NUnit加載項目,雙擊選中剛寫的方法,眼前出現(xiàn)一根長長的大紅條!

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd