AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

.NET 6 update: Integration tests

Open beckerrobin opened this issue 4 years ago • 13 comments

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.

beckerrobin avatar Oct 15 '21 08:10 beckerrobin

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 avatar Nov 16 '21 14:11 hughesjs

@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 {}

beckerrobin avatar Nov 17 '21 14:11 beckerrobin

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.

GoldenCave avatar May 14 '22 14:05 GoldenCave

@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.

GoldenCave avatar May 14 '22 14:05 GoldenCave

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

build5 avatar May 15 '22 14:05 build5

Moved to #27089

Rick-Anderson avatar Sep 30 '22 02:09 Rick-Anderson

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.

bluebaroncanada avatar Oct 13 '22 04:10 bluebaroncanada

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.

Rick-Anderson avatar Oct 13 '22 19:10 Rick-Anderson

So you're agreeing with me?

bluebaroncanada avatar Oct 13 '22 20:10 bluebaroncanada

Who's going to know that unless they think to look in the GitHub feedback?

bluebaroncanada avatar Oct 13 '22 20:10 bluebaroncanada

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?

bluebaroncanada avatar Oct 13 '22 20:10 bluebaroncanada

This hasn't worked since at least May and you knew about it, and you decided to just let it slide?!

bluebaroncanada avatar Oct 13 '22 20:10 bluebaroncanada

How many people have wrestled with this document and left in frustration and how many more, even after 7 is released?

bluebaroncanada avatar Oct 13 '22 20:10 bluebaroncanada