2016-03-03 88 views
0

測試在這裏,他們是非常重複,我想改變...單元測試的重複問題

 [TestClass] 
    public class CompareDataTest 
    { 
private void GenerateTerms(int firstTerm, int secondTerm, out TaughtUnit firstGrade, out TaughtUnit secondGrade) 
     { 
      firstGrade = new TaughtUnit("foo"); 
      secondGrade = new TaughtUnit("bar"); 
     } 

     [TestMethod] 
     public void CompareResultsTest1() 
     { 
      var firstGrade = new TaughtUnit("foo"); 
      firstGrade.Percentage.Add("GRADE", 38); 
      var secondGrade = new TaughtUnit("foo"); 
      secondGrade.Percentage.Add("GRADE", 70); 
      var oldTerm = new List<TaughtUnit>() { firstGrade }; 
      var newTerm = new List<TaughtUnit>() { secondGrade }; 
      var rankBefore = firstGrade.Percentage; 
      var rankNow = secondGrade.Percentage; 
      var messages = Program.CompareData(oldTerm, newTerm); 

      Assert.AreEqual(0, messages.Count); 
     } 

     [TestMethod] 
     public void CompareResultsTest2() 
     { 
      var firstGrade = new TaughtUnit("foo"); 
      firstGrade.Percentage.Add("GRADE", 0); 
      var secondGrade = new TaughtUnit("foo"); 
      secondGrade.Percentage.Add("GRADE", 35); 
      var oldTerm = new List<TaughtUnit>() { firstGrade }; 
      var newTerm = new List<TaughtUnit>() { secondGrade }; 
      var rankBefore = firstGrade.Percentage; 
      var rankNow = secondGrade.Percentage; 
      int gradeOld = 0; int gradeNew = 10; 
      var getGradeOld = firstGrade.Percentage.TryGetValue("GRADE", out gradeOld); 
      var getGradeNew = secondGrade.Percentage.TryGetValue("GRADE", out gradeNew); 
      var difference = gradeNew - gradeOld; 

      var messages = Program.CompareData(oldTerm, newTerm); 

      Assert.AreEqual(1, messages.Count); 
      Assert.AreEqual("foo: GRADE rank has dropped by 30", messages[0]); 
     } 

我想我這組測試前,應使用私人無效,但我不知道如何... 我如何參考私人空間中創建的內容?

回答

0

在Test類級別聲明這些變量。使用私有方法填充設置變量。

[TestClass] 
public class CompareDataTest 
{ 
    private TaughtUnit firstGrade; 
    private TaughtUnit secondGrade; 

    private void GenerateTerms(int firstTerm, int secondTerm, out TaughtUnit firstGrade, out TaughtUnit secondGrade) 
    { 
     firstGrade = new TaughtUnit("foo"); 
     secondGrade = new TaughtUnit("bar"); 
    } 
    [TestMethod] 
    public void CompareResultsTest1() 
    { 
     GenerateTerms(..., ..., ..) 
     firstGrade.Percentage.Add("GRADE", 38); 
     secondGrade.Percentage.Add("GRADE", 70); 

     // rest of other things you want to set specific to the test. 

     Assert.AreEqual(0, messages.Count); 
    } 
} 
0

NUnit Framework有多種方法可以讓您的測試通過價值源。

比如我可以測試Guid.Parse像這樣

public static Dictionary<int, string> Datas 
    { 
     get 
     { 
      return Enumerable.Range(0, 100) //Lots of numbers 
          .ToDictionary(x => x, x => Guid.NewGuid().ToString()); 
     } 
    } 

    [Test] 
    public void MultiCacheTest([ValueSource("Datas")] KeyValuePair<int, string> item) 
    {      
     //ACT ...Yes this tests Guid.Parse 100 times 
     var value = Guid.Parse(item.value); 

     Assert.AreEqual(item.Value, value.ToString());   
    } 

的想法是通過測試所有的單元可以採取不同的路徑,並允許對的key所需的所有purmutations作爲該方法的輸入值,該對的值是預期的結果。在你的情況下,你可能會傳遞2個百分比值作爲密鑰「0 | 35」「foo:GRADE等級已經下降了30」將是預期值。