[英]Unit Tests fail when run together, pass individually
So I am having a few issues with my unit Tests, I cant just copy and past them here but I will give what I can. 所以我在单元测试中遇到了一些问题,我不能在这里复制和过去,但我会尽我所能。
The problem seems to be that if I run the tests one by one everything works as intended, but if I tell it to run the tests all together 1/5 will pass, 问题似乎是,如果我一个接一个地运行测试,一切都按预期工作,但如果我告诉它一起运行测试1/5将通过,
[TestMethod]
public void ObjTest()
{
//Arrange - multiple ecus and items
var t = new var();
t.itemNumbers = new List<ItemNumber>();
obj e = new obj();
e.property = "(12345 OR 55555) AND !65232";
Globals.masterList.Add(e);
ItemNumber i = new ItemNumber();
i.num= "12345";
ItemNumber i1 = new ItemNumber();
i1.num= "55555";
ItemNumber i2 = new ItemNumber();
i2.num= "55556";
t.itemNumbers.Add(i);
t.itemNumbers.Add(i1);
t.itemNumbers.Add(i2);
ICollection<Iinterface> tmp = new List<Iinterface>();
//act, process the ecu and item lists
;
functionCalled(t.itemNumbers, Globals.masterList, ref tmp);
//assert, there should be only 2 added to the list
Assert.AreEqual(1, tmp.Count, " ");
Assert.AreEqual("(12345 OR 55555) AND !65232", functionCalled(t.itemNumbers, Globals.masterList, ref tmp), "Wrong obj returned.");
}
All of the unit tests are basically a copy and past with the chages to e.property and possibly a change to one of the i numbers, 所有的单元测试基本上都是副本和过去的,其中包括对e.property的更改,并且可能更改为其中一个i数字,
the tests are designed to check edge cases cased by user input. 测试旨在检查用户输入的边缘情况。
is there something I am missing to ensure scope clears all of the variables and everything between tests. 是否有一些我缺少的东西,以确保范围清除所有变量和测试之间的一切。 or force serial execution.
或强制串行执行。
I propose to consider Globals.masterList.Add(e);
我建议考虑
Globals.masterList.Add(e);
Let's say your unit test executed in five threads. 假设您的单元测试在五个线程中执行。 It means that
Globals.masterList.Add(e);
这意味着
Globals.masterList.Add(e);
will be executed five times or masterList will be modified by five different threads. 将被执行五次或者masterList将被五个不同的线程修改。 Then you have next line of code:
然后你有下一行代码:
Assert.AreEqual("(12345 OR 55555) AND !65232", functionCalled(t.itemNumbers, Globals.masterList, ref tmp), "Wrong obj returned.");
functionCalled deals with modified list by other functions, and as outcome you have different output from it and as result failed unit test functionCalled通过其他函数处理修改后的列表,结果是你有不同的输出,结果是单元测试失败
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.