Skip to content

Creating connection strings

Jon P Smith edited this page Nov 18, 2021 · 6 revisions

The xUnit unit test library will run unit test classes in parallel. This means you need class-unique databases to allow the unit tests not to clash. I have a number of methods to help with this, but first you must add a appsettings.json file at the top level of your xUnit test project.

Specifying the base database connection string for SQL Server and PostgreSQL

If you are going to use this library to help create SQL Server or PostgreSQL databases, then you need to add a ConnectionStrings section to your appsettings.json file. The file should look like this:

{
  "ConnectionStrings": {
    "UnitTestConnection": "Server=(localdb)\\mssqllocaldb;Database=EfCore.TestSupport-Test;Trusted_Connection=True;MultipleActiveResultSets=true",
    "PostgreSqlConnection": "host=127.0.0.1;Database=TestSupport-Test;Username=postgres;Password=your-password",
  }
}

The UnitTestConnection connection string is for SQL Server databases and the PostgreSqlConnection connection string is for PostgreSQL databases.

The database name in either connection string must end with Test. That is a safety feature

  1. A connection string
    • SQL Server is called UnitTestConnection
    • PostgreSQL is called PostgreSqlConnection
  2. The name of the database in that connection string must end with Test. That is a safety feature if you want to delete all the test databases - see Delete all test databases.
  3. I recommend the start of the database name is the name of your application, so that its unique and the databases are easy to find.

SQL Server GetUniqueDatabaseConnectionString() extension method

The method GetUniqueDatabaseConnectionString() is an extension method on an object, typically the unit test class you are running in - see code below. This means the database name is unique to unit test class it is running in. This is useful because xUnit runs all the test classes at once, and you need a database that is unique to each test class.

It returns a connection string based on the connection string with the name UnitTestConnection in your appsettings.json file, but with the database name being a combination of the Database name in the connection and the name of the object as a suffix.

[Fact]
public void GetTestConnectionStringOk()
{
    //SETUP
    var config = AppSettings.GetConfiguration();
    var orgDbName = new SqlConnectionStringBuilder(
         config.GetConnectionString(AppSettings.UnitTestConnectionStringName)).InitialCatalog;

    //ATTEMPT
    var con = this.GetUniqueDatabaseConnectionString();

    //VERIFY
    var newDatabaseName = new SqlConnectionStringBuilder(con).InitialCatalog;
    newDatabaseName.ShouldEqual ($"{orgDbName}_{this.GetType().Name}");
}

Making a SQL Server connection string even more unique

The GetUniqueDatabaseConnectionString() extension method takes one, optional parameter, which it will add onto the database name. For instance, replacing the call in the about unit test with

this.GetUniqueDatabaseConnectionString("ExtraName");

would result in a database name now having an additional suffix of _ExtraName. This allows you to make method-level unique database names.

PostgreSQL GetUniquePostgreSqlConnectionString() extension method

The method GetUniquePostgreSqlConnectionString() is an extension method on an object, typically the unit test class you are running in - see code below. This means the database name is unique to unit test class it is running in. This is useful because xUnit runs all the test classes at once, and you need a database that is unique to each test class.

It returns a connection string based on the connection string with the name PostgreSqlConnection in your appsettings.json file, but with the database name being a combination of the Database name in the connection and the name of the object as a suffix.

[Fact]
public void GetUniquePostgreSqlConnectionStringOk()
{
    //SETUP
    var config = AppSettings.GetConfiguration();
    var orgDbName = new NpgsqlConnectionStringBuilder(
        config.GetConnectionString(AppSettings.PostgreSqlConnectionString)).Database;

    //ATTEMPT
    var con = this.GetUniquePostgreSqlConnectionString();

    //VERIFY
    var newDatabaseName = new NpgsqlConnectionStringBuilder(con).Database;
    newDatabaseName.ShouldEqual($"{orgDbName}_{GetType().Name}");
}

Making a PostgreSQL connection string even more unique

The GetUniquePostgreSqlConnectionString() extension method takes one, optional parameter, which it will add onto the database name. For instance, replacing the call in the about unit test with

this.GetUniquePostgreSqlConnectionString("ExtraName");

would result in a database name now having an additional suffix of _ExtraName. This allows you to make method-level unique database names.

Clone this wiki locally