Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock up a class variable value in python unit test?

Say I have a class:

class A():
  def f(self):
    self._v = 1

Tried:

m=Mocker()
A.f._v = m.mock()
...

but didn't work. Not sure how...

like image 488
Hailiang Zhang Avatar asked Dec 21 '22 04:12

Hailiang Zhang


1 Answers

Did you mean Mock library?

from mock import Mock
real = ProductionClass()
real.method = Mock(return_value=3)
real.method(3, 4, 5, key='value')

edit:

You are trying to access A.f._v before mocking which is impossible. Not sure what are you trying to do, but this will work

>>>A.f = Mock()
>>>a = A()
>>>a.f._v
<Mock name='mock._v' id='42076240'>
like image 108
reclosedev Avatar answered Dec 31 '22 13:12

reclosedev