Search code examples
specflow

SpecFlow+ config file transformation alternatives in .net core?


I'm wanting to run some SpecFlow+ tests with a bunch of config settings for a particular environment, then have the option of running the same tests against a different bunch of settings for a different environment.

With SpecFlow+ in .net framework this could be achieved by transforming the App.config file with environment specific .srprofile files. You could then hook up a .runsettings file to the .srprofile file and seamlessly change the environment you want to run your tests on by changing the test run settings in Visual Studio.

Is there an equivalent solution in .net core?

The only workaround I can think of is to use a different build configuration for each environment and then use #if DEBUG style preprocessor directives in code. That could then load a specific appsettings.json file or point to specific Azure App Configuration settings. I could then set the BuildConfiguration in my pipeline when running the tests.


Solution

  • I created a new example for your use case. You can find it at https://github.com/SpecFlowOSS/SpecFlow.Plus.Examples/tree/master/MultipleBrowserAgainstDifferentEnvironments

    You can define in the Default.srProfile which browser is used for which environment.
    The base URL is configured via the __BaseUrl environment.

    This is an example to use Firefox with google.com as base address.

    <Target name="Firefox-COM">
      <DeploymentTransformationSteps>
         <EnvironmentVariable variable="Test_Browser" value="Firefox" />
         <EnvironmentVariable variable="__BaseUrl" value="https://www.google.com" />
      </DeploymentTransformationSteps>
    </Target>
    

    If you need more values to configure, you can simply add new environment variables and get their value with a simple Environment.GetEnvironmentVariable (https://github.com/SpecFlowOSS/SpecFlow.Plus.Examples/blob/master/MultipleBrowserAgainstDifferentEnvironments/TestApplication.UiTests/Drivers/ConfigurationDriver.cs#L9).

    You can also write multiple different srProfile- files and with different runsettings files you can have different settings if you are running the tests locally or in your CI pipeline.

    If the environment variables are not enough or are getting too much to handle, I would suggest using only one environment variable, where you store a filename. In your bindings you can then use this filename to read out it.

    I hope this example helps you to achieve your requirements.


    Full disclosure: I am a developer on SpecFlow & SpecFlow+