我正在运行单元测试作为ASP.NET核心MVC解决方案的一部分.确切地说,.NET Core的版本是2.1.
我在launchSettings.json文件中创建了一个配置文件部分,其中包含我希望由测试运行器加载和注入的环境变量,以便环境变量可用并且在运行单元测试时可以包含特定值.
我的ASP.NET Core MVC项目中的launchSettings.json作为链接添加到我的单元测试项目中,并且属性设置为Build Action – None,Copy to output folder – Copy Always.
该文件被复制到我的输出文件夹,但我不知道如何让测试运行器将此文件与UnitTesting配置文件一起使用.我尝试使用“测试”这个词作为配置文件名称,似乎没有任何效果.
这是一个示例launchSettings.json文件:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:62267/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"MigrationHistoryTableName": "MigrationHistory",
"ConnectionStringName": "EFConnectionString",
"Redis__SSL": "True",
"Redis__Port": "6380",
"Redis__InstanceName": "RedisDev",
"Redis__AbortConnect": "False",
"Redis__ConnectionString": "{URI}:{Port},password={Password},ssl={SSL},abortConnect={AbortConnect}"
}
},
"MyDataServices": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"MigrationHistoryTableName": "MigrationHistory",
"ConnectionStringName": "EFConnectionString",
"Redis__SSL": "True",
"Redis__Port": "6380",
"Redis__InstanceName": "RedisDev",
"Redis__AbortConnect": "False",
"Redis__ConnectionString": "{URI}:{Port},password={Password},ssl={SSL},abortConnect={AbortConnect}"
},
"applicationUrl": "http://localhost:4080/"
},
"UnitTesting": {
"commandName": "Executable",
"executablePath": "test",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"MigrationHistoryTableName": "MigrationHistory",
"ConnectionStringName": "EFConnectionString",
"Redis__SSL": "True",
"Redis__Port": "6380",
"Redis__InstanceName": "RedisDev",
"Redis__AbortConnect": "False",
"Redis__ConnectionString": "{URI}:{Port},password={Password},ssl={SSL},abortConnect={AbortConnect}"
}
}
}
}
据我所知,默认情况下,git中会忽略launchSettings.json文件.我们将使用此文件的开发版本检入我们的代码,该文件将包含预期在开发环境中可用的设置示例.
谢谢你花时间阅读本文并提供帮助!
最佳答案 我现在写了这个静态加载器,但这并不理想:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MyNamespaceHere.Services.Common.Utilities
{
public static class LaunchSettingsLoader
{
public static void LaunchSettingsFixture(string launchSettingsPath = "Properties\\launchSettings.json", string profileName = "UnitTesting")
{
using (var file = File.OpenText(launchSettingsPath))
{
var reader = new JsonTextReader(file);
var jObject = JObject.Load(reader);
var allprofiles = jObject
.GetValue("profiles", StringComparison.OrdinalIgnoreCase);
// ideally we use this
var variables = jObject
.GetValue("profiles", StringComparison.OrdinalIgnoreCase)
//select a proper profile here
.SelectMany(profiles => profiles.Children())
//.Where(p => p.Value<String> == profileName)
.SelectMany(profile => profile.Children<JProperty>())
.Where(prop => prop.Name == "environmentVariables")
.SelectMany(prop => prop.Value.Children<JProperty>())
.ToList();
Console.WriteLine(variables?.Count);
var profilesDictJToken = allprofiles.ToObject<Dictionary<string, JToken>>();
var unitTestingProfile = profilesDictJToken[profileName];
var unitTestingProfileDictJToken = unitTestingProfile.ToObject<Dictionary<string, JToken>>();
var environmentVariables = unitTestingProfileDictJToken["environmentVariables"];
var environmentVariablesList = environmentVariables.ToList();
foreach (var variable in environmentVariablesList)
{
var name = ((JProperty)variable).Name;
var value = ((JProperty)variable).Value.ToString();
Environment.SetEnvironmentVariable(name, value);
}
}
}
}
}