2016-09-20 9 views
0

今日私はTDDを練習しています。 [TestCase]または[TestCaseSource]属性を使用して、不十分なテスト機能を簡素化したいと考えています。私を助けてください。これは今私の機能ですNUNITの[TestCase]または[TestCaseSource]を使用して、構造体パラメータをテスト関数に渡す方法

[Test] 
    public void GetLength_TEST_1() 
    { 
     double output = Program.GetLength(new Point { x = 1, y = 2 }, new Point { x = 7, y = 8 }); 

     output *= 1000; 
     output = Math.Truncate(output); 
     output /= 1000; 

     Assert.AreEqual(8.485, output);    
    } 

    [Test] 
    public void GetLength_TEST_2() 
    { 
     double output = Program.GetLength(new Point { x = 2, y = 7 }, new Point { x = -8, y = -6 }); 

     output *= 1000; 
     output = Math.Truncate(output); 
     output /= 1000; 

     Assert.AreEqual(16.401, output); 
    } 

答えて

1

TestCaseSource属性を使用してください。

同じテスト方法は両方のテストケースを実行する必要があることに注意してください。

次のようなものです。

[TestFixture] 
public class MyClass 
{ 
    public class PointTextCase 
    { 
     public string TestName { get; private set; } 
     public Point P1 { get; set; } 
     public Point P2 { get; set; } 
     public double Result { get; set; } 

     public PointTextCase(Point p1, Point p2, double result) 
     { 
      P1 = p1; 
      P2 = p2; 
      Result = result; 
     } 

     public override string ToString() 
     { 
      return TestName; 
     } 
    } 

    private static IEnumerable<PointTextCase> PointTestCases 
    { 
     get 
     { 
      yield return new PointTextCase(new Point { x = 1, y = 2 }, new Point { x = 7, y = 8 }, 8); 
      yield return new PointTextCase(new Point { x = 2, y = 7 }, new Point { x = -8, y = -6 }, -12); 
     } 
    } 

    [Test, TestCaseSource("PointTestCases")] 
    public void GetLength_TEST(PointTextCase pointTestCase) 
    { 
     double output = Program.GetLength(pointTestCase.P1, pointTestCase.P2); 

     output *= 1000; 
     output = Math.Truncate(output); 
     output /= 1000; 

     Assert.AreEqual(pointTestCase.Result, output); 
    } 
} 

public static class Program 
{ 
    public static double GetLength(Point p1, Point p2) 
    { 
     //some logic/calculation 
     return p1.x * p2.y; 
    } 
} 

public class Point 
{ 
    public double x { get; set; } 
    public double y { get; set; } 
} 
関連する問題