Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Tests failing when I Run All Tests but pass when I Debug

I'm using NUnit3 in Visual Studio 2017 and doing TDD. Something really strange is happening since I updated my code to make my latest test pass.

Now, 3 of my other tests are failing when I click Run All Tests, as below:

enter image description here

It is telling me that the actual and expected values in my Assert method are not equal.

However, when I put a breakpoint at the line where the Assert method is and start debugging, the stacktrace is showing that expected and actual are the same value and then the test passes, as below:

enter image description here

Am I doing something stupid or could there be a bug in VS2017 or NUnit or something?

This ever happen to anyone else?

[Edit: I should probably add that I have written each test as a separate class]

like image 525
Michael Hennigan Avatar asked May 13 '17 21:05

Michael Hennigan


2 Answers

The failing tests share a resource that affects them all when tested together. Recheck the affected tests and their subjects.

You should also look into static fields or properties in the subjects. They tends to cause issues if not used properly when designing your classes.

like image 57
Nkosi Avatar answered Oct 23 '22 19:10

Nkosi


Some subtle differences might occur. For instance if a first test change a state which affects the behavior of a second test, then the outcome of this 2nd test may not be the same if I run it alone.

An idea to help understand a test failure when a breakpoint can't be used, could be to add logging.

Anyway, to answer your questions:

This ever happen to anyone else?

Yes

Am I doing something stupid or could there be a bug in VS2017 or NUnit or something?

I bet that it's neither: just a case a bit more subtle

like image 28
gturri Avatar answered Oct 23 '22 21:10

gturri