I am trying to write a C # unit test using the VS 2008 built-in unit testing system and the method I am testing for calls Environment.Exit(0). When I call this method in my unit test, my unit test is canceled. This method really needs to be called Exit, and I want to test it, and also check the exit code that it uses. How can i do this? I looked at the Microsoft.VisualStudio.TestTools.UnitTesting namespace , but did not see anything that looked relevant.
[TestMethod]
[DeploymentItem("myprog.exe")]
public void MyProgTest()
{
MyProg_Accessor.myMethod();
}
Meanwhile, here is the gist of the code I want to test:
static void myMethod()
{
Environment.Exit(0);
}
Edit: here is the solution I used in my test method, thanks to RichardOD :
Process proc;
try
{
proc = Process.Start(path, myArgs);
}
catch (System.ComponentModel.Win32Exception ex)
{
proc = null;
Assert.Fail(ex.Message);
}
Assert.IsNotNull(proc);
proc.WaitForExit(10000);
Assert.IsTrue(proc.HasExited);
Assert.AreEqual(code, proc.ExitCode);