Building Your First VS Ultimate Performance Test

I’ve been asked, how do you get started building performance tests in Visual Studio (VS). This post is an attempt at guiding some one with some VS experience in building a performance test using VS Ultimate.

Context:

  • API testing
  • C#

Prerequisites:

  • Some programming experience.
    • If you are familiar with some programming terminology and can write¬†code, you should be able to follow along with this tutorial.
  • Dummy API is active.
    • The specific code in the tutorial relies on a dummy API build using Mockable’s online service. The free service relies on my interaction to keep the dummy alive.
  • VS Ultimate
    • If you don’t have VS Ultimate, you won’t be able to use this tutorial.
    • You also need a little experience using VS.

The Tutorial:

  • Create a Web Performance and Load Test Project.
    • On the VS menu bar: File -> New -> Project…
    • On the LHS of the new window: Installed -> Templates -> Visual C# -> Test -> Web Performance and Load Test Project
    • Enter a name.
    • Choose a location.
    • Click OK.
  • A web and load test project is created with one .webtest file already created for you.
  • Record the API call.
    • Double click the .webtest file to open it in VS.
    • Click the Add Recording button.
    • In the browser that is opened, enter this URL, http://demo3838757.mockable.io/.
    • In the browser, click Stop.
  • Create the Web Test Coded file.
    • Double click the .webtest file in the solution explorer to open it in VS.
    • Click the Generate Code button.
    • Name the file and click OK.
  • At this point you can delete the .webtest file. It is no longer needed.
  • Parametrize the API URL.
    • Edit the .cs file.
      • Add the following line to the constructor.
this.Context.Add("Url", "http://demo3838757.mockable.io/");

Change the following line, contained in the GetRequestEnumerator method, from:

WebTestRequest request1 = new WebTestRequest("http://demo3838757.mockable.io/");

To:

WebTestRequest request1 = new WebTestRequest(this.Context["Url"].ToString());

Validate Response:

  • In the .cs file, import the WebTestFramework library.
using Microsoft.VisualStudio.QualityTools.WebTestFramework;
  • In the .cs file, add an expected result parameter.
    • Add the following line to the constructor.
      • I’ll show you in a future post how to make this look prettier.
this.Context.Add("ExpectedMessage", "{\n\t\"msg\":\"hello world\"\n}");
  • In the .cs file, create a new class.
public class MessageValidation : ValidationRule
{
    public override void Validate(Object sender, ValidationEventArgs e)
    {
        WebTestContext context = e.WebTest.Context;
        string responseString = e.Response.BodyString;
        string statusCode = e.Response.StatusCode.ToString();
        string expectedMessage = context["ExpectedMessage"].ToString();

        if (responseString.Equals(expectedMessage) &&
        statusCode.Equals("OK"))
        {
            e.IsValid = true;
            return;
        }
        e.IsValid = false;
        e.Message = "Invalid response";
    }
}

This class compares the response of an HTTP request to the “ExpectedMessage” parameter, and if they are equal and the request’s status code is equal to “OK”, marks the request as valid. If they are not equal or the request’s status code does not equal “OK”, the request is marked as not valid and sets an error message.

  • Execute the validation rule.
    • Add the following lines of code to the GetRequestEnumerator method, after the WebTestRequest but before the return value.
MessageValidation validation = new MessageValidation();
request1.ValidateResponse += new EventHandler<ValidationEventArgs>(validation.Validate);

These lines trigger the validation rule to run.

You can now right click anywhere in the GetRequestEnumerator method and select Run Coded Web Performance Test from the right click menu to run your test.

Up Next:

  • Parsing JSON with data structures and Jayrock
  • Using the performance test to create a load test.

Leave a Reply

Your email address will not be published. Required fields are marked *