Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2008 unit tests - assert method exits

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);
like image 653
Sarah Vessels Avatar asked Jul 13 '09 19:07

Sarah Vessels


3 Answers

You'll need to create a wrapper for the Environment class, then use the wrapper in your code. For your unit tests, inject a mock version of the wrapper. The following example uses RhinoMocks to verify that the method calls the wrapper with the expected argument.

public class EnvironmentWrapper
{
    public virtual void Exit( int code )
    {
        Environment.Exit( code );
    }
}


public class MyClass
{
    private EnvironmentWrapper Environment { get; set; }

    public MyClass() : this( null ) { }

    public MyClass( EnvironmentWrapper wrapper )
    {
        this.Environment = wrapper ?? new EnvironmentWrapper();
    }

    public void MyMethod( int code )
    {
        this.Environment.Exit( code )
    }
}


[TestMethod]
public void MyMethodTest()
{
     var mockWrapper = MockRepository.GenerateMock<EnvironmentWrapper>();

     int expectedCode = 5;

     mockWrapper.Expect( m => m.Exit( expectedCode ) );

     var myClass = new MyClass( mockWrapper );

     myclass.MyMethod( expectedCode );

     mockWrapper.VerifyAllExpectations()
}
like image 178
tvanfosson Avatar answered Oct 19 '22 13:10

tvanfosson


This sounds like an incredibly bad idea. Environment.Exit(0), will obviously do as prescribed, hence why your unit testings are breaking.

If you really want to still test this you can by launching a seperate process and checking the return code- have a look at wrapping it up in Process.Start.

I guess another option is factoring this code out and injecting a test spy, or using a mock object to verify correct behaviour.

Perhaps you can do something with Typemock Isolator- I believe this lets you mock static methods.

like image 25
RichardOD Avatar answered Oct 19 '22 12:10

RichardOD


You won't be able to test this - Environment.Exit kills the application completely. This means that any AppDomain that uses this code will be unloaded completely, whether it is your production application or the unit testing framework.

like image 3
Andrew Hare Avatar answered Oct 19 '22 13:10

Andrew Hare