How can I Fail a WebTest?

Posted by craigb on Stack Overflow See other posts from Stack Overflow or by craigb
Published on 2008-10-22T04:11:24Z Indexed on 2010/05/06 14:58 UTC
Read the original article Hit count: 624

Filed under:
|
|
|

I'm using Microsoft WebTest and want to be able to do something similar to NUnit's Assert.Fail(). The best i have come up with is to throw new webTestException() but this shows in the test results as an Error rather than a Failure.

Other than reflecting on the WebTest to set a private member variable to indicate the failure, is there something I've missed?

EDIT: I have also used the Assert.Fail() method, but this still shows up as an error rather than a failure when used from within WebTest, and the Outcome property is read-only (has no public setter).

EDIT: well now I'm really stumped. I used reflection to set the Outcome property to Failed but the test still passes!

Here's the code that sets the Oucome to failed:

public static class WebTestExtensions
{
    public static void Fail(this WebTest test)
    {
        var method = test.GetType().GetMethod("set_Outcome", BindingFlags.NonPublic | BindingFlags.Instance);
        method.Invoke(test, new object[] {Outcome.Fail});
    }
}

and here's the code that I'm trying to fail:

    public override IEnumerator<WebTestRequest> GetRequestEnumerator()
    {
        this.Fail();
        yield return new WebTestRequest("http://google.com");
    }

Outcome is getting set to Oucome.Fail but apparently the WebTest framework doesn't really use this to determine test pass/fail results.

© Stack Overflow or respective owner

Related posts about mstest

Related posts about webtest