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

オープン 初心者向け
#1,296 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

@SeanKilleen がすでに取り組んでいます。

2026年10月25日 から。

  • #1306 @SeanKilleen による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
83/100
issue の種類
ドキュメント
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp
領域
documentation

調査の方向性

Known problems and workarounds ページまたは TestContext の出力に関するドキュメントから始め、リンクされている NUnit の議論とフレームワークの issue を確認して、動作と注意点を把握します。ASP.NET Core のリクエストハンドラーからの出力がキャプチャされない理由を説明し、logger と実行コンテキストを使った回避策を記述して、[OneTimeSetUp] の制限を含むセクションを追加します。読者が原因を特定し、回避策を選べるようになれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Dockerfile
スター
601
フォーク
159
平均マージ
22時間 18分
マージ済み PR(30日)
6

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

nunit/docs のほかの issue

nunit/docs の issue をすべて見る

似ている issue

Documentation の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。