同时运行所有单元测试时出现异常

发布于 2024-12-13 10:24:33 字数 1709 浏览 2 评论 0原文

我在单元测试类中有很多单元测试。

当我单独运行每个时,它们都会通过,但是当我同时运行它们时,第一个通过,其余的失败:

System.ArgumentException:具有相同键的项目已被 添加了

谁能告诉我为什么?我需要如何纠正该错误?

示例:

 public TestContext TestContext
    {
        get
        {
            return testContextInstance;
        }
        set
        {
            testContextInstance = value;
        }
    }

    #region Additional test attributes
    #endregion

    /// <summary>
    ///A test for SplitTdsName
    ///</summary>
    [TestMethod()]
    public void SplitTdsNameTest_SimpleValidName1()
    {            
        string tdsName = "Title FirstName Surname";
        MemberName expected = new MemberName("Title", "FirstName", "Surname");
        MemberName actual;
        actual = TdsTransformer.SplitTdsName(tdsName);
        Assert.AreEqual(expected, actual);            
    }

    /// <summary>
    ///A test for SplitTdsName
    ///</summary>
    [TestMethod()]
    public void SplitTdsNameTest_SimpleValidName2()
    {
        string tdsName = "Title FirstName MiddleName Surname";
        MemberName expected = new MemberName("Title", "FirstName MiddleName", "Surname");
        MemberName actual;
        actual = TdsTransformer.SplitTdsName(tdsName);
        Assert.AreEqual(expected, actual);
    }

    [TestMethod()]
    public void SplitTdsNameTest_SimpleValidName3()
    {
        string tdsName = "FirstName MiddleName Surname";
        MemberName expected = new MemberName("", "", "FirstName MiddleName Surname");
        MemberName actual;
        actual = TdsTransformer.SplitTdsName(tdsName);
        Assert.AreEqual(expected, actual);
    }

其余测试均为同一类型。

I have a bunch of unit tests in unit test class.

When I run each one individually, they all pass, but when I run them all at the same time, the first one passes and the rest fail:

System.ArgumentException: An item with the same key has already been
added

Could anyone tell me why? And how I need to remedy the error?

Sample:

 public TestContext TestContext
    {
        get
        {
            return testContextInstance;
        }
        set
        {
            testContextInstance = value;
        }
    }

    #region Additional test attributes
    #endregion

    /// <summary>
    ///A test for SplitTdsName
    ///</summary>
    [TestMethod()]
    public void SplitTdsNameTest_SimpleValidName1()
    {            
        string tdsName = "Title FirstName Surname";
        MemberName expected = new MemberName("Title", "FirstName", "Surname");
        MemberName actual;
        actual = TdsTransformer.SplitTdsName(tdsName);
        Assert.AreEqual(expected, actual);            
    }

    /// <summary>
    ///A test for SplitTdsName
    ///</summary>
    [TestMethod()]
    public void SplitTdsNameTest_SimpleValidName2()
    {
        string tdsName = "Title FirstName MiddleName Surname";
        MemberName expected = new MemberName("Title", "FirstName MiddleName", "Surname");
        MemberName actual;
        actual = TdsTransformer.SplitTdsName(tdsName);
        Assert.AreEqual(expected, actual);
    }

    [TestMethod()]
    public void SplitTdsNameTest_SimpleValidName3()
    {
        string tdsName = "FirstName MiddleName Surname";
        MemberName expected = new MemberName("", "", "FirstName MiddleName Surname");
        MemberName actual;
        actual = TdsTransformer.SplitTdsName(tdsName);
        Assert.AreEqual(expected, actual);
    }

The remaining tests are all of the same type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北风几吹夏 2024-12-20 10:24:33

您的测试不遵循独立原则,因此一项测试不应影响其他测试。

查看您提供的代码我可以假设问题是
TdsTransformer.SplitTdsName() 缓存一些数据。我建议清理所有共享变量:

[TestCleanup()]
public void Cleanup()
{
   // cleanup all shared variables     
}

有用的链接:

Your tests not following Independent principle, so one test should not affect other tests.

Looking in the code you've provided I can assume that the problem that
TdsTransformer.SplitTdsName() cache some data. I would suggest cleanup all shared variables:

[TestCleanup()]
public void Cleanup()
{
   // cleanup all shared variables     
}

Useful links:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文