繁体   English   中英

如何通过WebTest失败?

[英]How can I Fail a WebTest?

我正在使用Microsoft WebTest,并希望能够执行与NUnit的Assert.Fail()类似的Assert.Fail() 我提出的最好的方法是throw new webTestException()但这在测试结果中显示为Error而不是Failure

除了在WebTest上反映设置私有成员变量以指示失败之外,还有什么我错过了吗?

编辑:我也使用了Assert.Fail()方法,但是当从WebTest中使用时,这仍然显示为错误而不是失败,并且Outcome属性是只读的(没有公共设置器)。

编辑:好吧,现在我真的很难过。 我使用反射将Outcome属性设置为Failed,但测试仍然通过!

这是将Oucome设置为失败的代码:

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

这是我试图失败的代码:

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

Outcome是设置为Oucome.Fail但显然WebTest框架并没有真正使用它来确定测试通过/未通过结果。

Outcome属性设置为Fail

Outcome = Outcome.Fail;

Microsoft.VisualStudio.QualityTools.UnitTestFramework程序Assert.Fail()还有一个Assert.Fail()

Outcome属性将在2010年的vsts上公开:-)

可以在声明性测试(以及编码)中使用的解决方案是:

  • 编写在上下文中存在某个上下文参数(例如“FAIL”)时失败的验证规则
  • 当你想触发失败时,设置Context Parameter并调用WebTest.Stop()
  • 将验证规则添加为WebTest级别规则(而不是请求级别),以便它可以在所有请求上运行

我认为这是可以做到的简洁。

首先,我正在使用VB.net,但我也试图将结果设置为失败,然后才发现它不起作用(这使我在这里)。

我终于设法通过抛出异常来做到这一点:

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

我知道这个话题很老但是我希望无论如何都能帮助别人:)

通过添加始终失败的验证规则,您始终会使测试失败。 例如,您可以编写一个失败验证规则,如下所示:

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

然后在webtest的ValidateResponse事件中附加新的验证规则,如下所示:

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

在PostWebTest事件处理程序中设置Outcome的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM