使用 VS2010 的负载测试功能和记录的网络测试。
我在记录的网络测试中遇到级联错误问题。也就是说,如果一个请求失败,其他几个请求也会失败。这会在日志中造成很多困惑,因为通常只有第一个错误是相关的。
有没有办法让失败的验证规则在失败时终止网络测试,而不运行其余的请求?
(要明确的是,我仍然想继续进行一般的负载测试,只是停止该特定测试用例的特定迭代)
下面是一些示例代码,演示了我正在尝试做的事情:
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;
}
}
}
}
最佳答案
我找到了一个很好的解决方案。有个博客here详细说明了它,但简短的版本是使用 e.WebTest.Stop()。这将中止当前测试的当前迭代,同时根据需要保持运行的其余部分完好无损。
关于c# - 如果发生错误,则中止网络测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14468173/