繁体   English   中英

单元测试在一起运行时失败,单独传递

[英]Unit Tests fail when run together, pass individually

所以我在单元测试中遇到了一些问题,我不能在这里复制和过去,但我会尽我所能。

问题似乎是,如果我一个接一个地运行测试,一切都按预期工作,但如果我告诉它一起运行测试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.");

    }

所有的单元测试基本上都是副本和过去的,其中包括对e.property的更改,并且可能更改为其中一个i数字,

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

是否有一些我缺少的东西,以确保范围清除所有变量和测试之间的一切。 或强制串行执行。

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

Assert.AreEqual("(12345 OR 55555) AND !65232", functionCalled(t.itemNumbers, Globals.masterList, ref tmp), "Wrong obj returned.");

functionCalled通过其他函数处理修改后的列表,结果是你有不同的输出,结果是单元测试失败

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM