admin管理员组

文章数量:1196352

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

Share Improve this question edited Jan 23 at 13:33 ath0m asked Jan 22 at 22:58 ath0math0m 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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);

本文标签: cNo Endpoints are configured when ProgramMain() is called from an unit testStack Overflow