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>