I'm trying to write a C# unit test with VS 2008's built-in unit testing framework and the method I'm testing calls Environment.Exit(0)
. When I call this method in my unit test, my unit test is Aborted. The method should indeed be calling Exit
, and I want a way to test that it does, and also to test the exit code that it uses. How might I do this? I looked at Microsoft.VisualStudio.TestTools.UnitTesting Namespace but didn't see anything that looked relevant.
[TestMethod]
[DeploymentItem("myprog.exe")]
public void MyProgTest()
{
// Want to ensure this Exit's with code 0:
MyProg_Accessor.myMethod();
}
Meanwhile, here's the gist of the code that I want to test:
static void myMethod()
{
Environment.Exit(0);
}
Edit: here's 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);