views:

941

answers:

5

In Visual Studio 2010, I have a number of unit tests. When I run multiple tests at one time using test lists, I sometimes reveive the following error for one or more of the tests:

The agent process was stopped while the test was running.

It is never the same test failing, and if I try to run the test again, it succeeds.

I found this bug report on Connect, which seems to be the same problem, but it does not offer a solution.

Has anyone else seen this behaviour ? How I can avoid it ?

Edit

I am still experiencing this bug, and so is many of my colleagues on the same software/hardware setup. I have evaluated the answers so far, but they don't resolve the problem. I am starting a bounty for a solution to this problem.

+1  A: 

Thanks for posting the question. I just ran into this problem and figured out a cause that you may be running into.

An asynchronous exception may have occurred

During my test setup, I create an object that queues a worker thread in the thread pool. If I run through debugging fast enough my code passes.

If the worker thread kicks off and has an error BEFORE the test setup completes, then I get a result of Aborted with no reasoning.

If the worker thread kicks off and has an error AFTER the test has begun, then I get a result of : Error - The agent process was stopped while the test was running.

Important to note: this is a component that I use throughout several of my tests. If the test framework encounters too many of these errors it aborts the rest of the tests.

Hope this helps

Brent VanderMeide
Thank you for the answer. I am aware that asynchronous exceptions might cause something similar to what I am seeing, but I am almost certain that this is not the case. Code is for a web app, and we don't do anything asynchronously. Also, it seems that which test that fails is random.
driis
A: 

I was having this problem, and it turned out to be a problem in my code which the Test Framework wasn't catching properly. A little accidental refactoring had left me with this code:

public void GetThingy()
{
    this.GetThingy();
}

This is of course an infinite recursion, and caused a StackOverflowException (I guess). What this caused was the dreaded: "The agent process was stopped while the test was running."

A quick code inspection showed me the problem, and my tests are now running fine. Hope this helps - might be worth inspecting the code looking for issues, or maybe extracting a bit into a console app and checking it works properly there.

Simon Steele
This is not the issue (I know because it is different tests that fail each time), but thank you for taking the time to answer.
driis
+5  A: 

I've just experienced the similar problem: some tests fail and they are different in different test runs. I don't know exactly the reason why it happens, but it began to occur when I added a finalizer to one of my classes. When I disable the finalizer - the problem disappears. When I turn the finalizer on - the problem comes back.

Right now I don't know how to overcome this.

Satorg
THANKS - this answer lead me to the solution. I only have a finalizer on a couple of types, and sure enough, removing them also removed the problem. Upon further investigation, I discovered a subtle bug in one finalizer, which occured only when an exception was thrown in the constructor, and the finalizer tries to finalize an object that is not fully constructed. Conclusion: If an exception occurs in a finalizer on a type, and that finalizer runs before all the tests has finished, Visual Studio will give the error I was facing; without any further explanation, and on random tests.
driis
I have no finalizers/destructors in my code ... ~MyClass() and get the same error. Running tests with Resharper are all green
Peter Gfader
The problem with uncaught exceptions in finalizers is a special case of uncaught exceptions in background tasks which can be started or scheduled (perhaps implicitly) by some test and can continue executing even if the test has been completed.
Satorg
A: 

In my case I had some unit-tests for a WCF-service. This WCF service was starting up 2 timers.
Those timers caused side effects.
--> I disable these timers by default and everything is fine!

BTW: I use WCFMock to fake the WCF service, so I have "real" unit tests around my WCF service

Peter Gfader
+2  A: 

This message is caused by an exception on a thread different from the executing test thread. All answers so far boil down to this simple explanation. It is a known bug in Visual Studio not to display any sensible information in that case.

When creating background threads to deal with receiving data, make sure to close them when the connection is lost.

while (true) {
    connection.ReceiveStuff ();
}

// try this instead:
while (connection.State == ConnectionState.Opened) {

And lo and behold, Visual Studio’s test runner totally chokes if a thread other than the executing test thread throws an exception: It gets swallowed and there’s no output, no chance to intercept and debug and no nothing except a burned-down smoldering mess that was supposed to be your unit test.

source

mafutrct