Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:30:32+00:00 2026-05-23T19:30:32+00:00

I am getting into testing in python and I asked myself how to test

  • 0

I am getting into testing in python and I asked myself how to test this method.

def get_response(self, url, params):
    encoded_params = urllib.urlencode(params)
    request = urllib2.Request(BASE_URL, headers=HEADERS)
    response = urllib2.urlopen(request, encoded_params)
    return response

Using doctest or unittest, how is this best achieved? I thought of passing get_response() a test url and some test parameters, which exists in real world and to check if response.read() returns the expected data. But somehow I feel, this is not the way it should be done. Any suggestions? I would appreciate suggestions how to handle such cases in context of texting.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:30:34+00:00Added an answer on May 23, 2026 at 7:30 pm

    This is a good opportunity to use a mock testing framework, such as minimock.

    BASE_URL='http://google.com'
    HEADERS = {"accept-language":"klingon"}
    import urllib, urllib2
    def get_response(self, url, params):
        r"""
        >>> from minimock import Mock
        >>> urllib2.Request = Mock('urllib2.Request')
        >>> urllib2.urlopen = Mock('urllib2.urlopen')
        >>> get_response(None, 'http://google.com', {'foo':'bar'})
        Called urllib2.Request(
            'http://google.com',
            headers={'accept-language': 'klingon'})
        Called urllib2.urlopen(None, 'foo=bar')
        >>> 
        """
        encoded_params = urllib.urlencode(params)
        request = urllib2.Request(BASE_URL, headers=HEADERS)
        response = urllib2.urlopen(request, encoded_params)
        return response
    

    Do note that the unit is embedded in the docstring for the function under test, in doctest format.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just getting into unit testing, and have written some short tests to check
I'm new to Unit Testing, and I'm only getting into the routine of building
I'm getting into ASP.NET (C# - I know it doesn't matter for this particular
I'm using Watir to do some automated testing for a website. This particular test
Just getting into Unit Testing with C++. It looks like I will need to
I have been getting into Unit Testing with Zend Framework. I am getting used
I'm trying to use the persistence specification testing built into FNH. The CheckList method
Just getting into SQL stored queries right now... anyway, here's my database schema (simplified
Thinking about getting into .net technology project management I've had plenty of experience with
I'm getting into more of a TDD workflow, and have a mix of MVC

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.