I ran into a case today where one of our unit tests was failing when we ran it, but would pass when we debugged it. We’re using MSTest and had recently branched the project in TFS, and I was working on changes in the new branch. We were getting a “method not found” error when running the test which immediately made me think that Visual Studio was using a cached version of the class under test.
It turned out that the culprit was our use of Code Coverage. While working in the initial branch, we had turned on Code Coverage for the project we were testing. However Visual Studio will look for the project dll two different ways depending on your solution structure. If the project is in the solution directory, it will look for the dll using a relative path such as:
However, if you like to keep your solutions separate from the projects like we are (we have a separate folder containing all our solutions), it will use an absolute path to find the dll.
This worked fine until we created a new branch of the code. I mapped the new branch to a different location locally and Visual Studio was still using the old version when running Code Coverage. (Which why it would pass when we debugged the tests, Code Coverage isn’t run when you debug tests.)
So after branching, your Code Coverage list could potentially look something like this.
Note that there’s two ClassLibrary1.dll artifacts in the list. The first actually points to the initial branch of the code. The second points to the newly created branch. Unchecking the first and checking the second fixes the issue.
On a side note, I can’t wait to drop MSTest and go back to NUnit!