microsoft / microsoft/aspire

Azure function not launched by Aspire when running test in normal test mode

Open
#13,789 3 comments 1 reaction 0 assignees View on GitHub
area-app-testing
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

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.1
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("my-function-app")
.WithExternalHttpEndpoints();

builder.Build().Run();
```
Test file (mytest.cs)
```
public async ValueTask InitializeAsync()
{
var appHost = await DistributedApplicationTestingBuilder
.CreateAsync();

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 = "test@example.com"
};

// 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
```


Exe
net10.0
enable
enable
true







```
csproj - abc.tests
```


net10.0
enable
enable
false
true




runtime; build; native; contentfiles; analyzers; buildtransitive
all












```
csproj - MyFunctionApp
```


net8.0
v4
enable
enable
Exe









```

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!

Contributor guide

Open the contributing guide

Research direction

Start with the Aspire apphost Program.cs and mytest.cs, then run the integration test through dotnet test and in debug mode while observing the func process with lsof -i TCP | grep LISTEN. Review the three project files and the Azure Functions hosting setup to compare the two launch paths. Done means the Azure Function starts during normal test execution and the test reaches its HTTP assertion.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp
Domain
cloud, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.