Creating isolated integration tests

Isolated integration tests are automated tests that work with database objects within their own separate database. A new clean database instance is created for each test (using the default Xperience installation scripts) and then deleted after the test run. Note that integration tests are significantly slower than unit tests with faked data.

All test classes with isolated integration tests must inherit from the IsolatedIntegrationTests base class (provided by the CMS.Tests library). The base class automatically performs database initialization before test runs and cleanup thereafter.

Requirements

To use isolated integration tests, you need to have Microsoft SQL Server 2012 Express LocalDB v11 (or newer) installed. LocalDB is automatically included in Visual Studio or Microsoft SQL Server installations.

If you wish to run integration tests in environments with no native LocalDB support (e.g., on Linux distributions), see Running isolated integration tests without LocalDB

Creating isolated integration tests

  1. Open the solution containing your test project in Visual Studio.

  2. Make the test class inherit from the IsolatedIntegrationTests base class.

    
    
    
     using CMS.Tests;
     using NUnit.Framework;
    
     [TestFixture]
     public class MyIntegrationTests : IsolatedIntegrationTests
     {
         ...
     }
    
    
     
  3. Write the required isolated integration test methods.

  4. (Optional) Create a Tests.config file in your test project and set the following keys under the configuration/appSettings section:

    • CMSTestDatabaseScriptFolderPath – the path to the folder with files required for database creation. The folder must contain the SQL.zip file. If not specified, the test initialization searches the parent directories of the test project and attempts to locate the default Xperience installation scripts under the CMS\App_Data\Install folder path.
    • CMSTestDatabaseFolderPath – the path to the folder that will contain test databases. If not specified, the databases are stored in the TestDatabases folder under the current solution’s root folder.
    • CMSTestDatabaseInstanceName – the name of the SQL server instance used by tests. The default value is: (localdb)\MSSQLLocalDB



<appSettings>
  <add key="CMSTestDatabaseScriptFolderPath" value="C:\inetpub\wwwroot\Xperience\CMS\App_Data\Install" />
  <add key="CMSTestDatabaseFolderPath" value="C:\CustomDBFolder" />
  <add key="CMSTestDatabaseInstanceName" value="(localdb)\CustomTestingInstance" />
</appSettings>


Each test in a test class inheriting from IsolatedIntegrationTests uses its own database, which is automatically initialized before the test run and cleaned up after.

Running isolated integration tests without SQL Express LocalDB or integrated authentication support

By default, Xperience uses a LocalDB instance to automatically manage databases for isolated integration tests via integrated security authentication.

In environments where SQL Express LocalDB or integrated security authentication is not supported or available (such as Linux), you need to set an alternative connection string for the test project via the CMSTestIsolatedAltConnectionString application setting. The connection string needs to specify the server instance intended to host test databases together with authentication credentials for a user with full access (able create and manage databases), for example:

Tests.config of the isolated tests project



<configuration>
    <appSettings>
        <add key="CMSTestIsolatedAltConnectionString" value="Data Source=localhost;Initial Catalog=##DBNAME##;Integrated Security=False;User ID=mylogin;Password=mypassword;Connection Timeout=240;" />"
    </appSettings>
</configuration>


The connection string must contain the ##DBNAME## string in place of the database name. The system dynamically resolves this value at runtime to create (and dispose of) a database for the given test run.

Moreover, the target SQL Server instance must be running on the same machine and have read-write access to the test databases folder specified via the CMSTestDatabaseFolderPath application setting.