I am currently writing system tests for a legacy windows forms application.
In my test, I call the Program.Main()
method, which in turn calls Application.Run(new MainForm());
at some point.
Is there a way to replace Application.Run() with something that I can control?
Specifically, I need to be able to stop the execution and catch unhandled exceptions.
You could modify Program.Main to accept a form as an input parameter, with a default of MainForm. Reference types can't have non-null defaults but we can accomplish the same thing by using two prototypes:
When you run the program in the normal way, it'll use a
MainForm
.But when you run it from your test project, you can call it like this:
And then you can control it.