4

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;
            }
        }
    }
}
John Sprad
  • 85
  • 7
  • You should be able to accomplish this through Assertions and Exceptions. Is this what you mean by errors? – Ryan Gates Jan 22 '13 at 21:21
  • Do you have any breaks in the code like (this is pseudo-code) `onErrorGotoNext()` or `onErrorEndTest()`? – Brian Jan 22 '13 at 21:42
  • I tried throwing an Exception from my validation plug-in, and it did show the exception text in the log, but it still kept going with the rest of the requests in the test. I need it to stop dead and not do any further requests on that test case. – John Sprad Jan 22 '13 at 21:43
  • Couldn't you use the `break` keyword then? Can you post the code, please? – Brian Jan 22 '13 at 21:43

2 Answers2

2

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.

John Sprad
  • 85
  • 7
1

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

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

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.

chaliasos
  • 9,659
  • 7
  • 50
  • 87