liveBook · Manning

Test configuration provider pattern

As explained in Effective Behavior-Driven Development

A pattern that centralizes how test configuration settings are loaded and made available.

A test configuration provider centralizes how settings are loaded and made available, keeping configuration-source handling out of scenario code. section 20.2 section 20.5

A provider can:

  • Load settings from sources such as a JSON file and environment variables. section 20.2
  • Expose values as individual settings or group them into strongly typed sections. section 20.2

Test environment settings

A test configuration can specify whether to stub the database, its connection string, and payment-gateway details. The provider can make these settings available to tests. section 20.2

Listing 20.2 Test configuration provider exposing flattened settings and its usage
*** Support/TestConfigurationProvider.cs ***
using Microsoft.Extensions.Configuration;

namespace WIMP.Specs.Support;

public class TestConfigurationProvider
{
  private readonly IConfigurationRoot configurationRoot = 
    new ConfigurationBuilder()    #A
      .SetBasePath(AppContext.BaseDirectory)
      .AddJsonFile("testconfig.json", optional: true)
      .AddEnvironmentVariables(prefix: "WIMP__")
      .Build();

  public string PaymentGatewayUrl =>    #B
      configurationRoot["PaymentGateway:Url"]
      ?? throw new InvalidOperationException(
          "Missing configuration setting: PaymentGateway:Url");

  public string PaymentGatewayApiKey =>    #B
      configurationRoot["PaymentGateway:ApiKey"]
      ?? throw new InvalidOperationException(
          "Missing configuration setting: PaymentGateway:ApiKey");

  public bool DatabaseUseStub =>    #B
      configurationRoot.GetValue<bool?>("Database:UseStub")
      ?? true;    #C

  public string DatabaseConnectionString =>    #B
      configurationRoot["Database:ConnectionString"]
      ?? throw new InvalidOperationException(
          "Missing configuration setting: Database:ConnectionString");
}
*** Support/PaymentGatewaySimulator.cs ***
public class PaymentGatewaySimulator(    #D
    TestConfigurationProvider testConfigurationProvider)
{
  public IPaymentGateway Start()
  {
    string url = testConfigurationProvider
        .PaymentGatewayUrl;    #E
    string apiKey = testConfigurationProvider
        .PaymentGatewayApiKey;    #E
    ...
  }
  ...
}

See a provider that loads a JSON file and environment variables, then exposes settings through properties. section 20.2

Listing 20.3 Test configuration provider exposing hierarchical settings and its usage
*** Support/TestConfigurationProvider.cs ***
using Microsoft.Extensions.Configuration;

namespace WIMP.Specs.Support;

public class TestConfigurationProvider
{
  public class DatabaseConfiguration    #A
  {
    public bool UseStub { get; set; } = true;
    public string ConnectionString { get; set; } = null!;
  }

  public class PaymentGatewayConfiguration    #A
  {
    public string Url { get; set; } = null!;
    public string ApiKey { get; set; } = null!;
  }

  private readonly IConfigurationRoot configurationRoot = 
    new ConfigurationBuilder()    #B
      .SetBasePath(AppContext.BaseDirectory)
      .AddJsonFile("testconfig.json", optional: true)
      .AddEnvironmentVariables(prefix: "WIMP__")
      .Build();

  public PaymentGatewayTestConfiguration PaymentGateway    #C
  {
    get
    {
      var paymentGatewayConfig = configurationRoot
        .GetRequiredSection("PaymentGateway")    #D
        .Get<PaymentGatewayTestConfiguration>()!;
      return paymentGatewayConfig.Url is null
        ? throw new InvalidOperationException("Missing configuration ...")
        : paymentGatewayConfig.ApiKey is null
          ? throw new InvalidOperationException("Missing configuration...")
          : paymentGatewayConfig;
    }
  }

  public DatabaseTestConfiguration Database    #C
  {
    get
    {
      var databaseConfig = configurationRoot
        .GetRequiredSection("Database")    #D
        .Get<DatabaseTestConfiguration>()!;
      return databaseConfig.ConnectionString is null
        ? throw new InvalidOperationException("Missing configuration ...")
        : databaseConfig;
    }
  }
}
*** Support/PaymentGatewaySimulator.cs ***
public class PaymentGatewaySimulator(    #E
    TestConfigurationProvider testConfigurationProvider)
{
  public IPaymentGateway Start()
  {
    string url = testConfigurationProvider
        .PaymentGateway.Url;    #F
    string apiKey = testConfigurationProvider
        .PaymentGateway.ApiKey;    #F
    ...
  }
  ...
}

See the alternative of exposing settings as hierarchical, strongly typed sections. section 20.2

How Effective Behavior-Driven Development covers it

How deep it goes
Shows configuration-source loading, flattened access, and typed sections; leaves platform-specific library choice to the implementation.
Examples
Uses JSON database and payment-gateway settings, environment-variable overrides, and flattened or hierarchical provider properties.
What you can do after
Apply it by encapsulating configuration loading and exposing settings centrally, using platform configuration libraries where available.
How it connects
Connects test configuration to Parameterized Execution and Parameterized Scenario for configurable behavior and deployment variants.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Behavior-Driven Development. The section links point to the parts of the book it draws on.