【问题标题】:DotNet alternative to spring boot @LocalServerPort for integration tests用于集成测试的 Spring Boot @LocalServerPort 的 DotNet 替代方案
【发布时间】:2026-01-28 10:20:03
【问题描述】:

我正在尝试测试 DotNet Core 应用程序的回调功能。我已经公开了我想在集成测试中调用的控制器。

我在https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1 之后使用 xUnit 设置了集成测试 在测试中,我能够使用 WebApplicationFactory 准备客户端。可以使用此客户端调用所需的控制器。

但我关心的是如何从外部调用控制器。换句话说,它是否暴露在某个 localhost 端口上?我找不到这样的。 它没有监听 5000(我的 appsettings 中定义的端口)。它也没有监听端口 80(默认端口)。

我想拥有类似 spring boot @LocalServerPort 的东西并在测试中使用它。有这种可能吗?

【问题讨论】:

    标签: .net testing integration xunit


    【解决方案1】:

    如果有人想尝试,可以这样:https://github.com/dotnet/aspnetcore/issues/4892

    【讨论】: