我正在 Visual Studio (2017) 中为基于 VSTS 构建并从那里部署的 Net Core 应用程序构建集成测试。我的项目是测试项目,现在我到部署的 API url 的连接字符串和数据库是硬编码的,但我想从代码中删除它们并将它们放在添加环境变量的 VSTS 构建步骤中。
现在,我的测试 .cs 文件看起来像这样:
[TestClass]
public class TestFeature
{
//Set up variables
private static string _connectionString = "server=localhost;port=5432;database=databaseName;user id=postgres;password=postgres";
[TestInitialize]
public void Initialize()
{
}
//And going into my test methods
}
如何设置我的项目以读取从 VSTS 上的配置设置的环境变量?
到目前为止,我的研究成果是右键单击测试项目的Properties,在Debug下,设置带有键和值的环境变量。
然后我更改我的测试项目以采用 GetEnvironmentVariable()
private static string _connectionString = Environment.GetEnvironmentVariable(nameOfVariable);
在 VSTS 的另一端,我试图找到一个构建步骤,将变量设置为那里的连接字符串。我能找到的最佳步骤是“设置变量”。
但是,这对我不起作用。程序将无法识别环境变量中设置的新变量的名称,以启动。
最佳答案
您需要在“变量”选项卡中添加 VSTS 变量。
如果您想使用 Powershell 脚本任务以动态方式进行设置,另一种选择是使用 Powershell 来设置它们。
Write-Host "##vso[task.setvariable variable=sauce]crushed tomatoes"
关于.net-core - VSTS Visual Studio 设置环境变量测试项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48408977/