3

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.

Codor
  • 17,447
  • 9
  • 29
  • 56
asuppa
  • 571
  • 1
  • 11
  • 27
  • 4
    My guess is that `Globals.masterList.Add(e);` is your problem - having a global element to your tests means that they're not separate from each other. It smells bad. – dav_i Jan 22 '15 at 15:56
  • 2
    I will suck my pinkytoe if it hasn't got something to do with `Globals.masterList.Add(e)`. – Jeroen Vannevel Jan 22 '15 at 15:56
  • as @dav_i suggests you are probably hit by a static member being shared between your lists. i, i1 and i2 are then added by the threads and your assertions about their presence becomes unreliable. – faester Jan 22 '15 at 15:58
  • 9
    I'd also *strongly* recommend against naming a type `var`. That's just *designed* to confuse people... – Jon Skeet Jan 22 '15 at 16:01
  • 1
    Not an issue but -var t = new var();- ... – Benji_9989 Jan 22 '15 at 16:06
  • Do you have to use `Globals.masterList`? Can you mock it out instead so you can isolate it in each test? – tyh Jan 22 '15 at 16:27
  • @JonSkeet var is not what it actually is, changed to hide class naming – asuppa Jan 22 '15 at 16:28
  • and it was the global ! I cant believe i didn't see that.... – asuppa Jan 22 '15 at 16:29
  • If you're going to use "dummy" names, I'd suggest avoiding ones which are contextual keywords then. – Jon Skeet Jan 22 '15 at 16:31
  • @JonSkeet I will keep that in mind next time I post to StackOverflow – asuppa Jan 22 '15 at 16:32

1 Answers1

1

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

Yuriy Zaletskyy
  • 4,983
  • 5
  • 34
  • 54