python runtime_error ai_generated true

名称错误:名称 'setup_method' 未定义

NameError: name 'setup_method' is not defined

ID: python/nameerror-name-not-defined

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2025-01-08首次发现

版本兼容性

版本状态引入弃用备注
3.8 active
3.9 active
3.10 active

根因分析

测试类使用了如 setup_method 的方法但未定义,或拼写错误。

English

A test class uses a method like setup_method without defining it, or it is misspelled.

generic

解决方案

  1. 95% 成功率 Define setup_method as class method
    class TestClass:
        def setup_method(self):
            pass
  2. 90% 成功率 Use pytest fixture instead
    @pytest.fixture(autouse=True)
    def setup_method(self):
        pass

无效尝试

常见但无效的做法:

  1. Defining setup_method inside test function 70% 失败

    setup_method should be a method of the test class, not nested inside a test.

  2. Using setup_method as a fixture 50% 失败

    setup_method is a unittest convention; pytest uses fixtures instead.