我想在集成测试中默认使用
WebApplicationFactory
设置环境,env 设置为 Development
。像这样:
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup>
where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseSolutionRelativeContentRoot(AppContext.BaseDirectory);
base.ConfigureWebHost(builder);
}
protected override IWebHostBuilder CreateWebHostBuilder()
{
return WebHost.CreateDefaultBuilder()
.UseStartup<TStartup>()
.UseEnvironment("test"); // i want to launch `test` environment while im
// testing my app
}
}
当我开始调试
Startup
类(当我运行测试时)我仍然得到Development
环境:
public Startup(IHostEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
// env.EnvironmentName is set to 'Development' while i set it to 'test'
// in my WebApplicationFactory
.AddEnvironmentVariables();
Configuration = builder.Build();
}
如何正确设置
WebApplicationFactory
中的环境?或者也许如何仅在启动时更改测试策略,我依赖于 appsettings
文件?
我遇到了同样的问题,并为我解决了以下问题:
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Test");
}
作为替代方案,如果您不使用自定义
WebApplicationFactory
,您可以使用 WithWebHostBuilder
扩展方法指定环境:
var application = new WebApplicationFactory<Startup>()
.WithWebHostBuilder(builder =>
{
builder.UseEnvironment("Test");
});
在.Net 6.0中测试
https://github.com/akhanalcs/mvc-integration-test
第1步: 在您的测试项目中创建一个名为
CustomWebApplicationFactory
的类。
public class CustomWebApplicationFactory<TProgram>
: WebApplicationFactory<TProgram> where TProgram : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
// Works for: Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") in Program.cs
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "test");
// OR
// Works for: builder.Environment.EnvironmentName in Program.cs
builder.UseEnvironment("test");
}
}
第2步: 创建
SomeServiceTests.cs
文件并从那里使用它。 (我这里使用的是MSTest)
using Microsoft.AspNetCore.Mvc.Testing;
namespace SomeProject.IntegrationTests.SomeServiceTests;
[TestClass]
public class SomeServiceTests
{
private static WebApplicationFactory<Program> _factory;
[ClassInitialize]
public static void ClassInit(TestContext testContext)
{
Console.WriteLine(testContext.TestName);
_factory = new CustomWebApplicationFactory<Program>();
}
[ClassCleanup]
public static void ClassCleanup()
{
_factory.Dispose();
}
[TestMethod]
public async Task Some_Test_Method_Here_Async()
{
// ARRANGE
var client = _factory.CreateClient();
// If I wanted to resolve some service:
//using var scope = _factory.Services.CreateScope();
//var scopedServices = scope.ServiceProvider;
//var db = scopedServices.GetRequiredService<ApplicationDbContext>();
// ACT
// ASSERT
}
}
第3步: 我在我的
Program.cs
中使用环境变量,如下所示:
var builder = WebApplication.CreateBuilder(args);
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? builder.Environment.EnvironmentName;
builder.Configuration
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true);
// Rest of the code here
Host.CreateDefaultBuilder().ConfigureWebHost(e => e.UseEnvironment("开发"))
我从这些网址推断出解决方案:
https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1 https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-5.0
在您的 CustomApplicationFactory 中,您应该使用以下代码覆盖 CreateWebHostBuilder 方法:
protected override IWebHostBuilder CreateWebHostBuilder()
{
return base.CreateWebHostBuilder().UseEnvironment("test");
}