简体   繁体   中英

Mock an attribute in python class for unit testing

I would like to mock the self.var in my unit test since the name server lookup fails. I've tried bunch of things using patch , but I couldn't get it to work. What is the best way to mock self.var ?

My class definition is as below:

class A:
   def __init__(self):
       self.var = Pyro4.Proxy(self.ns.lookup(temp_name))
   def method_1(self):
   .....

My unit test goes like this:

class TestA(TestCase):
    def setUp(self):
        self.temp_A = classA()    
    def test_method(self):
       self.temp_A.method_1()`

you could do 2 things here, the simplest its just patch Pyro4.Proxy, something like this:

from unittest.mock import patch
# or from mock import create_autospec if you are in py2

class TestA(TestCase):
  @patch('Pyro4.Proxy', autospec=True)
  def test_method(self):
     temp_A = A()
     temp_A.method_1()

the trick is to patch the "right object" and don't instantiate the class till after you patch (inside your test function) , Pyro4.Proxy is the best object i could deduce from your code, but if your class A is in my_module, maybe the best object to patch is my_module.Pyro4.Proxy .

the other thing you could want is to just mock the var variable. then as you define self.var inside the __init__ method and assuming you don't want to mock the whole __init__ method then you can

from unittest.mock import create_autospec
# or from mock import create_autospec if you are in py2

class TestA(TestCase):
  def setUp(self):
     self.temp_A = classA()
     self.temp_A.var = create_autospec(Pyro4.Proxy)(None)

  def test_method(self):
     self.temp_A.method_1()

if you can refactor your class a bit i would suggest make the var a property for easy mocking

class A(object):
   def __init__(self):
         ...
   @property
   def var(self):
       return Pyro4.Proxy(self.ns.lookup(temp_name))

   def method_1(self):
       .....

and then your testing class can be

from unittest.mock import create_autospec, PropertyMock
class TestA(TestCase):
  def setUp(self):
     self.temp_A = classA()
     type(self.temp_A).var = PropertyMock(side_effect=lambda *x, **y: create_autospec(Pyro4.Proxy)(*x, **y))

  def test_method(self):
     self.temp_A.method_1()

I think the easiest way to handle this is to stub out the class undertest.

In the test file

class AStub(A):
   def __init__(self):
     #  By pass super constructor
     self.var = None

class TestA(TestCase):
  def setUp(self):
    self.helper = AStub()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM