Intellitest为每个生成的测试添加断言的位置

Isa*_*aac 6 unit-testing assertions visual-studio-2015 intellitest

在这里,我将通过一个例子来解释这个问题.最初的问题更抽象地提出了问题.不需要阅读它.

更新:问题为例

让我们说我们已经实现了这个buggy函数来找到int []的min:

public int MyMin(int[] data)
{
    int min = 1000;

    for (int i = 1; i < data.Length; i++)
    {
        if (data[i] < min)
        {
            min = data[i];
        }
    }

    return min;
}
Run Code Online (Sandbox Code Playgroud)

在这个函数上运行Intellitest给我们: 在此输入图像描述

注意,对于测试#4和#6,该函数由于其错误实现而未正确计算最小值.但是,这些测试正在通过,这是不希望的.

Intellitest不能神奇地确定我们的预期行为,MyMin并使测试失败.但是,如果我们可以手动指定这些测试的所需结果,那将是非常好的.

@michał-komorowski的解决方案是可行的,但对于每个测试用例,我必须重复其输入的PexAssumes.是否有更加优雅/干净的方式来为测试输入指定所需的输出?

原始问题

Intelitest生成可修改的参数化测试,并可在其中添加通用/全局断言.它还生成最大数量的输入,以最大化代码覆盖率.Intellitest将输入存储为单独的单元测试,每个测试都使用精心设计的输入调用参数化测试.

我正在寻找一种方法来为每个输入添加断言.

由于每个输入都作为单元测试函数存储在.g.cs文件中,因此可以在那里添加断言.问题是这些功能不应由用户自定义,因为它们将在后续运行中被Intellitest覆盖.

为每个单元测试添加断言的推荐方法是什么?

Mic*_*ski 2

You shouldn't add assertions to test methods (methods with [TestMethod] attribute). They are only used to provide parameters values. The place to put assertions are methods with [PexMethod] attribute.

At first glace it may look like a limitation. However, if we consider how IntelliTest works it isn't. There is no sense to add assertion per each input because inputs can be deleted, updated or created at any moment. For example when:

  • A method being tested was changed.
  • PexAssume class was used.
  • Configuration of PexMethod attribute was changed.

However, you can do something else i.e. to add more than one "Pex method" for a method being tested and use PexAssume. For example lets assume that we have BubbleSort method and we want to define different assertions depending on the length of an input array.

[PexMethod]
public void BubbleSort(int[] a)
{
    PexAssume.IsTrue(a.Length == 5);
    int[] result = Program.BubbleSort(a);
    // Assertions specific for an array with 5 elements
}

[PexMethod]
public void BubbleSort(int[] a)
{
    PexAssume.IsTrue(a.Length == 10);
    int[] result = Program.BubbleSort(a);
    // Assertions specific for an array with 10 elements
}
Run Code Online (Sandbox Code Playgroud)