使用Python的补丁装饰器模拟Twitter的API库

时间:2016-05-04 23:00:50

标签: python unit-testing twitter mocking tweepy

我使用https://github.com/tweepy/tweepy lib在Python中进行Twiitter的API集成。我有tw.py个文件,包含一些Twitter的API用法和tests.py个文件以及一些单元测试。为什么我的测试:test_third没有通过?修补tweepy.API课程时我做错了什么?

# tw.py
import tweepy
import settings


def get_tweepy_api():
    """
    Constructs twitter api object with all required keys, tokens.

    @return: tweepy API object
    @rtype: tweepy.api.API
    """
    oauth = tweepy.auth.OAuthHandler(
        consumer_key=settings.TWITTER_CONSUMER_KEY_ENGINE,
        consumer_secret=settings.TWITTER_CONSUMER_SECRET_KEY_ENGINE,
        callback=None)
    oauth.set_access_token(settings.TWITTER_ACCESS_KEY_ENGINE,
        settings.TWITTER_ACCESS_SECRET_ENGINE)
    api = tweepy.API(oauth)
    return api


class TwitterAccount(object):

    @staticmethod
    def validate(user_id):
        try:
            api = get_tweepy_api()
            print('object api', api)
            tuser = api.get_user(user_id)
            print('method', api.get_user)
            print('tuser', tuser)
            if tuser.protected:
                return False
        except tweepy.TweepError:
            return False
        return True


# tests.py
import unittest
from unittest.mock import create_autospec, MagicMock, ANY, patch
import tweepy
from tw import TwitterAccount


class MockedUser(object):
    protected = False


class TwitterTestCase(unittest.TestCase):

    def test_first(self):
        api = create_autospec(tweepy.API, name='FirstAPI')
        api.get_user = MagicMock(return_value=MockedUser)
        tweepy.API = MagicMock(return_value=api)
        self.assertTrue(TwitterAccount.validate('123'))
        api.get_user.assert_called_once_with('123')

    @patch.object(tweepy.API, 'get_user', return_value=MockedUser)
    def test_second(self, tweepy_api):
        self.assertTrue(TwitterAccount.validate('123'))
        tweepy_api.assert_called_once_with(ANY)

    @patch('tw.tweepy.API', autospec=True)
    def test_third(self, tweepy_api):
        tweepy_api.get_user = MagicMock(return_value=MockedUser)
        self.assertTrue(TwitterAccount.validate('123'))

回溯:

kisiel@kisiel-X550LC:~/tw$ python -m unittest tests.TwitterTestCase
object api <MagicMock name='FirstAPI' spec='API' id='3042918028'>
method <MagicMock name='FirstAPI.get_user' id='3061190636'>
tuser <class 'tests.MockedUser'>
.object api <MagicMock name='FirstAPI' spec='API' id='3042918028'>
method <MagicMock name='FirstAPI.get_user' id='3061190636'>
tuser <class 'tests.MockedUser'>
Fobject api <MagicMock name='API()' id='3042271340'>
method <MagicMock name='API().get_user' id='3042284812'>
tuser <MagicMock name='API().get_user()' id='3042292332'>
F
======================================================================
FAIL: test_second (tests.TwitterTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/mock.py", line 1136, in patched
    return func(*args, **keywargs)
  File "/home/kisiel/tw/tests.py", line 24, in test_second
    tweepy_api.assert_called_once_with(ANY)
  File "/usr/lib/python3.4/unittest/mock.py", line 781, in assert_called_once_with
    raise AssertionError(msg)
AssertionError: Expected 'get_user' to be called once. Called 0 times.

======================================================================
FAIL: test_third (tests.TwitterTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/mock.py", line 1136, in patched
    return func(*args, **keywargs)
  File "/home/kisiel/tw/tests.py", line 29, in test_third
    self.assertTrue(TwitterAccount.validate('123'))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 3 tests in 0.028s

FAILED (failures=2)

0 个答案:

没有答案