Skip to content

Test Application Management

Alexander edited this page Apr 13, 2015 · 1 revision

Overview

Creation of test web applications is the central part of Sitecore.LiveTesting framework. For most of the cases Sitecore.LiveTesting.LiveTest base class bears the burden of proper ASP .NET environment setup, but if you would like to do that explicitly to gain more control over the execution, you can refer to the classes from Sitecore.LiveTesting.Applications namespace. Typical examples are given below in the following sections.

Action Execution in Created Test Applications

Consider the following code from the introductory part of the documentation:
using NUnit.Framework;

using NUnit.Framework;

[TestFixture]
public class SampleTest
{
  [Test]
  public void ShouldGetConfiguration()
  {
    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
  }
}  

Let's make it work but without using Sitecore.LiveTesting.LiveTest class this time:

using NUnit.Framework;

[TestFixture]
public class SampleTest
{
  [Test]
  public void ShouldGetConfiguration()
  {
    Sitecore.LiveTesting.Applications.TestApplicationManager appManager = new Sitecore.LiveTesting.Applications.TestApplicationManager();
    Sitecore.LiveTesting.Applications.TestApplicationHost appHost = new Sitecore.LiveTesting.Applications.TestApplicationHost("MyApplication", "/", "..");

    Sitecore.LiveTesting.Applications.TestApplication app = appManager.StartApplication(appHost);

    RealTestClass realTest = (RealTestClass)app.CreateObject(typeof(RealTestClass));

    realTest.RealTestMethod();
  }

  public class RealTestClass : System.MarshalByRefObject
  {
    public void RealTestMethod()
    {
      System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
    }
  }
}  

Sitecore.LiveTesting.Applications.TestApplicationManager is the class that is responsible for test application creation, while Sitecore.LiveTesting.Applications.TestApplicationHost provides the information about application to be created: its identifier, physical and virtual paths. StartApplication method starts test application for the provided arguments if it has not been started yet, or just returns reference to existing test application object if application has already been started.
Instantiated object of Sitecore.LiveTesting.Applications.TestApplication type has two accessible methods: CreateObject which creates an instance of an object in the test application and ExecuteAction which executes static method in the test application. Because all test applications exist in their own separate AppDomains, the class you would like to create must be derived from System.MarshalByRefObject (it can also be made serializable, but this case has very limited applicability).

Request Execution

Let's now consider how we can execute requests in explicitly created test applications. Using the code above as a basis it turns to be relatively easy:

using NUnit.Framework;

[TestFixture]
public class SampleTest
{
  [Test]
  public void ShouldExecuteRequest()
  {
    Sitecore.LiveTesting.Applications.TestApplicationManager appManager = new Sitecore.LiveTesting.Applications.TestApplicationManager();
    Sitecore.LiveTesting.Applications.TestApplicationHost appHost = new Sitecore.LiveTesting.Applications.TestApplicationHost("MyApplication", "/", "..");

    Sitecore.LiveTesting.Applications.TestApplication app = appManager.StartApplication(appHost);

    Sitecore.LiveTesting.Requests.RequestManager requestManager = (Sitecore.LiveTesting.Requests.RequestManager)app.CreateObject(typeof(Sitecore.LiveTesting.Requests.RequestManager));
    Sitecore.LiveTesting.Requests.Request request = new Sitecore.LiveTesting.Requests.Request { Path = "non-existent-url" };
    Sitecore.LiveTesting.Requests.Response response = requestManager.ExecuteRequest(request);

    Assert.AreEqual(404, response.StatusCode);
  }
}  

Since Sitecore.LiveTesting.Requests.RequestManager inherits from System.MarshalByRefObject it can be instantiated remotely in test application. Sitecore.LiveTesting.Requests.Request and Sitecore.LiveTesting.Requests.Response are marked as serializable and that allows to transfer instances of these classes to and from remote Sitecore.LiveTesting.Requests.RequestManager instance.

Tests for Distributed environment

As you may have seen, Sitecore.LiveTesting.LiveTest gives you much easier way to execute tests in real ASP .NET environment, but there is a set of scenarios which Sitecore.LiveTesting.LiveTest does not handle and it is running tests in distributed environment.
Let's imagine that we want to test if two web applications share the state between each other. Access to the data is done via MyDataStorage class for both of the applications, so we will use this class for our testing purposes too. There are no problems with request execution and running requests would probably give us a more reliable way to test the functionality, however for sake of brevity we will not do this and will just go with MyDataStorage.

using NUnit.Framework;

[TestFixture]
public class SampleDistributedTest
{
  [Test]
  public void ShouldShareDataBetweenApplications()
  {
    Sitecore.LiveTesting.Applications.TestApplicationManager appManager = new Sitecore.LiveTesting.Applications.TestApplicationManager();
    Sitecore.LiveTesting.Applications.TestApplicationHost appHost1 = new Sitecore.LiveTesting.Applications.TestApplicationHost("MyApplication1", "/", "..");
    Sitecore.LiveTesting.Applications.TestApplicationHost appHost2 = new Sitecore.LiveTesting.Applications.TestApplicationHost("MyApplication2", "/", "..");

    Sitecore.LiveTesting.Applications.TestApplication app1 = appManager.StartApplication(appHost1);
    Sitecore.LiveTesting.Applications.TestApplication app2 = appManager.StartApplication(appHost2);

    MyDataStorage app1DataStorage = (MyDataStorage)app1.CreateObject(typeof(MyDataStorage));
    MyDataStorage app2DataStorage = (MyDataStorage)app2.CreateObject(typeof(MyDataStorage));

    Assert.AreEqual(1, app1DataStorage.IncrementVisitCount());
    Assert.AreEqual(2, app2DataStorage.IncrementVisitCount());
  }

  public class MyDataStorage : System.MarshalByRefObject
  {
    private static int counter = 0;

    public int IncrementVisitCount()
    {
      return ++counter;
    }
  }
}  

The test fails and it shows actual problem in MyDataStorage design: MyDataStorage stores its state in a static variable, therefore the data will not be synchronized between different applications. If MyDataStorage stores data in a database or some other kind of storage which is accessible from different processes, the problem will be resolved and the test will be fixed.