How can I Fail a WebTest?
Asked Answered
E

6

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.

Ensheathe answered 22/10, 2008 at 4:11 Comment(0)
M
3

Set the Outcome property to Fail:

Outcome = Outcome.Fail;

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

Maugre answered 22/10, 2008 at 4:15 Comment(3)
Should have been clearer... I had already tried both of these. (edited question to reflect this)Ensheathe
[Microsoft.VisualStudio.QualityTools.WebTestFramework, Version=9.0.0.0] Microsoft.VisualStudio.TestTools.WebTesting.WebTest.set_Outcome(Microsoft.VisualStudio.TestTools.WebTesting.Outcome value) is publicMaugre
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)Ensheathe
C
1

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

Carrol answered 25/6, 2009 at 6:25 Comment(0)
P
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;
    }
}
Paramedical answered 6/5, 2010 at 14:53 Comment(0)
H
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.

Homunculus answered 9/5, 2012 at 5:52 Comment(0)
S
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 :)

Saponify answered 10/6, 2013 at 15:39 Comment(0)
A
0

Set the value of Outcome in the PostWebTest event handler.

Alika answered 7/5, 2010 at 20:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.