Create isolated integration tests
Isolated integration tests are automated tests that work with database objects within their own separate database. A clean database instance is created for each test (using the default Xperience installation scripts) and then deleted after the test run. Isolated integration tests are significantly slower than unit tests with faked data. If you can use a shared database and clean up manually, see Create integration tests with a connection string.
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 after.
Requirements
To use isolated integration tests, you need Microsoft SQL Server Express LocalDB installed. LocalDB is automatically included in Visual Studio or Microsoft SQL Server installations.
If you want to run isolated integration tests in environments without native LocalDB support (e.g., Linux or Docker containers), see Run without LocalDB.
Create isolated integration tests
Open the solution containing your test project in your IDE.
Make the test class inherit from the IsolatedIntegrationTests base class.
C#Isolated integration test classusing CMS.Tests; using NUnit.Framework; [TestFixture] public class IsolatedIntegrationTestsExampleTests : IsolatedIntegrationTests { [Test] public void TestMethod() { // Test code that creates or modifies database objects // The database is automatically cleaned up after the test } }Write the required isolated integration test methods. You do not need to manually clean up test data, the framework handles this automatically.
Share a database across a fixture
By default, a new database is created and destroyed for each test in the fixture. For test classes where all tests share a common setup and the database can be reused, apply the SharedDatabaseForAllTests attribute to improve performance:
using CMS.Tests;
using NUnit.Framework;
[TestFixture]
[SharedDatabaseForAllTests]
public class IsolatedIntegrationTestsWithSharedDatabaseExampleTests : IsolatedIntegrationTests
{
[OneTimeSetUp]
public void OneTimeSetUp()
{
// Test data initialization shared by all tests in the fixture.
}
[Test]
public void FirstTest()
{
// Test code that accesses the database.
}
[Test]
public void SecondTest()
{
// Test code that accesses the database.
}
}
With [SharedDatabaseForAllTests], the database is created once before the first test in the class and deleted after the last test completes.
Configuration options
Configure the following keys under appSettings in one of the supported test configuration sources (app.config in your test project, or global Tests.config in the solution root):
|
Setting |
Description |
Default |
|
|
Path to the folder that contains test database files. |
|
|
|
Name of the SQL Server instance used by tests. |
|
|
|
Indicates whether the connection to the test database uses a SQL Server user instance. |
|
|
|
Alternative connection string for environments without LocalDB. See Run without LocalDB. |
Not set |
<configuration>
<appSettings>
<add key="CMSTestDatabaseFolderPath" value="C:\TestDatabases" />
<add key="CMSTestDatabaseInstanceName" value="(LocalDB)\MSSQLLocalDB" />
</appSettings>
</configuration>
Run without LocalDB
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 distributions or Docker containers), set an alternative connection string via the CMSTestIsolatedAltConnectionString application setting.
The connection string must specify the server instance intended to host test databases together with authentication credentials for a user with full access (able to create and manage databases):
<configuration>
<appSettings>
<add key="CMSTestIsolatedAltConnectionString"
value="Data Source=localhost;Initial Catalog=##DBNAME##;Integrated Security=False;User ID=sa;Password=yourpassword;Connection Timeout=240;Encrypt=False;" />
<add key="CMSTestDatabaseFolderPath" value="/var/testdatabases" />
</appSettings>
</configuration>
The connection string must contain the ##DBNAME## placeholder 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.
The target SQL Server instance must be running on the same machine and have read-write access to the test databases folder specified via CMSTestDatabaseFolderPath.
When running tests in containerized environments, the folder configured in CMSTestDatabaseFolderPath must be accessible to both the test runner process and the SQL Server process. If the path is not shared or SQL Server cannot write to it, isolated integration tests fail during database file creation.