Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何提高具有大量参数的测试用例的可读性_C#_Unit Testing_Nunit - Fatal编程技术网

C# 如何提高具有大量参数的测试用例的可读性

C# 如何提高具有大量参数的测试用例的可读性,c#,unit-testing,nunit,C#,Unit Testing,Nunit,这主要是一个概念性的问题。我有一些类表示从(自定义)TextArea控件中删除文本的算法。我想测试一下——当然是算法;)。我担心我的测试方法缺乏可读性: [TestCase(new[] { "some text asdf" }, 5, 0, 9, 0, "some asdf", new int[0])] [TestCase(new[] { "some text", "", "totally unimportant texttext that stays" }, 0, 0, 24, 2, "te

这主要是一个概念性的问题。我有一些类表示从(自定义)TextArea控件中删除文本的算法。我想测试一下——当然是算法;)。我担心我的测试方法缺乏可读性:

[TestCase(new[] { "some text asdf" }, 5, 0, 9, 0, "some  asdf", new int[0])]
[TestCase(new[] { "some text", "", "totally unimportant texttext that stays" }, 0, 0, 24, 2, "text that stays", new[] { 0, 1, 2 })]
public void ShouldRemoveSelectedText(string[] lines, int colStart, int lineStart, int colEnd, int lineEnd, string notRemovedText, int[] expectedRemovedLines) {
    var newLines = algorithm.RemoveLines(lines, new TextPositionsPair {
        StartPosition = new TextPosition(column: colStart, line: lineStart),
        EndPosition = new TextPosition(column: colEnd, line: lineEnd)
    });

    Assert.That(newLines.LinesToChange.First().Value, Is.EqualTo(notRemovedText));
    CollectionAssert.AreEqual(expectedRemovedLines, newLines.LinesToRemove.OrderBy(key => key));
}
正如你所看到的,这是一个非常简单的测试。我为算法提供了
IEnumerable
of
string
和选择区域,但是乍一看,很难看出哪个
TestCase
参数在哪里。我在想,有没有“更干净”的方法?
旁注:我有和这个一样简单的测试,但必须提供更多的参数…

一个简单的方法就是每种情况使用更多的行

[TestCase(new [] { "some text asdf" },
          5, 0, 9, 0,
          "some  asdf",
          new int[0])]
[TestCase(new [] { "some text", "", "totally unimportant texttext that stays" },
          0, 0, 24, 2,
          "text that stays",
          new [] {0, 1, 2})]
public void ShouldRemoveSelectedText(...
或者,您可以使用TestCaseSource,引用fixture类中的静态数组

TestCaseData[] MySource = {
    new TestCaseData(new [] { "some text asdf" },
                     5, 0, 9, 0,
                     "some  asdf",
                     new int[0]),
    new TestCaseData(new [] { "some text", "", "totally unimportant texttext that stays" },
                     0, 0, 24, 2
                     "text that stays",
                     new [] { 0, 1, 2})};

[TestCaseSource("MySource")]
public void ShouldRemoveSelectedText(..
这些是在不改变测试参数的情况下我能看到的最好的选项,如果它是我的代码,我实际上会这么做

我将创建一个对象来封装一个文本缓冲区和另一个用于选择的对象。我在这里展示类,但它们可能是结构

class TextBuffer
{
    public string[] Lines;
    public Selection Selection;
    ...
}

class Selection
{
    public int FromLine;
    public int FromCol;
    public int ToLine;
    public int ToCol;
    ...
}
然后,我用这些测试原语重写测试,使它更易于阅读