Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion TUnit.Aspire/AspireFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using TUnit.Core;
using TUnit.Core.Interfaces;

Expand Down Expand Up @@ -123,6 +124,16 @@ protected virtual void LogProgress(string message)
/// </remarks>
protected virtual string[] Args => [];

/// <summary>
/// Override to configure the <see cref="DistributedApplicationOptions"/> and
/// <see cref="Microsoft.Extensions.Hosting.HostApplicationBuilderSettings"/> passed to
/// <see cref="DistributedApplicationTestingBuilder.CreateAsync{TEntryPoint}(string[], Action{DistributedApplicationOptions, Microsoft.Extensions.Hosting.HostApplicationBuilderSettings}, CancellationToken)"/>.
/// This callback is invoked during builder creation, before <see cref="ConfigureBuilder"/>.
/// </summary>
/// <param name="options">The distributed application options.</param>
/// <param name="settings">The host application builder settings.</param>
protected virtual void ConfigureAppHost(DistributedApplicationOptions options, HostApplicationBuilderSettings settings) { }

/// <summary>
/// Override to customize the builder before building the application.
/// </summary>
Expand Down Expand Up @@ -201,7 +212,7 @@ public virtual async Task InitializeAsync()
var sw = Stopwatch.StartNew();

LogProgress($"Creating distributed application builder for {typeof(TAppHost).Name}...");
var builder = await DistributedApplicationTestingBuilder.CreateAsync<TAppHost>(Args);
var builder = await DistributedApplicationTestingBuilder.CreateAsync<TAppHost>(Args, ConfigureAppHost);
ConfigureBuilder(builder);
LogProgress($"Builder created in {sw.Elapsed.TotalSeconds:0.0}s");

Expand Down
5 changes: 4 additions & 1 deletion docs/docs/examples/aspire.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public class ApiTests(AppFixture fixture)
│ FIXTURE LIFECYCLE │
├──────────────────────────────────────────────────────────────────┤
│ 1. CreateAsync<TAppHost>() Build the Aspire test builder │
│ ↳ ConfigureAppHost() Configure options & host settings │
│ 2. ConfigureBuilder() Your customization hook │
│ 3. BuildAsync() Build the distributed app │
│ 4. StartAsync() Start containers & projects │
Expand Down Expand Up @@ -197,6 +198,7 @@ When a timeout occurs, the error includes:
| `InitializeAsync()` | Full lifecycle | Override to add post-start logic (migrations, seeding) |
| `DisposeAsync()` | Stop and dispose app | Override to add custom cleanup |
| `Args` | Empty | Command-line arguments passed to the AppHost entry point |
| `ConfigureAppHost(options, settings)` | No-op | Configure `DistributedApplicationOptions` and `HostApplicationBuilderSettings` during builder creation |
| `ConfigureBuilder(builder)` | No-op | Customize the builder before building |
| `ResourceTimeout` | 60 seconds | How long to wait for startup and resources |
| `WaitBehavior` | `AllHealthy` | Which resources to wait for |
Expand Down Expand Up @@ -246,8 +248,9 @@ public class AppFixture : AspireFixture<Projects.MyAppHost>
}
```

:::tip When to use `Args` vs `ConfigureBuilder`
:::tip When to use `Args` vs `ConfigureAppHost` vs `ConfigureBuilder`
- Use **`Args`** for configuration values that the AppHost reads during `CreateBuilder(args)` — these must be set *before* the builder is created.
- Use **`ConfigureAppHost`** to configure `DistributedApplicationOptions` (e.g., `DisableDashboard`) and `HostApplicationBuilderSettings` — these are passed to `CreateAsync` during builder creation.
- Use **`ConfigureBuilder`** for service registrations, HTTP client defaults, and other configuration that can be applied *after* the builder is created.
:::

Expand Down
Loading