python / python/cpython

Unittest Patch only works on attributes, methods

未關閉
#129,629 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

I am having an issue where the patch method from the unittest.mock module is only working when I patch attributes of imported modules in other files. I do not understand if this is an issue of my import patterns or a misunderstanding of how patching works. (I am importing the app code before the patch is able to run).

I devised a test to show how the functionality is confusing me:

In my app code I have the following code

    from datetime import date
    ...
    import boto3
    ...
    from common.functions import asodates2datetime
    from core.mixins.usecases import ASOUsecaseTools


    class foo:
        @staticmethod
        @ASOUsecaseTools.get_by_name
        def bar(self, relevant_date):
            client = boto3.client("s3")
            today = date.today()
            date = asodates2datetime(relevant_date)

And from my test code I can patch in the following way:

    @mock.patch("src.aoi.usecases.date")
    @mock.patch("src.aoi.usecases.ASOUsecaseTools.get_by_name")
    @mock.patch("src.aoi.usecases.asodates2datetime")
    @mock.patch("src.aoi.usecases.boto3.client")
    def test_foo_gets_bar(self, m_d, m_g, m_a, m_c):
        m_d.today.return_value = <date>
        m_d.side_effect = lambda *args, **kw: date(*args, **kw)
        # more patching of functions here

However, when the test is run, If I set a breakpoint in the bar function and check the value of the patched items, only the patched methods show up as MagicMock

    >>> boto3.client
        <MagicMock name='client...>
    >>> ASOUsecaseTools.get_by_name
        <MagicMock name='get_by_name...>
    >>> asodates2datetime
        <function asodates2datetime...>
    >>> date
        <class 'datetime.date'>

The thing that confuses me is that I am patching along with how the docs say to patch: https://docs.python.org/3/library/unittest.mock-examples.html#partial-mocking

Also the fact that patched methods work but classes and functions don't indicates that the import structure is not a problem because the imported methods would be overwritten in the same way as the imported classes. It also indicates that the import paths are not incorrect; as the only difference between the patches is the type of object they are patching.

My current workaround is using the freeze package to freeze the date, but I prefer using builtin packages if possible.

CPython versions tested on:

3.9

Operating systems tested on:

macOS

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 issue 中提供的 unittest.mock 重現程式開始,並將其 patch 目標與連結的 partial mocking 文件進行比較。在 CPython 3.9 上驗證行為,然後確定觀察到的差異究竟是可重現的直譯器 bug,還是對 import/patching 的誤解;一個具體的失敗測試和預期行為將定義完成標準。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
testing-qa
Issue 類型
缺陷
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
需要釐清
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。