While preparing for my presentation Load and Performance Testing: How to do Transactional Root-Cause Analysis with Visual Studio Team System for Testers that I gave at the Boston .NET User Group on May 13th I came across certain load-testing topics. One was: How to randomize Input Data.
If you go with Visual Studio you can code your web tests in any .NET Language giving you the freedom to create random data by using e.g.: System.Random. If you however want to use the “nicer” UI Driven Web Test Development (that’s how I call it) – you are limited in your options. You add web requests – you can parameterize input values by using Context Parameters with hard coded values, you can reuse values extracted from a previous request or you can use data from an external data source like a database, CSV or XML file.
Basic random numbers for VSTS Web Tests
One thing that I missed was the ability to use basic random values, e.g.: a random number from 1 to 5 that would be used for a quantity field of a web form or a random username with the pattern “testuserX” where X is in the range of my test user accounts.
In order to do that there only seems to be one way – implementing a WebTest or WebTestRequest Plugin that generates random data and makes it available as Context Parameter to the test.
Sample: Randomizing username and password
Lets get back to my username/password example. Following illustration shows my original Web Test:
I extracted the hard coded values from the recorded web request into a context parameter – but still using the hard coded values. One option that I would have here – as indicated in the first paragraph – would be to make this test data driven by binding the Context Parameters to an external data source. But this is not what I want in my example. I really want to randomize the input data so that every web request uses a new random value. I therefore created a WebTestRequestPlugin as shown in the following image:
My plugin defines two properties that can be configured with a Parameter Name (or names) and a random value pattern. In the PreRequest override I use a random value generator that generates the random value based on the passed pattern and put the generated value in the Test Context.
Now I can use the plugin in my webtest as follows:
Now I can run my Web Test either “standalone” by defining the number of iterations or using it in a Load Test. Every time the WebRequest will be executed the WebTestRequestPlugin will create a new random value that will then be used by the request. This allows me to run this test over and over again – always using a different username and password.
Check out the option to create your own WebTestRequestPlugin or WebTestPlugin. This extension option gives you more flexibility when creating web tests for Visual Studio.