I am trying to integration test a whole Web-API Project. When starting the web-api using 'assembly.exe or using console 'dotnet assembly.dll' the api starts successfully and all endpoints are discovered. But when I execute the Program.Main() from a unit test no endpoints are configured during startup although the same code is executed. Does anybody have a clue what is different between standalone execution and execution from a unit test:
[OneTimeSetUp]
public static void StartWebApiForLocalRuns()
{
Program.Main().ConfigureAwait(false);
}
public static async Task Main()
{
var builder = WebApplication.CreateBuilder();
builder.Configuration
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.logging.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
builder.Host
.UseWindowsService()
.UseSerilog();
Startup.ConfigureServices(builder.Configuration, builder.Services);
var app = builder.Build();
Startup.Configure(_app, builder.Environment, _app.Lifetime, _app.Services);
await app.RunAsync();
}
When running the Web-App from VS:
Endpoints State from normal start
but running from unit test:
Endpoints State from unit test start
I am trying to integration test a whole Web-API Project. When starting the web-api using 'assembly.exe or using console 'dotnet assembly.dll' the api starts successfully and all endpoints are discovered. But when I execute the Program.Main() from a unit test no endpoints are configured during startup although the same code is executed. Does anybody have a clue what is different between standalone execution and execution from a unit test:
[OneTimeSetUp]
public static void StartWebApiForLocalRuns()
{
Program.Main().ConfigureAwait(false);
}
public static async Task Main()
{
var builder = WebApplication.CreateBuilder();
builder.Configuration
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.logging.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
builder.Host
.UseWindowsService()
.UseSerilog();
Startup.ConfigureServices(builder.Configuration, builder.Services);
var app = builder.Build();
Startup.Configure(_app, builder.Environment, _app.Lifetime, _app.Services);
await app.RunAsync();
}
When running the Web-App from VS:
Endpoints State from normal start
but running from unit test:
Endpoints State from unit test start
I solved it by manually setting the application name to the default namespace of the web application. As when starting the application from a unit test the application name per default changes to "testhost". This breaks the reflection based search for controllers as .net will not search for controllers referenced by the applications default assembly. By forcing the application name to the default assembly name of the web-api all controllers are loaded as expected. This simplifies the (integration-testing) as we can simply start the application using Program.Main().
Also found a solution reading this post: Integration tests do not recognize actions of controllers
var options = new WebApplicationOptions()
{
ApplicationName = typeof(Program).Assembly.GetName().Name //important to allow starting API from unit test
};
var builder = WebApplication.CreateBuilder(options);