4

I have written 5 test methods in Visual Studio.

Now I want to exit the (test) program if my 2nd test case fails. i.e. Suppose my code reaches the 2nd test case and it fails, then the entire to be run test suite should be aborted.

How can I do that?

chaliasos
  • 9,659
  • 7
  • 50
  • 87
theVincentTor
  • 131
  • 1
  • 1
  • 7

2 Answers2

3

I think you are looking for Ordered Tests.

Create an Ordered Test and add the Unit Tests with the order you want. When one of the Unit Test fails the rest are not executed:

enter image description here

chaliasos
  • 9,659
  • 7
  • 50
  • 87
  • How can I execute all, even one test case fails ? – kbvishnu Sep 24 '12 at 14:43
  • Using an `Ordered Test` you cannot. If you need to execute them all you have to create a "Parent" Test that contains (executes) all other tests. But in that case your tests need to be completely unaffected one by the other. Check this [link](http://stackoverflow.com/questions/8931678/ordering-coded-ui-tests-and-results-in-test-manager/9118815#9118815). – chaliasos Sep 24 '12 at 15:04
  • I just found that it is very. There is a parameter when you create the ordered test called `Continue after failure`. You can set this to 'true' and then it will execute all the tests even if one failed. – chaliasos Oct 10 '12 at 13:30
  • Ordered test and continue after failure are described in detail in this blog http://binaryclips.com/2013/08/13/coded-ui-test-how-to-continue-execution-after-test-case-is-failed/ – joinsaad Mar 04 '16 at 11:21
1

You can always exit the whole "program" you are currently running by using Environment.Exit(). Do not forget to also specify an exit code (normally, an exit code of 0 if there were no errors, > 0 if an error occurred).

Actually I don't think it's useful to end your program if some unit tests happen to fail. However it is possible by using the following statement:

Environment.Exit(0)

In order to run different (in your case 5) unit tests in a row you can use Test Lists, please refer to "How to use test lists" for more information.

Mat
  • 202,337
  • 40
  • 393
  • 406
Joe Black
  • 155
  • 1
  • 1
  • 6
  • Matten, I didn't knew that NUnit doesn't preserve the order of exe. thanks for the info, I found the alternative for doing it :) – theVincentTor Jul 10 '12 at 09:39