-1

I am creating some tests for the validation tool I have written to validate a file. When an error is found with the file that it is checking I call Environment.Exit(). This works fine to quit the program when when running normally.

However when I am running my tests and it comes across the Environment.Exit() the test terminates but I want to continue testing afterwards and check the files that my program has produced. When the test terminates it gives the message:

The agent process was stopped while the test was running.

Is there anyway I can stop the test from ending prematurely due to this? Is there another way I can terminate the program but continue the tests? Should I consider multithreading?

Or should I just reconsider my implementation?

Community
  • 1
  • 1
Dan Smith
  • 280
  • 3
  • 13
  • How are you validating a file? Are you reading its content? Perhaps you can do stream.Close(); to that file and carry on. – ACS Aug 14 '14 at 23:49

2 Answers2

1

My recommendation would be that when there is an error file checking your file throw the exception(if its not throwing already). In your production code call this method with another surrounded by try/catch and inside catch you can do Environment.Exit().

Now write two different tests One to test a valid file. In this case it should go through all the assertions without any issue. Second to test files with error condition. In this case you can use something similar to ExpectedException of nunit to confirm that its throwing appropriate exception.

So in short please reconsider your implementation.

Lav
  • 1,850
  • 15
  • 17
1

Remove the Environment.Exit and throw an exception which isn't caught by anything except the base exception handler (which can print a message or not then quit).

The exceptions thrown during test run won't be caught, so the test will fail gracefully and move on. When you run the app normally, the exception causes the stack to unwind until the base exception handler which just exits the application.

Look into AppDomain.UnhandledException and if you want to also catch exceptions on different threads, Application.ThreadException

Basic
  • 26,321
  • 24
  • 115
  • 201