Skip to content

[Feature Request] enhancing for spec / create_autospec and error handling. #5

@skokado

Description

@skokado

Feature requests in PyCon JP 2025 Sprint.

1. Add supoprt for differnt types of Mock Creation

Extend the mock system to support different ways of generating mocks (autospec, spec, magic, default). This allows developers to enforce stricter testing, handle special SDKs, and test side effects more realistically.

Motivation
Inspired from unittest.mock usage, we can make restrictly for test target class / module.

  • Current mocks only support return_value and side_effect.

Example interfaces are:

  • Enforce correct signatures (autospec).
  • Limit attribute exposure (spec).
  • Handle dunder methods (magic).
  • Fall back to lightweight mocks (default).

Example: mocking genai.generate_content with either autospec or side effect behavior.

{
  "mocked_modules": {
    "genai": {
      "methods": {
        "generate_content": {
          "mode": "autospec",
          "return_value": "my_val",
          "side_effect": "ValueError"
        }
      }
    }
  }
}
  • mode: "default" | "autospec" | "spec" | "magic"
  • return_value: any JSON-serializable value (optional)
  • side_effect:
    • String: exception name (e.g. "ValueError")
    • List: sequence of values/exceptions
    • Future: callable reference

2. Automatically mock stop

referring from @patch decorator, following idea would be useful?

  • As is:
class TestFileSystem(unittest.TestCase):
    def setUp(self):
        self.mock_manager = MockManager('./mocks/os_mock_data.json')
        self.mock_manager.apply_mocks()

    def tearDown(self):
        self.mock_manager.stop_mocks()

    ...
  • To be?
@mock_from_json(path="./mocks/os_mock_data.json")
class TestFileSystem(unittest.TestCase):
    # setUp and tearDown is unnecessary

    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions