Skip to content

Example: Snapshot

Thomas Malowanczyk edited this page Jun 20, 2018 · 7 revisions

Scenario

As a project gets more and more test, the data setup tends to grow as well and will end up taking more and more time of the test execution. This is especially true for XrmMockup, as all data is created through the service where multiple logical checks and actions are applied and executed.

  • Validation of requests
  • Privilege checks
  • Auto filling of system fields
  • Execution of system plugins

Solution

In order to reduce the computation time for setting up data, we have added a snapshot feature where the current XrmMockup database can be stored, retrieved, and deleted during run-time. This means that it is only necessary to build the database once and then retrieve the snapshot during initialization of the test. It is even possible to swap between different snapshots during the test.

It is necessary to create the setup data and snapshots during the AssemblyInitialize phase, as it is the only place where it is possible to create the data only once. Any other places, and the data will be created multiple times and render the snapshot feature irrelevant. This, however, does require that the data is created in a static method.

How

The snapshot feature has the following three methods:

  • TakeSnapshot: Takes a snapshot of the XrmMockup database as it currently is at the time of the call
  • RestoreToSnapshot: Overwrite the current XrmMockup database with the given snapshot
  • DeleteSnapshot: Deletes the given snapshot. (This method is most likely unnecessary but were added for any possible use cases) All three methods takes a string defining the name of the snapshot.

Datasetup and snapshot are created as followed:

[AssemblyInitialize]
public static void InitializeMockup(TestContext context)
{
    crm = XrmMockup365.GetInstance(new XrmMockupSettings
    {
        ...
    });

    DataSetupBase();
    crm.TakeSnapshot("Base");

    DataSetupScenario1();
    crm.TakeSnapshot("Scenario1");

    crm.RestoreToSnapshot("Base");
    DataSetupScenario2();
    crm.TakeSnapshot("Scenario2");
}

It is possible to create different data setups for different scenarios that might build of the a base case. This does require the database to be restored to a snapshot of the base case.

Initialization and test execution is then done as followed:

[TestClass]
public class Scenario1 : BaseTestClass
{
    [TestInitialize]
    public void Initialize()
    {
        crm.RestoreToSnapshot("Scenario1");
    }

    [TestMethod]
    public void UnitTest1()
    {
        ...
    }
}