I have got a method that I am trying to test which uses environment variables from my "local.settings.json"
private static string _environmentVar = Environment.GetEnvironmentVariable("envirnomentVarConfig");
public string MyMethod()
{
var result = DoStuff(_environmentVar)
return result;
}
In my test I am calling this method and when debugging, I can see that _environmentVar is null.
Do I need to setup envirnomentVarConfig in the test? If so how?
With all these packages installed, I could initialize the ConfigurationBuilder with a subset of the default ASP.NET Core setup: var environment = Environment. GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); var builder = new ConfigurationBuilder() . AddJsonFile($"appsettings.
Environment variables can be used to pass configuration to an application when it is run. This is done by adding the definition of the environment variable to the deployment configuration for the application. To add a new environment variable use the oc set env command.
Solved this by setting up the variable in the test using:
Environment.SetEnvironmentVariable("environmentVarConfig", "environmentVarValue");
You can specify environment variables in the .runsettings
file:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<EnvironmentVariables>
<YOUR_VARIABLE>Value for your variable</YOUR_VARIABLE>
<SOME_OTHER_VARIABLE>With another Value</SOME_OTHER_VARIABLE>
</EnvironmentVariables>
</RunConfiguration>
</RunSettings>
Alternatively you can implement a DataCollector
which provides environment variables via ITestExecutionEnvironmentSpecifier
// Add a reference to nuget package `Microsoft.TestPlatform.ObjectModel`
// The assembly name must end with `Collector` (i.e. match `*collector.dll`)
[DataCollectorFriendlyName("my own example collector")]
[DataCollectorTypeUri("datacollector://myown/examplecollector/1.0")]
public class MyDataCollector : DataCollector, ITestExecutionEnvironmentSpecifier
{
public override void Initialize(
XmlElement configurationElement,
DataCollectionEvents events,
DataCollectionSink dataSink,
DataCollectionLogger logger,
DataCollectionEnvironmentContext environmentContext)
{
// inspect configurationElement for your custom settings
}
public IEnumerable<KeyValuePair<string, string>> GetTestExecutionEnvironmentVariables()
{
return new Dictionary<string, string>
{
["YOUR_VARIABLE"] = "your value",
};
}
}
You also configure your data collector via the .runsettings
file:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<TestAdaptersPaths>path/where/to/find/your/collector</TestAdaptersPaths>
</RunConfiguration>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="my own example collector" uri="datacollector://myown/examplecollector/1.0">
<Configuration>
<SomeSettingHere/>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
I was able to do it globally for tests on WebHostBuilder according to documentation https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#set-the-environment
protected override IWebHostBuilder CreateWebHostBuilder() =>
base.CreateWebHostBuilder().UseEnvironment("Testing");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With