代码之家  ›  专栏  ›  技术社区  ›  JustEngland

单元测试收集

  •  0
  • JustEngland  · 技术社区  · 16 年前

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

    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 回复  |  直到 16 年前
        1
  •  4
  •   dahlbyk    16 年前

    如果使用.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));
    }
    
        2
  •  4
  •   JustEngland    16 年前

    collectionassert.areequivalent(预期的,实际的)工作。

    我不太清楚它是如何工作的,但它确实能满足我的需要。我认为它会检查预期集合中每个项目的实际列中的每个项目,它还会检查它们是否是相同的计数,但顺序并不重要。

        3
  •  2
  •   David Basarab    16 年前

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

    另一种方法是检查结果数组是否包含“a”、“g”、“k”或随机字母。

    我从单元测试中学到的一些东西,有时你要么复制代码,要么做更多的工作,因为这就是测试的本质。基本上,用于单元测试的内容对于生产站点来说可能是不可接受的。

        4
  •  0
  •   Kevin LaBranche    16 年前

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

    assert.areequal(预期.length,实际.length)

    assert.areequal(应为[0],实际为[0]) …

        5
  •  0
  •   Brandon    16 年前

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

        6
  •  0
  •   Freddy    16 年前

    首先:

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