【问题标题】:How can I Fail a WebTest?我怎样才能使 WebTest 失败?
【发布时间】:2008-10-22 04:11:24
【问题描述】:

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

除了反映WebTest 设置一个私有成员变量来指示失败之外,我还有什么遗漏的吗?

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

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

以下是将 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 框架并没有真正使用它来确定测试通过/失败结果。

【问题讨论】:

    标签: c# unit-testing mstest webtest


    【解决方案1】:

    Outcome property 设置为失败

    Outcome = Outcome.Fail;
    

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

    【讨论】:

    • 应该更清楚...我已经尝试过这两种方法。 (编辑问题以反映这一点)
    • [Microsoft.VisualStudio.QualityTools.WebTestFramework, Version=9.0.0.0] Microsoft.VisualStudio.TestTools.WebTesting.WebTest.set_Outcome(Microsoft.VisualStudio.TestTools.WebTesting.Outcome 值) 是公开的跨度>
    • 不是根据我使用的 dll。 Oucome 是一个公共吸气剂,但没有公共二传手。我使用反射来校准 set_Outcome (这是私有的),但它仍然对测试结果没有影响(即它直到不会失败)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      您可以通过添加始终失败的验证规则来使测试始终失败。例如,您可以这样编写失败验证规则:

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

      【讨论】:

        【解决方案4】:

        适用于声明式测试(以及编码)的解决方案是:

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

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

        【讨论】:

          【解决方案5】:

          首先,我正在使用 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
          

          我知道这个话题很老,但我希望它对某人有所帮助:)

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              猜你喜欢
              • 2017-06-29
              • 1970-01-01
              • 2023-01-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-11-17
              • 2019-09-01
              • 2013-09-05
              相关资源
              最近更新 更多