单元测试重复问题

时间:2016-03-03 21:57:49

标签: c# unit-testing repeat

测试在这里,它们非常重复,我想改变......

     [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]);
        }

我想我应该在测试集之前使用私有空格,但我不知道如何... 我如何引用private void中创建的内容?

2 个答案:

答案 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);
    }
}

答案 1 :(得分:0)

NUnit Framework有很多方法可以为您的测试传递一个Value Source。

例如,我可以像这样测试Guid.Parse

getTime()

想法是通过测试单位可以采用的所有各种路径所需的所有预设,并允许该对的密钥作为方法的输入值,其值为这对是预期的结果。在您的情况下,您可以传递2个百分比值作为键&#34; 0 | 35&#34; &#34; foo:GRADE等级下降了30&#34; < / em>将是预期值。