我在单元测试类中有一堆单元测试。
当我单独运行每一个时,它们都会通过,但是当我同时运行它们时,第一个通过,其余的通过:
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 = "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);
}
其余测试都属于同一类型。
答案 0 :(得分:7)
您的测试不遵循独立原则,因此一项测试不应影响其他测试。
查看您提供的代码,我可以假设这个问题
TdsTransformer.SplitTdsName()
缓存一些数据。我建议清理所有共享变量:
[TestCleanup()]
public void Cleanup()
{
// cleanup all shared variables
}
有用的链接: