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:
class Program
{
//Normal entry point
static public void Main()
{
Main(new MainForm());
}
//Internal & test entry point
static public void Main(Form form)
{
DoSomeSetup();
Application.Run(form);
}
}
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:
Program.Main(new FormICanControl());
And then you can control it.
//Arrange
var t = new TestForm();
//Act
Program.Main(t);
t.ExecuteSomeTest();
//Assert
Assert.AreEqual(t.ResultCode, 0, "Test failed.");