简体   繁体   中英

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

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. I would suggest cleanup all shared variables:

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

Useful links:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM