簡體   English   中英

"如何在測試另一個類方法的測試中模擬一個類方法?"

[英]How to mock a class method in the test which tests another class method?

我有以下課程,並且正在對method2<\/code>進行測試。

y.py<\/strong>

class C:
    @classmethod
    async def method1(cls):  # to be mocked
        pass

    @classmethod
    async def method2(cls):  # to be tested
        await cls.method1()

您正在這里測試異步功能。 因此,您應該使用 AsyncMock。 使用 async_mock 您可以使用 awaited_onced 而不是調用一次。 你的代碼看起來像

from unittest.mock import AsyncMock
@pytest.mark.asyncio
async def test_method2():
    C.method1 = AsyncMock()
    await C.method2()
    C.method1.assert_awaited_once() 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM