17

To setup unit tests in previous versions of .Net Core, I could host my WebApp or WebAPI in a test project the following way:

         IHost host = Host.CreateDefaultBuilder()
            .ConfigureWebHostDefaults(config =>
            {
                config.UseStartup<MyWebApp.Startup>();
                config.UseUrls("https://localhost:44331/");
                ...    
            })
            .Build();

The current .Net 6.0 does not use Startup class concept, and thus it could not be referenced. How can host AspNet apps in a test project in a proper and clean way?

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
user2341923
  • 4,537
  • 6
  • 30
  • 44
  • 1
    I have a repo showing the use of `WebApplicationFactory` for unit/integration tests with Minimal APIs, maybe that will help you out: https://github.com/martincostello/dotnet-minimal-api-integration-testing – Martin Costello Nov 24 '21 at 10:23
  • @martin-costello I see the trick is adding `public partial class Program{}` – user2341923 Nov 24 '21 at 11:24

1 Answers1

22

Note that you can switch to generic hosting model (the one using the startup class) if you want.

To set up integration tests with the new minimal hosting model you can make web project internals visible to the test one for example by adding next property to csproj:

<ItemGroup>
  <InternalsVisibleTo Include ="YourTestProjectName"/>
</ItemGroup>

And then you can use the Program class generated for the web app in WebApplicationFactory:

class MyWebApplication : WebApplicationFactory<Program>
{
    protected override IHost CreateHost(IHostBuilder builder)
    {
        // shared extra set up goes here
        return base.CreateHost(builder);
    }
}

And then in the test:

var application = new MyWebApplication();
var client = application.CreateClient();
var response = await client.GetStringAsync("/api/WeatherForecast");

Or use WebApplicationFactory<Program> from the test directly:

var application = new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
    builder.ConfigureServices(services =>
    {
       // set up servises
    });
});
var client = application.CreateClient();
var response = await client.GetStringAsync("/api/WeatherForecast");

Or instead of using InternalsVisibleTo you can declare public partial Program class and use it. For example add next to the bottom of top-level statement file (the rest is the same):

public partial class Program { }

Code examples from migration guide.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 2
    Any idea on how to do this without using WebApplicationFactory or TestServer? Asking as I'm trying to implement PactNet provider tests, and have yet to figure this out https://github.com/pact-foundation/pact-net – Imer Muhović Nov 23 '22 at 19:29
  • @ImerMuhović did you ever get this solved for Pact? – Aaron Feb 01 '23 at 05:57
  • @Aaron you can try looking into options listed [here](https://stackoverflow.com/a/74561335/2501279) – Guru Stron Feb 01 '23 at 06:05