单元测试集

时间:2009-08-11 16:57:35

标签: c# unit-testing

我有一个简单的方法,它返回字母表中的字母数组。

public char[] GetLettersOfTheAlphabet() {
    char[] result = new char[26];
    int index = 0;

    for (int i = 65; i < 91; i++) {
        result[index] = Convert.ToChar(i);
        index += 1;
    }

    return result;
}

我尝试用

对代码进行单元测试
[TestMethod()]
public void GetLettersOfTheAlphabetTest_Pass() {
    HomePage.Rules.BrokerAccess.TridionCategories target = new HomePage.Rules.BrokerAccess.TridionCategories();
    char[] expected = new char[] { char.Parse("A"), 
                                   char.Parse("B"),
                                   char.Parse("C"),
                                   char.Parse("D"),
                                   char.Parse("E"),
                                   char.Parse("F"),
                                   char.Parse("G"),
                                   char.Parse("H"),
                                   char.Parse("I"),
                                   char.Parse("J"),
                                   char.Parse("k"),
                                   char.Parse("L"),
                                   char.Parse("M"),
                                   char.Parse("N"),
                                   char.Parse("O"),
                                   char.Parse("P"),
                                   char.Parse("Q"),
                                   char.Parse("R"),
                                   char.Parse("S"),
                                   char.Parse("T"),
                                   char.Parse("U"),
                                   char.Parse("V"),
                                   char.Parse("W"),
                                   char.Parse("X"),
                                   char.Parse("Y"),
                                   char.Parse("Z")};
    char[] actual;
    actual = target.GetLettersOfTheAlphabet();
    Assert.AreEqual(expected, actual);
}

似乎它归结为数组使用的比较函数。你是如何处理这种情况的?

6 个答案:

答案 0 :(得分:4)

如果您使用的是.NET 3.5,还可以使用SequenceEqual()

[TestMethod()]
public void GetLettersOfTheAlphabetTest_Pass() {
    var target = new HomePage.Rules.BrokerAccess.TridionCategories();
    var expected = new[] { 'A','B','C', ... };

    var actual = target.GetLettersOfTheAlphabet();
    Assert.IsTrue(expected.SequenceEqual(actual));
}

答案 1 :(得分:4)

CollectionAssert.AreEquivalent(预期,实际)有效。

我不确定它是如何工作的,但它确实能满足我的需求。我认为它检查实际列中预期集合exsits中每个项目中的每个项目,它还检查它们是否相同,但顺序无关紧要。

答案 2 :(得分:2)

只需遍历数组并比较每个项目。每个数组中的索引4应该相同。

另一种方法是检查结果数组是否包含'A','G','K'或随机字母。

我从单元测试中学到的东西有时候你需要复制代码或做更多工作,因为这是测试的本质。对于生产站点来说,单元测试的基本功能可能是不可接受的。

答案 3 :(得分:0)

您的K在预期数组中是小写的,但我相信代码会生成大写字符。如果比较区分大小写,则当两个数组不匹配时会导致失败。

答案 4 :(得分:0)

您还可以对数组中的值的大小和一些抽查进行测试....

Assert.AreEqual(expected.Length,actual.Length)

Assert.AreEqual(期望[0],实际[0]) ....

答案 5 :(得分:0)

首先:


Assert.AreEqual(expected.Length,actual.Length);

然后:


for(int i = 0; i < actual.Length; i++)
{

//Since you are testing whether the alphabet is correct, then is ok that the characters
//have different case.
  Assert.AreEqual(true,actual[i].toString().Equals(expected[i].toString(),StringComparison.OrdinalIgnoreCase));
}
相关问题