【问题标题】:xUnit tests suddenly throwing exception System.Collections.Generic.KeyNotFoundException: Unable to find UniqueTest VSTestAdapterxUnit 测试突然抛出异常 System.Collections.Generic.KeyNotFoundException: Unable to find UniqueTest VSTestAdapter
【发布时间】:2020-01-29 17:04:04
【问题描述】:

我有一个 xUnit 项目,到目前为止,它已经完美地运行了所有测试。

我添加了两个新的测试方法,现在突然因为抛出异常而无法运行测试:

System.Collections.Generic.KeyNotFoundException: Unable to find UniqueTest VSTestAdapter

后面是我的一个测试方法的路径,但有时当我仍然尝试在异常更改后运行该方法的所有测试时。

这种异常让我觉得可能下面的代码和问题有关,但是我写完之后所有的测试都运行良好,问题出现在我写了两个很普通的测试之后,评论它们什么也没做。

可疑代码:

    public static IEnumerable<object[]> TestData
    {
        get
        {
            return new[]
            {
                new object[] { "zrgrgffwf", new string[] { "sf", "age", "fe"} },
                new object[] { "gfsefsefse", new List<string>() { "fef", "awr", "hrg" } },
                new object[] { "shgfeswff", new HashSet<string>() { "shsg", "wde", "kiuik" } },
                new object[] { "pajfoehgsd", new Queue<string>(new string[] { "okd", "rnop", "rng" }) }
            };
        }
    }

正如我所说,在添加并在 MemberData 属性属性中使用它之后,一切运行良好几次。写完之后问题出现了:

    [Theory]
    [InlineData("sample text", "lolwut", "test")]
    [InlineData("other sample text", "yup", "ahha")]
    [InlineData("yadda", "-", "yadda.")]
    [InlineData("et", "cete", "ra")]
    public void Method_Test(string testString, string testCase, string expected)
    {
        string actual = testedMethod(testCase);

        Assert.Equal(expected, actual);
    }

附加说明:在编写该属性代码后,在尝试在“发布”配置中“运行所有”测试时,我曾发生过同样的异常。但是只需再次单击“运行所有测试”和/或切换到“调试”似乎就可以解决它 - 所有测试都运行了。现在我确保处于“调试”状态,但无论我点击多少次“运行所有测试”,有时唯一会改变的是,就像我说的,异常后引用的方法。

【问题讨论】:

  • 我没有答案,但我确实在 xUnit GitHub 存储库中注意到了这个非常相似的问题:github.com/nunit/nunit3-vs-adapter/issues/685。听起来这个错误可能在测试运行程序中,而不是在您的代码中。

标签: c# exception xunit


【解决方案1】:

link@xander 发布的帖子中,我看到安装 nUnit VS 适配器可以解决问题,即使我使用的是 xUnit。

Welp,它为我做到了。
在测试资源管理器中,我显示了一个额外的测试项目(这是我自己的测试项目,只有第一个“InlineData”作为参数)我的 xUnit 测试现在运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 2019-01-03
    相关资源
    最近更新 更多