Environment.Exit – don’t use it

We all know the seven deadly sins – sloth, greed, envy, and so on. Well, today I found an eighth. I set up an automated system-test for a report generator we use at work. Basically, it will run the tool, and then interrogate the output to ensure it is correct (like a very large unit test). I wrote the test harness in the inbuilt test system in Visual Studio 2008, as I’m familiar with it.

Initially I set it up to call the “Main” method directly (it’s a console application), but quickly discovered that this wouldn’t work because the original developer had sprinkled Enviroment.Exit’s like they were fucking confetti. The effect of this was, of course, that whenever an exception was thrown, or the tool successfully completed (so, every single run, one way or the other), the test script would be killed so it would report as aborted. At this point, I looked like this:
Angry baby

The workaround I came up with in the end, as there was far too much code to re-engineer it, was to use the Process class to launch the app, and then check it’s return code. The big disadvantage here is that I can’t debug the code straight from the unit test.

I’m sure there’s some clever way to attach a debugger through code, but it would be better if it just didn’t use Environment.Exit in the first place.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s