python runtime_error ai_generated true

NameError: name 'setup_method' is not defined

ID: python/nameerror-name-not-defined

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-01-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active
3.10 active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. Defining setup_method inside test function 70% fail

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

  2. Using setup_method as a fixture 50% fail

    setup_method is a unittest convention; pytest uses fixtures instead.