I am trying to find example usage and some documentation explaining the difference between using TestServer
and WebApplicationFactory
. I have seen this but I get the failure
System.InvalidOperationException : No method 'public static IWebHostBuilder CreateWebHostBuilder(string[] args)' found on 'AutoGeneratedProgram'. Alternatively, WebApplicationFactory
1 can be extended and 'protected virtual IWebHostBuilder CreateWebHostBuilder()' can be overridden to provide your own IWebHostBuilder instance. at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory
1.CreateWebHostBuilder()
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.EnsureServer() at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory
1.CreateDefaultClient(DelegatingHandler[] handlers)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
To replicate I installed the package Microsoft.AspNetCore.Mvc.Testing
and have the following test.
public class UnitTest1 : IClassFixture<WebApplicationFactory<UnitTest1.TestStartup>>
{
private readonly WebApplicationFactory<TestStartup> _factory;
public UnitTest1(WebApplicationFactory<TestStartup> factory)
{
_factory = factory;
}
[Fact]
public void Test1()
{
void ConfigureTestServices(IServiceCollection services) =>
services.AddSingleton<IRepository>(new Repository());
var client = _factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
.CreateClient();
var result = client.GetAsync("api/values").Result;
Assert.Equal(result.StatusCode, HttpStatusCode.InternalServerError);
}
Ideally, some documentation on how to use it and possibly the differences between using the two (TestServer and WebApplicationFactory) would be very useful. When would you favour one over the other?
@guardrex who's the right dev to ping on this?
@javiercn
Ideally, some documentation on how to use it and possibly the differences between using the two (TestServer and WebApplicationFactory) would be very useful.
@OnamChilwan Did u see the integration testing topic? https://docs.microsoft.com/aspnet/core/test/integration-tests
@guardrex I followed the documentation but still have the same issue. I would like to stick to using TestServer but asserting a 500 Internal Server Error is difficult.
@OnamChilwan I can't say. I haven't attempted to implement such a test with the new approach. I suggest that you ask on Stack Overflow. You'll probably get a faster answer there.
TestServer is part of the new testing approach. I'm not aware of interest among the engineers to have either another topic or topic section that addresses using TestServer directly.
WebApplicationFactory is the recommended approach and that's what we want people using. TestServer is just an infrastructure concept at this point, it lacks the setup that WebApplicationFactory does for your tests to behave in a way similar to your app environment.
I'm closing this because it isn't actionable for us. The guidance is clear on what we want devs to do.
I could reproduce the issue mentioned by @OnamChilwan when using a test Startup class defined in the test project itself.
It is probably worth mentioning it in the docs.
I was able to use a Startup
class in my test project, but needed to also derive WebApplicationFactory
and manually set the content root similar to: https://github.com/aspnet/Docs/issues/7063#issuecomment-414661566.
This works with v2.1.1 of Microsoft.AspNetCore.Mvc.Testing
.
I'm struggling with this issue and above doesn't seem to work for me. My case is as follows:
MyServiceFactory : WebApplicationFactory<TestStartup>
TestStartup : MyService.Startup
(I need this so I can override an virtual method in the standard implementation to change the Auth for testing)ScenariosBase
(base class with some helper methods, ctor takes MyServiceFactory
)BookingQueryScenarios : ScenariosBase, IClassFixture<MyServiceFactory>
Fails with same exception as above on CreateClient
. I've overridden ConfigureWebHost
, setting content root with UseSolutionRelativeContentRoot
, but it throws before it even gets called.
Tried overridding CreateWebHostBuilder()
, doing base.CreateWebHostBuilder().UseStartup<Startup>()
(even though it seems redundant) and still the same issue.
Anyone got any suggestions? I'm on 2.2.0.
Nevermind, as the exception correctly was stating, I didn't have a CreateWebHostBuilder
on my Program
d'oh. I'd base classed all my microservice's Program
s and forgot to leave the CreateWebHostBuilder
method. That'll teach me. Adding back worked :)
```
public class Program : MicroServiceProgram
{
private const string ServiceName = "MyService";
public static void Main(string[] args) => Main<Startup>(ServiceName);
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => CreateWebHostBuilder<Startup>(ServiceName);
}
```
Hi.
It looks like you are posting on a closed issue!
We're very likely to lose track of your bug/feedback/question unless you:
Thanks!
Most helpful comment
I was able to use a
Startup
class in my test project, but needed to also deriveWebApplicationFactory
and manually set the content root similar to: https://github.com/aspnet/Docs/issues/7063#issuecomment-414661566.This works with v2.1.1 of
Microsoft.AspNetCore.Mvc.Testing
.