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
Open your solution in your IDE.
Configure the CMSTestConnectionString connection string using one of the supported sources:
- NUnit parameters in a
.runsettingsfile (recommended in build pipelines, as it allows for simple specification of which run settings to use, e.g.,dotnet test --settings AcmeTests.runsettings) app.configin your test project- Global
Tests.configin the solution root, which is shared by all test projects in the solution
XMLapp.config<configuration> <connectionStrings> <clear/> <add name="CMSTestConnectionString" connectionString="Data Source=localhost;Initial Catalog=MyXperienceDB;Integrated Security=True;Encrypt=False;" /> </connectionStrings> </configuration>- NUnit parameters in a
Make the test class inherit from the IntegrationTests base class.
C#Integration test classusing CMS.Tests; using NUnit.Framework; [TestFixture] public class IntegrationTestsExampleTests : IntegrationTests { [Test] public void IntegrationTests_BaseClass_IsAvailable() { // Test code that accesses the database. } }Write the required integration test methods.
Add a method to the test class that cleans up all objects created or updated during the execution of tests.
C#Cleanup in TearDownusing 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):
- NUnit parameters in a
.runsettingsfile (highest priority) - Test project configuration (for example,
app.config, which is transformed intoMyProject.Tests.dll.configin output) - Global Tests.config in the solution root directory
<?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 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.