1

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()`

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.