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.
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: