Mocking Googleads for Unit Tests

徘徊边缘 提交于 2019-12-06 09:47:59

It is usually really easy to mock stuff in python with mock library. I do not guarantee this is the best way to test this code. Refactoring it can lead to more robust and easier to test code.

import unittest
import mock


class TestCaseName(unittest.TestCase):

    @mock.patch('path_to_module.AdWordsClient', autospec=True)
    @mock.patch('path_to_module.GoogleRefreshTokenClient', autospec=True)
    @mock.patch('path_to_module.tz', autospec=True)
    def test_get_account_timezone(self, tz_mock, adwords_client_mock, grefresh_token_client_mock):
        adwards_client_instance = mock.Mock()
        adwords_client_mock.return_value = test_get_account_timezone
        instance = AdWords(...)
        instance.get_account_timezone()
        adwards_client_instance.GetService.assert_called_with(...)

You should check the documentation for exact methods of the mock library.

Alternative to mock is https://pypi.python.org/pypi/doubles/1.1.3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!