python mocking third party modules

送分小仙女□ 提交于 2019-12-08 08:39:01

问题


im trying to test some classes that process tweets. Im using sixohsix twitter to deal with Twitter API. I have a class that acts as a facade for the Twitter classes, and my idea was to mock the actual sixohsix classes to simulate the arrival of tweets, by randomly generate new tweets or retrieving them from a database.

My facade looks something like:

from twitter import TwitterStream


class TwitterFacade(object):
    def __init__(self, dev='soom'):
        self._auth = OAuth(dev_keys["ACCESS_TOKEN"], dev_keys["ACCESS_SECRET"],   dev_keys["CONSUMER_KEY"], dev_keys["CONSUMER_SECRET"])

    def tweets(self, callback=None, users=[], terms=[], locations=[], count=5):
        t = TwitterStream(auth=self._auth)

        args = {}
        if users:     args['follow']    = ",".join(users)
        if terms:     args['track']     = ",".join(terms)
        if locations: args['locations'] = ",".join(str(l) for l in locations)

        # this controls the general loop, it re-enters if there was an exception,
        # otherwise the for loop should take care of looping trough the tweets
        cant = count
        while cant > 0:
            try:
                iterator = t.statuses.filter(**args)
                for twit in iterator:
                    if twit.get('text'):
                        callback(twit)
                        cant -= 1
                        if cant == 0:
                            iterator.close()
                            break
            except Exception as e:
                print e
                #some error handling code

So if in an unittest i want to test some module that does something with the tweets, how would i mock the TwitterStream class ? I've tried using Mock:

from mock import patch
from twitter_facade import TwitterFacade


class TwitterStreamProxy(object):
    def __init__(self):
        pass
        #some code for dealing with statuses.filter(..)


@patch('twitter.TwitterStream', TwitterStreamProxy())
def test_filter_on_tweets():
    facade = TwitterFacade()
    facade.tweets(somemethod, [], ['term1', 'term2'], [], count=50)

def somemethod(tweet):
    #some logic in here

This is not working, twitter api is still been called. I would have expected that sincen i didnt add any code to the mock class i would have gotten an error or something, but sixohsix twitter classes where called instead.


回答1:


You need to patch the local object; your module has a reference to the TwitterStream object, patch that:

@patch('yourmodule.TwitterStream', TwitterStreamProxy())

See the mock Where to Patch documentation.



来源:https://stackoverflow.com/questions/23112199/python-mocking-third-party-modules

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