Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Fail a WebTest?

Tags:

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.