Generate and run a coded web performance test
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
Web performance tests are recorded by browsing through your web app. The tests are included in load tests to measure the performance of your web application under the stress of multiple users. A web performance test can be converted to a code-based script that you can edit and customize like any other source code. For example, you can add looping and branching constructs.
Note
Web performance and load test functionality is deprecated. Visual Studio 2019 is the last version where web performance and load testing will be fully available. For more information, see the Cloud-based load testing service end of life blog post. To reduce the impact on users some minimum support is still available in Visual Studio 2022 Preview 3 or later. Users should also note web performance and load test functionality only supports Internet Explorer which is not available on Windows 11 and some versions of Windows 10. Read more on Internet Explorer Lifecycle policy.
Generate a coded web performance test
If you have not created a web performance test, see Record a web performance test.
Generate the coded test.
Name the test.
The new coded test opens in the code editor.
Depending on which web performance and load test project template you added to your solution, the code will be generated in either Visual Basic, or Visual C#.
You can see in the code that the GetRequestEnumerator() method in C#, or the Run() method in Visual Basic, contains each validation rule and web request that was in the recoded test.
To demonstrate adding some simple code, scroll down to the end of the method and after the code for the last web request, and add the following code:
if (DateTime.Today.DayOfWeek == DayOfWeek.Wednesday) { WebTestRequest customRequest = new WebTestRequest("http://weather.msn.com/"); yield return customRequest; } else { WebTestRequest customRequest = new WebTestRequest("https://msdn.microsoft.com/"); yield return customRequest; }
If DateTime.Today.DayOfWeek = DayOfWeek.Wednesday Then Dim customRequest As WebTestRequest = New WebTestRequest("http://weather.msn.com/") MyBase.Send(customRequest) Else Dim customRequest As WebTestRequest = New WebTestRequest("https://msdn.microsoft.com/") MyBase.Send(customRequest) End If
Build the solution to verify that your custom code compiles.
Run the test.
And because the day this was run happened to be a Wednesday…
Q&A
Q: Can I run more than one test at a time?
A: Yes, use the right-click (context) menu in Solution Explorer.
Q: Should I add a data source before or after I generate a coded test?
A: It is easier to add a data source before you generate the coded test because the code will be automatically generated for you.
When you run a coded test with a data source, you might see the following error message:
Could not run test <Test Name> on agent <Computer Name>: Object reference not set to an instance of an object.
This can occur because you have a DataSourceAttribute defined for the test class, without a corresponding DataBindingAttribute. To resolve this error, add an appropriate DataBindingAttribute, delete it, or comment it out of the code.
Q: Should I add validation and extraction rules before or after I generate a coded test?
A: It is easier to add validation rules and extraction rules before you generate the coded test; however, we recommend that you use coded UI tests for validation purposes.