1

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.

jazzcat
  • 4,351
  • 5
  • 36
  • 37
  • Do not do that. Do a root cause analysis, find the side effect and fix it. Otherwise it's not a unit test but a integration/system test. – Thomas Weller Mar 21 '17 at 12:02
  • 1
    Doesn't a debugging breakpoint achieve this? – David Mar 21 '17 at 12:02
  • 1
    Do a `Console.Read()`? Though this sounds like a horribly shite approach and you should just fix the actual issue – Jeroen Vannevel Mar 21 '17 at 12:04
  • Why not pause in debugger and inspect the situation? – Sergey.quixoticaxis.Ivanov Mar 21 '17 at 12:05
  • @ThomasWeller yes, its a integration test. Due to the nature of Selenium, this bug only presents itself in some rare occasions, like once in a 100 times... I need to pause and look at things. And the issue is not present when I *debug* the test, only when its being run regularly – jazzcat Mar 21 '17 at 12:05
  • @David this is a weird bug that happens only when running a test, not debugging it. For some reason. Selenium is a glitchy little fella... – jazzcat Mar 21 '17 at 12:07
  • I think Selenium has Pause and WaitFor like most automation frameworks. – Crowcoder Mar 21 '17 at 12:11
  • @JeroenVannevel Console.Read works! but it just pauses indefinitely... – jazzcat Mar 21 '17 at 12:13
  • Well yeah, you need to pass in some data to the input stream so it stops blocking. Not entirely sure how all that is hooked up during test execution to be honest. I feel like you'll spend more time on doing this hacky workaround than you would on figuring out the core problem – Jeroen Vannevel Mar 21 '17 at 12:16
  • In looks like nunit console redirects standard input so that console read key does not work. So the only way is messagebox. Could use reflection to avoid static dependency – max630 Mar 09 '20 at 13:37

1 Answers1

1

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");
MistyK
  • 6,055
  • 2
  • 42
  • 76