Create integration tests with a connection string

Create integration tests if you need to access data from the Xperience database when writing automated tests. Integration tests are significantly slower than unit tests with faked data.

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 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
    • Global Tests.config in the solution root, which is shared by all test projects in the solution
    XML
    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.

    C#
    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.

    C#
    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.

Use isolated integration tests 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
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. Add the following to your .runsettings file:

XML
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.