【问题标题】:Abort a webtest if an error occurs如果发生错误,中止 webtest
【发布时间】:2013-01-22 21:17:07
【问题描述】:

使用 VS2010 的负载测试功能和记录的 webtests。

我在记录的网络测试中遇到级联错误问题。也就是说,如果一个请求失败,其他几个请求也会失败。这会在日志中造成很多混乱,因为通常只有第一个错误是相关的。

有没有办法让失败的验证规则在失败时终止 Web 测试,而不运行其余的请求?

(要清楚,我仍然想继续进行一般的负载测试,只需停止该特定测试用例的特定迭代)

这是一些示例代码,演示了我正在尝试做的事情:

using System.ComponentModel;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace TestPlugInLibrary
{
    [DisplayName("My Validation Plugin")]
    [Description("Fails if the URL contains the string 'faq'.")]
    public class MyValidationPlugin : ValidationRule
    {
        public override void Validate(object sender, ValidationEventArgs e)
        {
            if (e.Response.ResponseUri.AbsoluteUri.Contains("faq"))
            {
                e.IsValid = false;
                // Want this to terminate the running test as well.
                // Tried throwing an exception here, but that didn't do it.
            }
            else
            {
                e.IsValid = true;
            }
        }
    }
}

【问题讨论】:

  • 您应该能够通过断言和异常来完成此操作。这就是你所说的错误吗?
  • 您的代码中是否有任何中断,例如(这是伪代码)onErrorGotoNext()onErrorEndTest()
  • 我尝试从我的验证插件中抛出一个异常,它确实在日志中显示了异常文本,但它仍然与测试中的其余请求保持一致。我需要它停止死机并且不对那个测试用例做任何进一步的请求。
  • 你不能使用break关键字吗?可以发一下代码吗?

标签: c# visual-studio-2010 mstest load-testing webtest


【解决方案1】:

我找到了一个很好的解决方案。有一个博客here 详细介绍了它,但简短的版本是使用 e.WebTest.Stop()。这会中止当前测试的当前迭代,同时根据需要保持其余运行不变。

【讨论】:

    【解决方案2】:

    使用Assert.Fail()。这将停止测试并抛出一个AssertFailedException,就像任何失败的断言一样。

    if (e.Response.ResponseUri.AbsoluteUri.Contains("faq"))
    {
        e.IsValid = false;
        Assert.Fail("The URL contains the string 'faq'.");
    }
    

    这只会停止特定的测试。在负载测试结束时,您可以看到由于此异常而失败的测试总数。

    【讨论】:

      猜你喜欢
      • 2019-09-04
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 2021-07-09
      • 2012-08-09
      • 1970-01-01
      • 2019-01-29
      相关资源
      最近更新 更多