c# - Abort a webtest if an error occurs

问题描述:

Using VS2010's load test feature with recorded webtests.

I have problems with cascading errors in my recorded webtests. That is, if one request fails, several other requests will also fail. This creates a lot of clutter in the logs, since usually only the first error is relevant.

Is there a way to make a failed validation rule terminate the web test at the point of failure, and NOT run the rest of the requests?

(to be clear, I still want to continue with the load test in general, just stop that specific iteration of that specific test case)

Here is some sample code that demonstrates what I'm trying to do:

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;

}

}

}

}

网友答案:

Use the Assert.Fail(). This will stop the Test and will throw an AssertFailedException, just like in any failed assertion.

This will stop only the specific test. At the end of the load test you can see the total number of tests failed with this exception.

网友答案:

I found a good solution to this. There's a blog here that details it in full, but the short version is to use e.WebTest.Stop(). This aborts the current iteration of the current test, while leaving the rest of the run intact as needed.