Repository navigation
Home
Sitecore.LiveTesting framework serves for the purpose of test execution in real ASP .NET environment. Here is a simple NUnit test that demostrates one of the problems that usually arise when executing tests that rely on ASP .NET and Sitecore functionality (for certainty, lets assume that the test assembly is located beneath \bin catalog of ASP .NET Web Application):
using NUnit.Framework;
[TestFixture]
public class SampleTest
{
[Test]
public void ShouldGetConfiguration()
{
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
}
}
When the test is run it turns out that ASP .NET infrastructure has not been initialized yet and the test crashes. If it is highly desirable to test interaction between your code and ASP .NET, Sitecore.LiveTesting will come to rescue! Apart from writing such kind of tests, Sitecore.LiveTesting also allows to issue requests to web application and retrieve responses as if the contents was requested by the client but without the need to setup web server and client infrastructure. You can find description of the most basic scenarios below.
All you need to do to make the test mentioned above run is to derive from Sitecore.LiveTesting.LiveTest class:
using NUnit.Framework;
[TestFixture]
public class SampleTest : Sitecore.LiveTesting.LiveTest
{
[Test]
public void ShouldGetConfiguration()
{
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
}
}
By deriving from Sitecore.LiveTesting.LiveTest you set up real ASP .NET environment for all of the tests in the test class. For the example above it is assumed that test assembly is located in the \bin catalog of the web application. If that is not the case, you can simply add the following setting with the path to the physical location of your web application into the App.config file of your test project:
<add key="Sitecore.LiveTesting.WebsitePath" value="PathToWebApplication" />
If this setting is not specified, it will be assumed that the test assembly resides in \bin catalog of the web application.
Consider an empty Web Application with no available pages and let's imagine that we want to test what the response will be if the requested page is not found. Here is how this can be done with help of Sitecore.LiveTesting:
using NUnit.Framework;
[TestFixture]
public class SampleTest : Sitecore.LiveTesting.LiveTest
{
[Test]
public void ShouldGet404()
{
Sitecore.LiveTesting.Requests.RequestManager manager = new Sitecore.LiveTesting.Requests.RequestManager();
Sitecore.LiveTesting.Requests.Request request = new Sitecore.LiveTesting.Requests.Request { Path = "\\page.aspx" };
Sitecore.LiveTesting.Requests.Response response = manager.ExecuteRequest(request);
Assert.AreEqual(404, response.StatusCode);
Assert.True(response.Content.Contains("<title>The resource cannot be found.</title>"));
}
}
You can find documentation for more advanced scenarios in sections below.