views:

822

answers:

5

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.

+1  A: 

Set the Outcome property to Fail:

Outcome = Outcome.Fail;

There's also an Assert.Fail() in the Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly.

Mark Cidade
Should have been clearer... I had already tried both of these. (edited question to reflect this)
craigb
[Microsoft.VisualStudio.QualityTools.WebTestFramework, Version=9.0.0.0] Microsoft.VisualStudio.TestTools.WebTesting.WebTest.set_Outcome(Microsoft.VisualStudio.TestTools.WebTesting.Outcome value) is public
Mark Cidade
Not according to the dll I'm using. Oucome is a public getter, but there's no public setter. I used reflection to cal the set_Outcome (which is private) but it still has no effect on the test result (i.e. it till does not Fail)
craigb
+1  A: 

The Outcome property will set the public at vsts 2010 :-)

+1  A: 

How about:

Assert.IsTrue(false);

I haven't verified it, so it's possible it'd get reported as an error just like Assert.Fail(), and I agree it's a hack. But since Assert.Fail() doesn't seem to work the way you expect it to, and neither does setting Outcome, I don't see another choice.

CodeMangler
+1  A: 

You make a test always fail by adding a validation rule that always fails. For example, you could write a fail validation rule like this:

public class FailValidationRule : ValidationRule
{
    public override void Validate(object sender, ValidationEventArgs e)
    {
        e.IsValid = false;
    }
}

Then attach the new validation rule you your webtest's ValidateResponse event, like so:

public class CodedWebTest : WebTest
{
    public override IEnumerator<WebTestRequest> GetRequestEnumerator()
    {
        WebTestRequest request1 = new WebTestRequest("http://www.google.com");
        FailValidationRule failValidation = new FailValidationRule();
        request1.ValidateResponse += new EventHandler<ValidationEventArgs>(failValidation.Validate);
        yield return request1;
    }
}
nathandelane
A: 

Set the value of Outcome in the PostWebTest event handler.

Nate Noonen