---
title: Create integration tests with a connection string
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

Create integration tests if you need to access data from the Xperience database when writing [automated tests](https://docs.kentico.com/documentation/developers-and-admins/customization/write-automated-tests.md). Integration tests are significantly slower than [unit tests with faked data](https://docs.kentico.com/documentation/developers-and-admins/customization/write-automated-tests/create-unit-tests.md).

All test classes with integration tests must inherit from the **IntegrationTests** base class (provided by the _CMS.Tests_ library). The base class ensures application initialization using a specific database connection string for each test.

## Create integration tests

1. Open your solution in your IDE.

2. Configure the **CMSTestConnectionString** connection string using one of the supported sources:

   - [NUnit](https://docs.nunit.org/) parameters in a `.runsettings` file (recommended in build pipelines, as it allows for simple specification of which run settings to use, e.g., `dotnet test --settings AcmeTests.runsettings`)
   - `app.config` in your [test project](https://docs.kentico.com/documentation/developers-and-admins/customization/write-automated-tests/set-up-test-projects.md)
   - Global `Tests.config` in the solution root, which is shared by all test projects in the solution

   ```xml title="app.config"
   <configuration>
       <connectionStrings>
           <clear/>
           <add name="CMSTestConnectionString"
                connectionString="Data Source=localhost;Initial Catalog=MyXperienceDB;Integrated Security=True;Encrypt=False;" />
       </connectionStrings>
   </configuration>
   ```

3. Make the test class inherit from the **IntegrationTests** base class.

   ```csharp title="Integration test class"
   using CMS.Tests;

   using NUnit.Framework;

   [TestFixture]
   public class IntegrationTestsExampleTests : IntegrationTests
   {
       [Test]
       public void IntegrationTests_BaseClass_IsAvailable()
       {
           // Test code that accesses the database.
       }
   }
   ```

4. Write the required integration test methods.

5. Add a method to the test class that cleans up all objects created or updated during the execution of tests.

   ```csharp title="Cleanup in TearDown"
   using CMS.Tests;

   using NUnit.Framework;

   [TearDown]
   public void Cleanup()
   {
       // Delete objects created during test execution.
   }
   ```

   A test class can contain only one `[TearDown]` method, so perform all cleanup within a single method.

All tests in a test class inheriting from **IntegrationTests** use the database specified by the **CMSTestConnectionString** connection string. Changes made during tests that you do not clean up in the tear down method remain in the database after the test runs finish.

> **Warning:** Use [isolated integration tests](https://docs.kentico.com/documentation/developers-and-admins/customization/write-automated-tests/create-isolated-integration-tests.md) if you cannot reset data modified during test execution back to its previous state (to avoid affecting other tests).

## Alternative configuration sources

The test framework reads the `CMSTestConnectionString` value from the following sources (in order of priority):

1. **NUnit parameters** in a `.runsettings` file (highest priority)
2. **Test project configuration** (for example, `app.config`, which is transformed into `MyProject.Tests.dll.config` in output)
3. **Global Tests.config** in the solution root directory

```xml title="Configuration via .runsettings"
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <TestRunParameters>
        <Parameter name="CMSTestConnectionString"
                   value="Data Source=localhost;Initial Catalog=MyXperienceDB;Integrated Security=True;Encrypt=False;" />
    </TestRunParameters>
</RunSettings>
```

## NUnit performance configuration

If you experience slowdowns with integration tests, disable the default assembly load context in [NUnit](https://docs.nunit.org/). Add the following to your `.runsettings` file:

```xml title="Performance configuration in .runsettings"
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <NUnit>
        <UseDefaultAssemblyLoadContext>false</UseDefaultAssemblyLoadContext>
    </NUnit>
</RunSettings>
```

This configuration prevents performance issues caused by newer versions of the NUnit3TestAdapter.
