I need to write some unit tests for an ASP.NET 6 API and need to create a test server to verify authorization. However since the startup class has been removed, I don’t know what I should use as the entry point for the test server creation.
This was the way of creating one in previous versions.
var server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
2
Answers
The issue ended up being that by default Program.cs isn't discoverable, so I ended up adding this to the ASP.Net .csproj file.
And adding
To the bottom of the Program.cs file
You can use
WebApplicationFactory
(which, based on docs, is a wrapper aroundTestServer
and exposes properties and methods to create and get it likeCreateServer
andServer
) for your integration tests. To set up it with the new minimal hosting model you need to make you web project internals visible to the test one for example by adding next property to csproj:And then you can inherit your
WebApplicationFactory
from the generatedProgram
class for the web app:And then in the test:
Or use
WebApplicationFactory<Program>
from the test directly:Code examples from migration guide.