1

I have made a function that connects to a twitter api. This function returns an twitter object. I want to create a testing function that checks if the returned object is really a twitter object. So this is my function:

def authenticate_twitter_api():
    """Make connection with twitters REST api"""
    try:
        logger.info('Starting Twitter Authentication')
        twitter_api = twitter.Twitter(auth=twitter.OAuth(config.TWITTER_ACCESS_KEY, config.TWITTER_ACCESS_SECRET,
                                                     config.TWITTER_CONSUMER_KEY, config.TWITTER_CONSUMER_SECRET))
        print twitter_api
        logger.info("Service has started")
        return twitter_api
    except:
        logger.error("Authentication Error. Could not connect to twitter api service")

When i run this function it returns:

<twitter.api.Twitter object at 0x7fc751783910>

Now, i want to create a testing function, maybe through numpy.testing in order to check if the type is a object.

numpy.testing.assert_equal(actual, desired, err_msg='')

actual = type(authenticate_twitter_api())
desired =<class 'twitter.api.Twitter'>

And here is the problem. I can't save an object to 'desired'.

What can i do ?

2 Answers 2

2

The desired object you are looking for is twitter.api.Twitter, just import it and pass the class the assert_equal.

However, it's more idiomatic to use isinstance:

from twitter.api import Twitter

if isinstance(authenticate_twitter_api(), Twitter):
    print("It was a Twitter object.")
Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfectly. Thanks!
2

classes are objects themselves in Python. So you can assign your desired variable like this:

import twitter
# (...)
desired = twitter.api.Twitter

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.