单元测试一起运行时失败,单独通过 [英] Unit Tests fail when run together, pass individually

查看:77
本文介绍了单元测试一起运行时失败,单独通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的单元测试有一些问题,我不能在这里复制和粘贴它们,但我会尽我所能.

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.

问题似乎是,如果我一个一个地运行测试,一切都会按预期进行,但是如果我告诉它一起运行所有测试,则 1/5 将通过,

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,

[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.");

    }

所有的单元测试基本上都是一个副本和过去,对 e.property 进行了修改,并且可能对 i 个数字之一进行了更改,

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,

测试旨在检查用户输入的边缘情况.

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.

推荐答案

我建议考虑Globals.masterList.Add(e);假设您的单元测试在五个线程中执行.这意味着 Globals.masterList.Add(e); 将被执行五次或 masterList 将被五个不同的线程修改.然后你有下一行代码:

I propose to consider Globals.masterList.Add(e); Let's say your unit test executed in five threads. It means that Globals.masterList.Add(e); will be executed five times or masterList will be modified by five different threads. 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 处理其他函数修改后的列表,结果你有不同的输出,结果单元测试失败

functionCalled deals with modified list by other functions, and as outcome you have different output from it and as result failed unit test

这篇关于单元测试一起运行时失败,单独通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆