I know this is an older post, but the way I am solving this is by leveraging nunit parameters (I am running SpecFlow with nUnit). So, for example, I am passing in a parameter of environment
(i.e. Dev, QA, Prod) and based on that value, the relevant test data file that contains environment specific values (i.e. API base URL) will change. I am using different "test data" files that will contain environment-specific values based on that environment parameter that is passed in.
The code used to read in that parameter looks like this:
// get the correct URL and user creds based on the environment passed in
var environment = TestContext.Parameters.Get("environment");
var dataFile = $"{environment}-test-data.json";
As you can see, that environment parameter that was passed, is being retrieved by nUnit TestContext object. And the value of that parameter is then used to retrieve the correct test data file above.
And the "<environment>-test-data.json file looks like this:
{
"baseUrl": "https://dev-some-endpoint.net",
}
Finally, I am reading in values from the test data file like this:
var config = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory).AddJsonFile(dataFile).Build();
var url = config.GetSection("baseUrl").Value;