ARTICLE AD BOX
I have a weird problem that I need som input on when running a test in normal test mode (not debug).
Tools
Visual Studio code, Version: 1.107.0
Mac OS, M4
Background
An azure function exists, we can call it abc.MyFunctionApp. This is a folder outside the test folder, i.e. the real implementation that starts the azure functions.
Inside the test folder, there are two folders: abc.tests and abc.aspire.tests.apphost
Inside the csproj for the aspire apphost project, Im referencing my abc.MyFunctionApp
The problem
I have an integration test that is supposed to use Aspire to launch the azure function in abc.MyFunctionApp. The weird problem is that it works fine when running the test in debug mode but when running in normal test mode, the function never starts up and test is timing out.
Tried it on both Mac and PC, same problem.
Some code to illustrate this
Aspire apphost (Program.cs)
var builder = DistributedApplication.CreateBuilder(args); builder .AddAzureFunctionsProject<abc.MyFunctionApp>("my-function-app") .WithExternalHttpEndpoints(); builder.Build().Run();Test file (mytest.cs)
public async ValueTask InitializeAsync() { var appHost = await DistributedApplicationTestingBuilder .CreateAsync<Projects.abc_aspire_tests_apphost>(); App = await appHost.BuildAsync(); await App.StartAsync(); // doesn't launch azure function await Task.Delay(TimeSpan.FromSeconds(30)); HttpClient = App.CreateHttpClient("my-function-app"); HttpClient.Timeout = TimeSpan.FromSeconds(30); ... } [Fact] public async Task MyFunctionApp_ReturnsResponse() { // Arrange var request = new { Email = "[email protected]" }; // Act try { response = await _fixture.HttpClient!.PostAsJsonAsync("/api/endpoint", request, TestContext.Current.CancellationToken); // not working since function is not launched } catch (Exception ex) { // ... } // Assert ... }csproj - abc.aspire.tests.apphost
<Project Sdk="Microsoft.NET.Sdk"> <Sdk Name="Aspire.AppHost.Sdk" Version="13.0.0" /> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net10.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <IsAspireHost>true</IsAspireHost> </PropertyGroup> <ItemGroup> <PackageReference Include="Aspire.Hosting.AppHost" Version="13.0.0" /> <PackageReference Include="Aspire.Hosting.Azure.Storage" Version="13.0.0" /> <PackageReference Include="Aspire.Hosting.Azure.Functions" Version="13.0.0-preview.1.25560.3" /> </ItemGroup> <ItemGroup> <ProjectReference Include="../abc.MyFunctionApp.csproj" /> </ItemGroup> </Project>csproj - abc.tests
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net10.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <IsPackable>false</IsPackable> <IsTestProject>true</IsTestProject> </PropertyGroup> <ItemGroup> <PackageReference Include="Aspire.Hosting.Testing" Version="13.0.0" /> <PackageReference Include="xunit.v3" Version="1.0.0" /> <PackageReference Include="xunit.runner.visualstudio" Version="3.0.0"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" /> </ItemGroup> <!-- Reference to the AppHost project --> <ItemGroup> <ProjectReference Include="../abc.aspire.tests.apphost/abc.aspire.tests.apphost.csproj" /> </ItemGroup> <ItemGroup> <Using Include="System.Net" /> <Using Include="System.Net.Http.Json" /> <Using Include="Aspire.Hosting" /> <Using Include="Aspire.Hosting.ApplicationModel" /> <Using Include="Aspire.Hosting.Testing" /> <Using Include="Xunit" /> </ItemGroup> </Project>csproj - MyFunctionApp
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <OutputType>Exe</OutputType> </PropertyGroup> <ItemGroup> <FrameworkReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.51.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.1.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.3.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="6.4.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.7" /> </ItemGroup> </Project>I hope this will be enough information. So, when running the test in debug mode, everything works fine but when running in normal test mode (both via VS or through dotnet test), no azure function spins up. Even if I set a sleep for a long time, nothing pops up.
I can verify it by running the following command to see which processes runs in the background:
lsof -i TCP | grep LISTEN
If running in debug mode, you can see a row called "func" starting up. But when running in test mode, no row shows up related to "func". I think this is an indicator that my azure function never launches.
If I go directly to my abc.MyFunctionApp and run "func start" from the terminal, then the azure function launches without problem.
Anyone else seen this problem before? Would appreciate some input, thanks!
