Did you know that the loadtest framework isn’t limited to only running webtests or calling web-services? You can also include your unittests! This is very convenient for loadtesting MVC controllers, WebApi controllers or SharePoint’s CSOM interface.
Your unittests should work without any changes. If you need one of the following then your unittest needs some minor modification:
- Work with loadtest transactions.
- Use information such as the UserId or AgentName.
Below is a small example class that shows how to achieve the above without sacrificing the ability to run the class as part of a standalone unittest. Just inherit from it and use its methods.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.LoadTesting; namespace Loadtesting { [TestClass()] public class CombinedUnitLoadTest { /// <summary> /// Visual Studio's Unit Test Context. Microsoft's UnitTestFramework sets this member /// </summary> public TestContext TestContext { get; set; } /// <summary> /// Load Test Context. Only present if the testcase is being run in a loadtest /// </summary> public LoadTestUserContext LoadTestUserContext { get { if (this.TestContext.Properties.Contains("$LoadTestUserContext")) { return this.TestContext.Properties["$LoadTestUserContext"] as LoadTestUserContext; } else { return null; } } } /// <summary> /// The name of the Agent where the testcase is running. Returns "" if the test is not running on an agent /// </summary> public string AgentName { get { if (this.TestContext.Properties.Contains("AgentName")) { return this.TestContext.Properties["AgentName"].ToString(); } return ""; } } /// <summary> /// The Id of the virtual user that is running the testcase. Returns "" if the test is not running in a loadtest /// </summary> public string UserId { get { if(null != this.LoadTestUserContext) { return this.LoadTestUserContext.UserId.ToString(); } return ""; } } /// <summary> /// Returns a number that specifies how many tests the current virtual user has already executed /// </summary> public string Iteration { get { if(null != this.LoadTestUserContext) { return this.LoadTestUserContext.CompletedTestCount.ToString(); } return ""; } } /// <summary> /// Starts a named transaction. The loadtest framework will report the time between BeginTransaction() and EndTransaction() /// </summary> /// <param name="name"></param> public void BeginTransaction(string name) { if(null != this.LoadTestUserContext) { this.TestContext.BeginTimer(name); } } /// <summary> /// Ends the named transaction. The loadtest framework will report the time between BeginTransaction() and EndTransaction() /// </summary> /// <param name="name"></param> public void EndTransaction(string name) { if(null != this.LoadTestUserContext) { this.TestContext.EndTimer(name); } } } }