Replies: 7 comments 59 replies
-
Yes, I'm supposed to write a sample, but there's currently one here: https://github.com/dotnet/eShop/blob/main/tests/Catalog.FunctionalTests/CatalogApiFixture.cs Still lots of clean up to do but definitely on the roadmap. |
Beta Was this translation helpful? Give feedback.
-
We're noticing that in Rider and VS.net that it will only allow debugging the test itself, we can't debug into the projects with breakpoints etc. And in vs code the tests don't work at all for debugging. Is there documentation on how to get this working? Is there work to get this working? |
Beta Was this translation helpful? Give feedback.
-
Is there some reference on running this in CI where you are already inside docker? |
Beta Was this translation helpful? Give feedback.
-
The solution works from the CatalogApiFixture, but what if I use an integration in the project like Seq? Program.cs for ApiService from AspireStarterProject Program.cs (AppHost)
This is because the reference to the Api project in the csproj of the integration test is
Link to Repo: https://github.com/Pacman1988/AspireAppIntegrationTestWithSeq |
Beta Was this translation helpful? Give feedback.
-
@davidfowl it's been a bit, and just wanted to see if there's any progress or additional thoughts on leveraging WebApplicationFactory in integration tests? I have successfully used your sample on a few different services in our org but it requires some heavier lifting to manually perform service discovery when I have multiple components that want to talk to each other. There's also a hard limit on some local container dependency calling our service because the WebApplicationFactory is only available in-proc - I haven't come across this use case but could see it happening. I know there's a ton of ongoing work with Aspire to prioritize so no worries if no update here. Additionally, I want to commend the team on the overall vision and product - it really is a joy to work with. |
Beta Was this translation helpful? Give feedback.
-
I use something like that with IDistributedApplicationTestingBuilder as Apphost and I clean and modify the "normal" resources (remove persistent lifetime or remove data volumes) It seems to work (I m at the begining) public class AspireFixture : IAsyncLifetime
{
public IDistributedApplicationTestingBuilder AppHost { get; private set; } = default!;
public HttpClient AppHttpClient { get; private set; } = default!;
public HttpClient AuthHttpClient { get; private set; } = default!;
public ResourceNotificationService ResourceNotificationService { get; private set; } = default!;
public DistributedApplication? App { get; private set; }
public async Task InitializeAsync()
{
AppHost = await DistributedApplicationTestingBuilder
.CreateAsync<Projects.UbikLink_AppHost>();
AppHost.Services.ConfigureHttpClientDefaults(clientBuilder =>
{
clientBuilder.AddStandardResilienceHandler();
});
//Remove useless resources for testing
RemoveNotNeededResourcesForTesting();
//Change config for testing
ModifyResourcesForTesting();
App = await AppHost.BuildAsync();
ResourceNotificationService = App.Services
.GetRequiredService<ResourceNotificationService>();
await App.StartAsync();
AppHttpClient = App.CreateHttpClient("ubiklink-proxy");
AuthHttpClient = App.Services.GetRequiredService<IHttpClientFactory>().CreateClient("auth-httpclient");
//TODO: Change that to be in config
AuthHttpClient.BaseAddress = new Uri("https://myauth/oauth/token");
await ResourceNotificationService.WaitForResourceAsync(
"ubiklink-proxy",
KnownResourceStates.Running
)
.WaitAsync(TimeSpan.FromSeconds(30));
}
private void RemoveNotNeededResourcesForTesting()
{
var pgAdminResources = AppHost.Resources
.Where(r => r.GetType() == typeof(PgAdminContainerResource))
.ToList();
foreach (var pgAdmin in pgAdminResources)
{
AppHost.Resources.Remove(pgAdmin);
}
}
private void ModifyResourcesForTesting()
{
var cache = AppHost.Resources.Where(r => r.Name == "cache")
.FirstOrDefault();
var db = AppHost.Resources.Where(r => r.Name == "ubiklink-postgres")
.FirstOrDefault();
if (cache != null)
{
var containerLifetimeAnnotation = cache.Annotations
.OfType<ContainerLifetimeAnnotation>()
.FirstOrDefault();
if (containerLifetimeAnnotation != null)
{
cache.Annotations.Remove(containerLifetimeAnnotation);
}
}
if (db != null)
{
var containerLifetimeAnnotation = db.Annotations
.OfType<ContainerLifetimeAnnotation>()
.FirstOrDefault();
if (containerLifetimeAnnotation != null)
{
db.Annotations.Remove(containerLifetimeAnnotation);
}
var dataVolumeAnnotation = db.Annotations
.OfType<ContainerMountAnnotation>()
.FirstOrDefault();
if (dataVolumeAnnotation != null)
{
db.Annotations.Remove(dataVolumeAnnotation);
}
}
}
public async Task DisposeAsync()
{
AppHttpClient?.Dispose();
AuthHttpClient?.Dispose();
if (App != null)
{
if (App is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync().ConfigureAwait(false);
}
else
{
App.Dispose();
}
}
}
}
[CollectionDefinition("AspireApp collection")]
public class AspireAppCollection : ICollectionFixture<AspireFixture>
{
// This class has no code, and is never created. Its purpose is simply
// to be the place to apply [CollectionDefinition] and all the
// ICollectionFixture<> interfaces.
} In this example, I removed pgadmin container and cleaned persistence annotations (lifecycle + volume). Because the testbuilder is not able to reuse the resources and it creates a big mess. I don't know if it's fine ? It seems to allow me to test my Yarp endpoints with all the "true" resources behind. (without webfactory). |
Beta Was this translation helpful? Give feedback.
-
You might want to have a look at this answer on how to perform functional unit tests with Aspire. It spins up |
Beta Was this translation helpful? Give feedback.
-
I've only heard about Aspire yesterday but it sounds like the perfect fit for a new project I've been working on. In my current solution I have a distributed system and I do a lot of the stuff Aspire does manually, I run everything in containers and manually wire up url's using docker-compose etc etc. Aspire looks like a great alternative to this as it is, admittedly, a headache.
The only thing that concerns me is there is no mention of integration testing using
WebApplicationFactory
along with the Aspire host. I'm not sure how this would work, but does anyone know if there is support for something similar on the roadmap? It seems like a no brainer that we are provided with something along these lines for Aspire.Thanks
Beta Was this translation helpful? Give feedback.
All reactions