2012-01-05 115 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

相关问题