Document why output from ASP.NET Core handlers (WebApplicationFactory) is not captured, and the workarounds
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 83/100
- Issue 类型
- 文档
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- csharp
调研方向
从 Known problems and workarounds 页面或 TestContext 输出文档开始,然后查看链接的 NUnit 讨论和框架 issue,了解其行为和注意事项。添加一个章节,说明为什么 ASP.NET Core 请求处理程序的输出不会被捕获,描述使用 logger 和执行上下文的解决方法,并包含 [OneTimeSetUp] 的限制。完成标准是读者能够找出原因并选择一种解决方法。
由索引模型根据 Issue 内容生成。
描述
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
TestExecutionContextis not present where the output is written. - A community package, agoda-com/NUnit.KestrelLogging, captures the
TestExecutionContextwhen 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:
- The symptom: output from code running inside the hosted app is not captured.
- The cause in one paragraph: no shared execution context between the test thread and Kestrel's request threads.
- Options: use the app's
ILoggerwith a test logger provider that writes toTestContext.Progress; or restore the capturedExecutionContext/TestExecutionContextin middleware (link the community package as an example, not an endorsement). - 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 分钟
- 30 天内合并 PR
- 6
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
nunit/docs 的其他 Issue
-
area:adapter type:newpage
难度 2/5 1-3 小时 新手友好度 82/100
-
type:newpage
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 3/5 1-2 天 新手友好度 15/100
-
难度 4/5 3-5 天 新手友好度 35/100
-
area:docfx pri:p1 type:design
难度 3/5 1-2 天 新手友好度 35/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
audacity/audacity.github.io#118 ·
-
bug improve content
难度 1/5 1 小时以内 新手友好度 92/100
opensafely/documentation#2102 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 75/100
agentregistry-dev/website#69 ·
-
15.0
难度 2/5 1-3 小时 新手友好度 66/100
-
docs: add SECURITY.md vulnerability reporting policy可能已有人在做 @AK-Lmn 今天认领。 未关闭
难度 1/5 1-3 小时 新手友好度 72/100
flisk-sh/linux2wasm#12 ·
维护者通常 1 天内回复