2012-01-05 118 views
2

我正在使用單元測試進行實驗性實驗,下面是我正在測試的應用程序的一段代碼。大多數單元測試已完成,但關於下面的構造函數,我無法弄清楚如何測試它。例如,構造函數是如何處理數組元素的?什麼是測試顧問的好方法?構造函數的單元測試

有沒有可能給我一個正確的方向踢一個善良的靈魂?

public struct Point { 
    public int x, y; 

    public Point(int a, int b) { 
     x = a; 
     y = b; 
    } 
    } 

...

public Triangle(Point[] s) { 
    sides = new double[s.Length]; 
    sides[0] = Math.Sqrt(Math.Pow((double)(s[1].x - s[0].x), 2.0) + Math.Pow((double)(s[1].y - s[0].y), 2.0)); 
    sides[1] = Math.Sqrt(Math.Pow((double)(s[1].x - s[2].x), 2.0) + Math.Pow((double)(s[1].x - s[2].x), 2.0)); 
    sides[2] = Math.Sqrt(Math.Pow((double)(s[2].x - s[0].x), 2.0) + Math.Pow((double)(s[2].x - s[0].x), 2.0)); 
    } 

...

 [TestMethod()] 
     public void TriangleConstructorTest1() 
     { 
      Point[] s = null; // TODO: Initialize to an appropriate value 
      Triangle target = new Triangle(s); 
      Assert.Inconclusive("TODO: Implement code to verify target"); 
     } 
+0

你確定他們需要你也測試構造嗎?對我來說似乎毫無意義,因爲你只是在做一些任務。 – Tudor 2012-01-05 17:24:48

+0

以任何方式暴露「雙方」? 「Triangle」有哪些公共屬性? – 2012-01-05 17:25:17

+1

@Tudor:其實我傾向於不同意。因爲它在做任務,所以它可能應該進行單元測試,以確保它分配預期的內容。但那僅僅是我的0.02美元,而且每個人都以不同的方式處理單元測試。 – 2012-01-05 17:27:03

回答

3

你說的是Traingle構造函數,不是嗎?

看起來它使用的是Pythagorean theorem來計算三角形邊的長度。
所以你應該測試這個計算是否正確完成。此外,您可以檢查構造函數是否檢測到無效參數,例如,:

[TestMethod] 
[ExpectedException(typeof(ArgumentNullException))] 
public void Cannot_Create_Passing_Null() 
{ 
    new Triangle(null); 
} 
[TestMethod] 
[ExpectedException(typeof(ArgumentException))] 
public void Cannot_With_Less_Than_Three_Points() 
{ 
    new Triangle(new[] { new Point(0, 0), new Point(1, 1) }); 
} 
[TestMethod] 
[ExpectedException(typeof(ArgumentException))] 
public void Cannot_Create_With_More_Than_Three_Points() 
{ 
    new Triangle(new[] { new Point(0, 0), new Point(1, 1), new Point(2, 2), new Point(3, 3) }); 
} 
[TestMethod] 
[ExpectedException(typeof(ArgumentException))] 
public void Cannot_Create_With_Two_Identical_Points() 
{ 
    new Triangle(new[] { new Point(0, 0), new Point(0, 0), new Point(1, 1) }); 
} 
[TestMethod] 
[ExpectedException(typeof(ArgumentException))] 
public void Cannot_Create_With_Empty_Array() 
{ 
    new Triangle(new Point[] { }); 
} 
1

我假設你有一個Sides屬性,可以斷言,返回的值從這個是應該是什麼。

您可能還想對您的代碼進行一些重構,對於我來說,看起來這3條線幾乎完全相同,但參數不同Point

此外,您粘貼的構造函數與示例測試中的構造函數不同。

+0

是的,我想我應該這樣做(聲稱返回的值是他們應該的)。但是,在這種情況下,我如何將它們返回到測試方法,以便我可以檢查它們? – holyredbeard 2012-01-05 17:42:32

4

也許我失去了一些東西,但不會這樣做嗎?在測試變量分配但實際上

[TestMethod()] 
     public void PointConstructorTest1() 
     { 
      Point target = new Point(1.5, 2.0); 
      Assert.AreEqual(1.5, target.x); 
      Assert.AreEqual(2.0, target.y); 
     } 

沒有多大意義....

+2

其實我在測試任務中發現了很多價值。如果內部表示發生變化等,那麼知道變更沒有破壞現有測試是很好的。因此,我傾向於爲我所有的構造函數甚至簡單的屬性編寫單元測試,但這只是我的$ 0.02 – 2012-01-05 17:31:06

+1

這是一個很好的單元測試,我想他是在問如何單元測試Triangle構造函數。 – 2012-01-05 17:32:27

3

硬盤基於最初的問題某些不一致的回答,但我會去根據你的構造函數的代碼,而不是你的樣品測試。首先,我要測試一個點數爲< 3或> 3的適當的異常(你應該檢查並拋出你的構造函數)。

首先,我有你的構造函數拋出,如果點的數組是壞:

public Triangle(Point[] s) 
    { 
     // you could use more specific ArgumentNullException for first, 
     // IllegalOperationException for second, etc, you get the point 
     // (no pun intended). 
     if (s== null || s.Length != 3 || s.Any(x => x == null)) 
      throw new ArgumentException("s"); 
     ... 
    } 

然後,我測試的是空還是非正確的長度

[TestMethod] 
    [ExpectedException(typeof(ArgumentException))] 
    public void TriangleConstructorWithNullPoints() 
    { 
     Point[] s = null; 
     Triangle target = new Triangle(s); 
    } 

[TestMethod]        
[ExpectedException(typeof(ArgumentException))]        
public void TriangleConstructorWithFourPoints()        
{ 
    Point[] s = new Point[4]; 
    Triangle target = new Triangle(s);        
} 

然後我會爲常見的情況進行測試。採取一個3/4/5三角形例如:

[TestMethod] 
public void TriangleConstructorWithFourPoints() 
{ 
    Point[] s = new [] 
    { 
     new Point { x = 0, y = 0 }, 
     new Point { x = 4, y = 0 }, 
     new Point { x = 4, y = 3 } 
    }; 

    Triangle target = new Triangle(s); 

    Assert.IsTrue(target.Sides.Contains(3.0)); 
    Assert.IsTrue(target.Sides.Contains(4.0)); 
    Assert.IsTrue(target.Sides.Contains(5.0)); 
} 

然後我測試任何邊緣的條件下,像長度3的陣列,但陣列中的點中的一個爲空,(檢查適當例外),或者如果任何兩點相同,依此類推。

1

我想先說,如果你遵循Eric Lippertrecommendations,那麼你不希望有可變的結構。現在,如果你將state verification作爲單元測試的關注點,你會得出結論:測試不可變對象沒有意義,因爲它們只能有一個狀態。最好的情況下,你可以達到的最多的是測試構造函數沒有做類似this.x = b

相關問題