Document why output from ASP.NET Core handlers (WebApplicationFactory) is not captured, and the workarounds

Abierto Apto para principiantes
#1,296 0 comentarios 0 reacciones 0 asignados Ver en GitHub

@SeanKilleen ya está trabajando en esto.

Desde el 25/10/2026.

  • #1306 de @SeanKilleen — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
83/100
Tipo de issue
Documentación
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
csharp

Línea de trabajo

Empieza por la página «Known problems and workarounds» o la documentación sobre la salida de TestContext y, después, consulta el debate de NUnit y el issue del framework enlazados para conocer el comportamiento y las salvedades. Añade una sección que explique por qué no se captura la salida de los controladores de solicitudes de ASP.NET Core, describa las soluciones alternativas con el logger y el contexto de ejecución e incluya la limitación de [OneTimeSetUp]. La tarea estará terminada cuando los lectores puedan identificar la causa y elegir una solución alternativa.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

area:framework type:update

Summary

Console.WriteLine and TestContext.Out writes made inside an ASP.NET Core request handler during a WebApplicationFactory integration test do not appear in test output. This is a common setup and the failure is silent. Nothing in the docs explains why or what to do. The only WebApplicationFactory mention is a testhost.deps.json tip in the .NET Core getting-started page.

Evidence

nunit/nunit#5067 Console.WriteLine not showing up from AspNetCore http handler (2024, 8 comments, repro in nunit/nunit.issues Issue4860):

  • @manfred-brands' explanation: the controller runs on Kestrel's own thread/async context, and the test talks to it over HTTP, so there is no shared execution context and NUnit's per-test TestExecutionContext is not present where the output is written.
  • A community package, agoda-com/NUnit.KestrelLogging, captures the TestExecutionContext when the factory is created and restores it in the pipeline.
  • Important caveat from @manfred-brands: that approach only works when the factory is created per test (in the test body or [SetUp]). It does not work when the factory is created in [OneTimeSetUp], because every test gets a new output writer.
  • Related framework issue nunit/nunit#4728.

Proposed change

Add a section to the adapter Known problems and workarounds page, or a short recipe near TestContext output docs, covering:

  1. The symptom: output from code running inside the hosted app is not captured.
  2. The cause in one paragraph: no shared execution context between the test thread and Kestrel's request threads.
  3. Options: use the app's ILogger with a test logger provider that writes to TestContext.Progress; or restore the captured ExecutionContext / TestExecutionContext in middleware (link the community package as an example, not an endorsement).
  4. The [OneTimeSetUp] caveat, since that is how most people build their factory.

This is framework behavior rather than adapter behavior, so if we would rather not put it under the adapter, a "Capturing output" subsection on the TestContext page would also fit. Mentioning it there would also help with the stale TestContext content tracked in #531.

Lenguaje dominante
Dockerfile
Estrellas
601
Forks
159
Merge medio
22 h 18 min
PR fusionados (30 d)
6

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de nunit/docs

Todos los issues de nunit/docs

Issues similares

Más issues de Documentation

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.