Testing a function call inside the function using

2019-09-02 03:04发布

I want to test this class using python unittest framework and also mockito.

class ISightRequestEngine(object):
    def __init__(self, pInputString=None):
        self.__params = (pInputString)

    def openHTTPConnection(self):
        pass

    def __closeHTTPConnection(self):
        pass

    def testFunc(self):
        print 'test function called'

    def startEngine(self):
        self.__params.parseinputString()
        self.openHTTPConnection()
        self.testFunc()

    def processRequest(self, header = None):
        pass

I wanted to test that function startEngine() calls testFunc().

Similar to what we do in our mocked class,

obj = mock(ISightRequestEngine)
obj.startEngine()

try:
    verify(obj).startEngine()
except VerificationError:

Unfortunately this only verifies whether the startEngine function is called or not, but it does not give the actual function call and I cannot verify that whether the call to testFunc() has been made or not.

Is there any way to test this scenario? I am new to testing world and framework.

2条回答
Summer. ? 凉城
2楼-- · 2019-09-02 03:52

Having similar such issue, where I am bit lost in writing the test case

class UserCompanyRateLimitValidation:
    def __init__(self, user_public_key):
        self.adapter = UserAdapter(user_public_key)
        container = self.adapter.get_user_company_rate_limit()
        super(UserCompanyRateLimitValidation, self).__init__(container,\
                                            UserCompanyRateLimitValidation.TYPE)

I have to test this function. I have written test case something like this. I have tried to mock the UserAdapter class but I am not able to do so completely.

def test_case_1():
   self.user_public_key = 'TEST_USER_PUBLIC_KEY_XXXXXX1234567890XXXXX'
   UserAdapter_mock = mock(UserAdapter)
   when(UserAdapter_mock).get_user_company_rate_limit().\
                                          thenReturn(self.get_container_object())
   self.test_obj = UserCompanyRateLimitValidation(self.user_public_key)

Here if you see I have mocked get_user_company_rate_limit() call from the testable function, container = self.adapter.get_user_company_rate_limit() but I am still not able to figure out the way in which I can mock this call,

 self.adapter = UserAdapter(user_public_key)
查看更多
做自己的国王
3楼-- · 2019-09-02 03:57

In your example you are testing your mock.

  • You create a mock of ISightRequestingEngine
  • You call startEngine() method of that mock
  • You verify that the mocked object was called

What you want to do is:

  • Mock out testFunc()
  • Call startEngine()
  • Verify that testFunc() was called

I'm not familiar with mockito, but what from what I can make up from the documentation, I think you have to do something like the following:

from mockito import mock, verify

# Setup ---------------------------------------------   
my_mock = mock(ISightRequestingEngine)

system_under_test = ISightRequestingEngine()

system_under_test.testFunc = my_mock.testfunc # Mock out only testFunc()

# Exercise ------------------------------------------
system_under_test.startEngine()

# Verify --------------------------------------------
verify(my_mock).testFunc()
查看更多
登录 后发表回答