首页 > 解决方案 > MSTest DataRowAttribute 字符串参数乱序

问题描述

我有一个使用 MSTest 的 .NET Framework 4.7.2 测试项目,方法如下:

[DataTestMethod]
[DataRow("https://www.example.com", "x", "y", "https://www.example.com?x=y")]
[DataRow("https://www.example.com/", "x", "y", "https://www.example.com?x=y")]
[DataRow("https://www.example.com/page.aspx", "x", "y", "https://www.example.com/page.aspx?x=y")]
[DataRow("https://www.example.com/page.aspx/", "x", "y", "https://www.example.com/page.aspx?x=y")]
[DataRow("https://www.example.com/page.aspx?k=v", "x", "y", "https://www.example.com/page.aspx?k=v&x=y")]
[DataRow("https://www.example.com/page.aspx?k=v&k2=v2", "x", "y", "https://www.example.com/page.aspx?k=v&k2=v2&x=y")]
[DataRow("https://www.example.com/Directory/page.aspx?k=v&k2=v2", "x", "y", "https://www.example.com/Directory/page.aspx?k=v&k2=v2&k=y")]
public void DoSomething___works(string url, string n, string v, string expected)
{
    url.DoSomething(n, v).ShouldBe(expected);    // using Shouldly nuget package
}

当我运行测试时,第 5 个成功并正常运行,但其他所有测试都失败了。有趣的是,它们失败了,因为来自DataRowAttribute的参数以与预期不同的顺序分配给测试方法的参数。

例如,如果我调试第一个测试用例,这些是方法参数的值: | 参数 | 实际值 | 预期值 | | ----- | ------------- | --------------- | | 网址 | https://info.chromres.com?foo=bar | https://info.chromres.com | | n | https://info.chromres.com | x | | v | x | 是 | | 预期 | 是 | https://info.chromres.com?foo=bar |

所以看起来他们只是随机向右移动了一个位置。

为什么会发生这种情况?为什么它只会发生在大多数——但不是所有——的情况下?

有没有人见过这个?

标签: .netmstest

解决方案


推荐阅读