C# Stryker 变异框架

C# Stryker Mutation Framework

我不熟悉测试和编写单元测试。我偶然发现了这个名为 Stryker 的突变框架。 我喜欢它,因为我正在用它编写更好的测试用例,这有助于我涵盖所有用例。

在一些测试用例突变中,我遇到了等式突变或字符串突变。我对这些是什么感到困惑?

例子

if (file.Length < 10) // Equality Mutation Here
{
throw new ArgumentException("The file name was too short", "file"); // String Mutation Here with the filename is too short
//throw new System.IO.FileNotFoundException();
}

这些是什么,为什么给我?有什么意义吗?

我知道我的问题可能是非常初学者的水平。

你看过文档了吗? https://github.com/stryker-mutator/stryker-handbook/blob/master/mutator-types.md

更具体地说:

  1. https://github.com/stryker-mutator/stryker-handbook/blob/master/mutator-types.md#equality-operator

当前比较运算符与众多可用运算符之一(>、<、>=、<=、==、!=)之间的相等突变变化

  1. https://github.com/stryker-mutator/stryker-handbook/blob/master/mutator-types.md#string-literal

字符串修改器基本上改变了一个字符串。如果您使用消息 "Exception because X" 抛出异常,它可能会将消息更改为不同的内容 "Exception because Y""A completely different message"

恕我直言,String Mutator 通常没有用。我从不对 String 消息断言(我依赖异常类型),但 Equaly Mutator 是现有的最经典、最简单和最有用的 mutator 之一

我很高兴突变测试可以帮助您学习单元测试,这也是我帮助构建 Stryker 的原因之一。要针对此特定案例回答您的问题:

if (file.Length < 10) // Equality Mutation Here
{
throw new ArgumentException("The file name was too short", "file"); // String Mutation Here with the filename is too short
//throw new System.IO.FileNotFoundException();
}

我们将 < 变异为 <=> 以查看您是否为以下的边缘情况编写了测试 file.Length == 9 file.Length == 10

我们改变字符串以查看您是否在测试中检查字符串的值。例如,您可以测试异常中的消息是否正确。

如果您对 Stryker 有任何疑问,请查看文档或查看这个很棒的博客 post:https://medium.com/swlh/mutation-tests-in-net-via-stryker-9fd9e8e4bcde

如果您认为文档不足,请提交问题,我们可以改进!