简体   繁体   中英

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,

[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,

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); 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 deals with modified list by other functions, and as outcome you have different output from it and as result failed unit test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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