同时运行所有单元测试时出现异常
我在单元测试类中有很多单元测试。
当我单独运行每个时,它们都会通过,但是当我同时运行它们时,第一个通过,其余的失败:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的测试不遵循独立原则,因此一项测试不应影响其他测试。
查看您提供的代码我可以假设问题是
TdsTransformer.SplitTdsName()
缓存一些数据。我建议清理所有共享变量:有用的链接:
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:Useful links: