If you are ever in the situation where you need to write a WebTestPlugin or WebTestRequestPlugin, you might find it useful to be able to feedback, into the test results, any errors that occur. Reading the docs for the WebTestRequest or WebTest objects you get passed does not immediately suggest a way to do this. One way I have found is to use the ValidateResponse Event and force the validation to fail. This will allow you to mark the request up as having failed:

image

and also allows you to provide more information about what went wrong on the details tab:

image

To archive this, when the error occurs, subscribe to the Request's ValidateResponse event. Oh and if you pop the actual error into the Context, you can use it later on.

catch (Exception ex)

{

    e.WebTest.Context["MyExceptionKey"] = ex;

    e.Request.ValidateResponse += new EventHandler<ValidationEventArgs>(Request_ValidateResponse);

}

Then in the event, we are going to fail the test using the IsValid property on the ValidationEventArgs object ...

void Request_ValidateResponse(object sender, ValidationEventArgs e)

{

    Exception ex = (Exception) e.WebTest.Context["MyExceptionKey"];

    e.IsValid = false;

    e.Message = ex.Message;

}

Strictly speaking this event is supposed to be where you perform any validation on the data that was returned from the server, but hey, you need to bend the rules occasionally ;-)

Neil