简体   繁体   English

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

[英]Exception whilst running all unit tests at the same time

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 System.ArgumentException:具有相同键的项已添加

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 = "Mr Daniel Trunley";
        MemberName expected = new MemberName("Mr", "Daniel", "Trunley");
        MemberName actual;
        actual = TdsTransformer.SplitTdsName(tdsName);
        Assert.AreEqual(expected, actual);            
    }

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

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

The remaining tests are all of the same type. 其余测试均为同一类型。

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. 查看您提供的代码,我可以假设TdsTransformer.SplitTdsName()缓存一些数据的问题。 I would suggest cleanup all shared variables: 我建议清理所有共享变量:

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

Useful links: 有用的链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当多个单元测试复制同一文件时,运行所有单元测试失败 - When multiple unit tests copy the same file, running all unit tests fail 在构建服务器上使用Fakes运行单元测试的异常 - Exception running unit tests with Fakes on build server Resharper Unit测试未运行 - Resharper Unit Tests not running 单元测试在NCrunch中运行但不在Resharper中运行 - Unit tests running in NCrunch but not in Resharper 在团队合作中并行运行单元测试? - Running unit tests in parallel in teamcity? 如果一次运行一个单元测试则传递正常,如果运行“解决方案中的所有测试”,则为FileLoadException - Unit Tests pass fine if run one at a time, FileLoadException if run “All Tests in Solution” 使用事件进行集成测试 - 同时运行多个测试时的异常 - Integration testing with Events - Exceptions when running multiple tests at the same time "在一个单元测试项目中运行多个单元测试" - Running multiple Unit Tests in an unit test project 我正在获取System.BadImageFormatException:操作系统在运行单元测试时无法运行异常 - I am getting System.BadImageFormatException : The operating system cannot run exception while running unit tests 单元测试未运行,因为“无法加载文件或程序集 System.IO”异常 - Unit Tests not running because “Could not load file or assembly System.IO” exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM