AspNetCore.Docs
AspNetCore.Docs copied to clipboard
.NET 6 update: Integration tests
The lack of a Startup file breaks the examples Since the removal of the startup file in ASP.NET Core 6 even the first example on the page breaks.
How are controllers in ASP.NET 6 supposed to be integration tested?
*EDIT by @Rick-Anderson : Change monikers to .NET 6
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 77889d43-fedb-f8b4-8fea-379ec946555c
- Version Independent ID: f499b131-8b14-7e10-1728-543fdd5a5656
- Content: Integration tests in ASP.NET Core
- Content Source: aspnetcore/test/integration-tests.md
- Product: aspnet-core
- Technology: aspnetcore-test
- GitHub Login: @Rick-Anderson
- Microsoft Alias: riande
Do we have any updates on this front or at least some examples if the docs aren't updated completely yet? Seems like this is quite a common issue.
@hughesjs I went with the CustomWebApplicationFactory-route mentioned in the docs. One thing the docs don't mention is that if your tests are in a different assembly then (because the Program class is no longer public) you have to append the following to your Program.cs:
public partial class Program {}
The docs state you can do beckerrobin's suggestion above, or modify the .csproj file to have these lines (from https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory):
<ItemGroup>
<InternalsVisibleTo Include="MyTestProject" />
</ItemGroup>
However, if you do not use public partial class Program {}, you will encounter a compilation error "CS0051: Inconsistent accessibility" when trying the next section sample code with BasicTests, because Program is less accessible than the public BasicTests constructor. I wonder if it makes sense to just to remove out the csproj alternative to avoid confusion? Or otherwise mention that the public partial class Program {} is needed for the sample BasicTests to work?
Thank you.
@beckerrobin In the sample code for BasicTests, you can update WebApplicationFactory<RazorPagesProject.Startup> to be WebApplicationFactory<Program> in order for that to work, since in .NET6, Startup has been combined within Program.
As a novice I guess Integration tests in ASP.NET Core is a bit confusing. It would be better to state that "Expose internal types from the web app to the test project" doesn't work for simple test section and the following is mandatory to be added to the SUT Project file.
public partial class Program { }
Also since the ASP.NET Core 6.0 create project templates doesn't create a Startup file, it would be better to update the code to use Program file.
using Microsoft.AspNetCore.Mvc.Testing;
namespace RazorPagesProject.Tests.IntegrationTests
{
public class BasicTests : IClassFixture<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> _factory;
public BasicTests(WebApplicationFactory<Program> factory)
{
_factory = factory;
}
[Theory]
[InlineData("/")]
[InlineData("/Index")]
[InlineData("/About")]
[InlineData("/Privacy")]
[InlineData("/Contact")]
public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
{
// Arrange
var client = _factory.CreateClient();
// Act
var response = await client.GetAsync(url);
// Assert
response.EnsureSuccessStatusCode(); // Status Code 200-299
Assert.Equal("text/html; charset=utf-8",
response.Content.Headers.ContentType.ToString());
}
}
}
Moved to #27089
This ticket is not finished and the current .Net 6.0 version of this document does not work out of the box. The answer is in this ticket, but you have not implemented the fix. Also, the code that is referenced has a Startup, so it's likely .Net 5 or less and needs to be updated. Please reopen this ticket.
This ticket is not finished and the current .Net 6.0 version of this document does not work out of the box. The answer is in this ticket, but you have not implemented the fix. Also, the code that is referenced has a Startup, so it's likely .Net 5 or less and needs to be updated. Please reopen this ticket.
The .NET 7 version works with .NET 6.
So you're agreeing with me?
Who's going to know that unless they think to look in the GitHub feedback?
What kind of Quality is that? "The .NET 7 version works with .NET 6." So we're just going to leave the .Net 6 version forever broken and never fix it because it would take 30 seconds I don't have?
This hasn't worked since at least May and you knew about it, and you decided to just let it slide?!
How many people have wrestled with this document and left in frustration and how many more, even after 7 is released?