修补已修补的 class 的方法

Patching a method of a class that is already patched

我正在尝试修补 class 及其方法,这是我正在尝试做的一个示例:

class Car:
    def __init__(self, color):
        self.color = color

    def show_color(self):
        return self.color

我有这个 class,我想单独修补 class 及其方法,我没有创建假的 class,因为在我的情况下 class 很复杂,所以我想创建 class 然后只修补一些具有 class 的方法,我正在尝试这样做:

import example

def test_example(
    mocker
):
    mocker.patch("example.Car")
    mocker.patch("example.Car.show_color").return_value = "red"
    c = example.Car("blue")
    assert c.show_color() == "red"

此外,我尝试做这样的事情:

import example

def test_example(
    mocker
):
    mocker.patch("example.Car").return_value = mocker.create_autospec(
        example.Car,
        show_color = "red"
    )
    c = example.Car("blue")
    assert c.show_color() == "red"

在这两种情况下,我得到了这个错误:

========================================================================================== FAILURES ==========================================================================================
________________________________________________________________________________________ test_example ________________________________________________________________________________________

mocker = <pytest_mock.plugin.MockerFixture object at 0x102281bb0>

    def test_example(
        mocker
    ):
        mocker.patch("example.Car")
        mocker.patch("example.Car.show_color").return_value = "red"
        c = example.Car("blue")
>       assert c.show_color() == "red"
E       AssertionError: assert <MagicMock name='Car().show_color()' id='4331364896'> == 'red'
E        +  where <MagicMock name='Car().show_color()' id='4331364896'> = <MagicMock name='Car().show_color' id='4331344512'>()
E        +    where <MagicMock name='Car().show_color' id='4331344512'> = <MagicMock name='Car()' id='4331304944'>.show_color

test_mocking.py:21: AssertionError
================================================================================== short test summary info ===================================================================================
FAILED test_mocking.py::test_example - AssertionError: assert <MagicMock name='Car().show_color()' id='4331364896'> == 'red'
===================================================================================== 1 failed in 0.06s ======================================================================================

我正在修补该方法,因为我有另一个 class 的方法我想测试

你正在打补丁:

show_color = "red"

但是你的断言试图调用 show_color 就好像它是一个函数

if c.show_color() == “red”:

这是注定要失败的,也许你应该测试一下:

if c.show_color == “red”:

mocker.patch 本身 returns 模拟,然后您可以与之交互:

def test_example(mocker):
    Car = mocker.patch("example.Car")
    Car.return_value.show_color.return_value = "red"
    c = example.Car("blue")
    assert c.show_color() == "red"

或者,patch 接受 **kwargs,您可以使用它来配置 sub-mocks:

def test_example_alternative(mocker):
    config = {"return_value.show_color.return_value": "red"}
    mocker.patch("example.Car", **config)
    c = example.Car("blue")
    assert c.show_color() == "red"