python / python/cpython

Add a decorator to run a test in an isolated subprocess

未关闭
#152,548 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

tests type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Some tests need to run in a fresh interpreter, isolated from the rest of the test run: they mutate process-global or interpreter state that would leak into (or be polluted by) other tests, depend on a clean interpreter startup, or may crash the interpreter outright. Today this has to be done ad hoc per test (spawning a child via script_helper, hand-rolling the result reporting), which is verbose and inconsistent.

I propose a decorator that runs a test in a dedicated subprocess and reports the result transparently, so the test reads like any other:

class MyTest(unittest.TestCase):
    @isolated()
    def test_global_state(self):
        ...

It would start in test.support and could later move to unittest.

Proposed behaviour

  • @isolated() decorates a test method (only that method runs in a subprocess) or a whole TestCase subclass (the whole class runs in a single subprocess, so setUpClass()/setUpModule() run once there).
  • Failures, errors and skips — including those of individual subTest()s — are reported for the corresponding test, and show the original subprocess traceback.
  • A setUpClass()/setUpModule() failure or skip in the subprocess is reported for the whole class.
  • The subprocess inherits the parent's regrtest configuration (enabled resources -u, memory limit -M, verbosity -v, failfast), so requires_resource(), requires(), bigmemtest() and similar behave the same in both processes.
  • A running_isolated flag is true while running in the subprocess, so setUp()/tearDown() and the class- and module-level fixtures can choose what to run there.

Linked PRs

Linked PRs
  • gh-152551
  • gh-152565
  • gh-152570
  • gh-152639
  • gh-154780
  • gh-154783
  • gh-154784
  • gh-155162
  • gh-155163
  • gh-155167
  • gh-155168
  • gh-155169
  • gh-155230
  • gh-155231
  • gh-155238

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 test.support 入口点以及 script_helper 中现有的 subprocess 处理开始。结合提议的装饰器行为,审查相关的 pull request,包括在方法和 TestCase 中的使用、结果报告、继承的 regrtest 配置以及 running_isolated。指定的失败、跳过、fixture、subTests 和 subprocess 回溯都能透明地报告,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
testing
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。