jazzcat
jazzcat

Reputation: 4441

How do I programmatically pause a unittest?

I'm using visual-studio built-in unit-testing framework - Microsoft.VisualStudio.TestTools.UnitTesting namespace.

I need to pause unit-test execution from the test code (and, preferably, allow the developer to resume the test by pressing some button or something).

Is there any built-in way to do that?

I know I can use a Thread.Sleep() to simply pause for several seconds, but is there a way to pause/resume or prompt the user somehow? Probably another obvious solution would be to call MessageBox.Show() but that'd require a reference to the whole Windows.Forms family...

P.S. The reason behind this: I have a "fuzzy" integration test that sometimes fails, but I can't tell why exactly (it's a UI test that automates a browser via Selenium, and the browser screen flashes quickly and I can't tell what happened there...). And for some reason I cannot catch this failure when debugging a test, only when running it regularly...

UPDATE: I guess there's no built-in way to do that, and it actually makes sense, since tests should run without any user interaction by design. Guess, a simple Sleep() or even Console.ReadLine() to simply freeze execution, until developer clicks "cancel" in the test-explorer.

Upvotes: 1

Views: 2856

Answers (1)

MistyK
MistyK

Reputation: 6222

If you know that event happened in the code i.e exception was thrown then you can wait for debugger until it's attached.

                Console.WriteLine("Waiting for debugger to attach");
                 if(WeirdBehaviourOccurred)
                   while (!Debugger.IsAttached)
                   {
                      Thread.Sleep(100);
                   }
                Console.WriteLine("Debugger attached");

Upvotes: 1

Related Questions