How to test application running inside Visual Studio Container Tools

Viewed 22

How can I run tests from TestExplorer on app dockerized using Visual Studio Container Tools?

I have aspnet solution with multiple webapi apps:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () =>
{
    return "Hello world";
});

app.Run();

I would like to test my services:


public class UnitTest1
{
    private HttpClient _client;

    public UnitTest1()
    {
        _client = new HttpClient();
    }

    [Fact]
    public async Task Test1Async()
    {
        await _client.GetAsync("http://webapi1");
    }

    [Fact]
    public async Task Test2Async()
    {
        await _client.GetAsync("http://webapi2");
    }
}

I have following docker compose:

services:
  webapi1:
    image: ${DOCKER_REGISTRY-}webapi1
    build:
      context: .
      dockerfile: webapi1/Dockerfile
  webapi2:
    image: ${DOCKER_REGISTRY-}webapi2
    build:
      context: .
      dockerfile: webapi2/Dockerfile
  tests:
    image: ${DOCKER_REGISTRY-}tests
    build:
      context: .
      dockerfile: tests/Dockerfile

In this setup I can login onto my test container and run dotnet test to run tests. However there seems to be no way to run tests using test explorer, or debug them.

0 Answers
Related