Python mock.patch class instance attribute

Viewed 27

Is it possible to patch an instance attribute of a class with only a path string and not directly by importing the class and using mock.patch.object

Example:

file.py

class A:
    def __init__(self):
        self.a = 1

test_file.py

instead of

import mock
from file import A

@mock.patch.object(A, 'a', 2)
def test_a(self):
    print(A.a)

something like

import mock

@mock.patch("config.A.a", return_value=2)
def test_a(self):
    print(A.a)
0 Answers
Related