10

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.

Jim Counts
  • 12,535
  • 9
  • 45
  • 63
craigb
  • 16,827
  • 7
  • 51
  • 62

6 Answers6

3

Set the Outcome property to Fail:

Outcome = Outcome.Fail;

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

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • Should have been clearer... I had already tried both of these. (edited question to reflect this) – craigb Oct 22 '08 at 14:08
  • [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 Oct 22 '08 at 14:20
  • 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 Oct 23 '08 at 01:16
1

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

1

A solution that would work in declarative tests (as well as coded) is:

  • write a Validation Rule that fails when a certain Context Parameter (e.g. 'FAIL') is present in the Context
  • when you want to trigger the failure, set the Context Parameter and call WebTest.Stop()
  • add the Validation Rule as a WebTest-level rule (not request-level) so that it runs on all requests

I think that's as concise as it can be done.

agentnega
  • 3,478
  • 1
  • 25
  • 31
1

First off, I'm working with VB.net, but I also tried to set the outcome to fail before finding out it does not work (which brought me here).

I finally managed to do it by just throwing an exception :

Public Overrides Sub PostRequest(ByVal sender As Object, ByVal e As PostRequestEventArgs)

    If YourTest = True Then

        Throw New WebTestException("My test Failed")

    End If

    MyBase.PostRequest(sender, e)

  End Sub

I know this topic is old but I hope it helps someone anyway :)

yvandd
  • 11
  • 1
1

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;
    }
}
0

Set the value of Outcome in the PostWebTest event handler.

Nate Noonen
  • 1,371
  • 9
  • 19