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

  • SEARCH
  • Home
  • 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 735669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:31:00+00:00 2026-05-14T07:31:00+00:00

I`d like to know how I could unit-test the following module. def download_distribution(url, tempdir):

  • 0

I`d like to know how I could unit-test the following module.

def download_distribution(url, tempdir):
    """ Method which downloads the distribution from PyPI """
    print "Attempting to download from %s" % (url,)

    try:
        url_handler = urllib2.urlopen(url)
        distribution_contents = url_handler.read()
        url_handler.close()

        filename = get_file_name(url)

        file_handler = open(os.path.join(tempdir, filename), "w")
        file_handler.write(distribution_contents)
        file_handler.close()
        return True

    except ValueError, IOError:
        return False
  • 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-14T07:31:01+00:00Added an answer on May 14, 2026 at 7:31 am

    Unit test propositioners will tell you that unit tests should be self contained, that is, they should not access the network or the filesystem (especially not in writing mode). Network and filesystem tests are beyond the scope of unit tests (though you might subject them to integration tests).

    Speaking generally, for such a case, I’d extract the urllib and file-writing codes to separate functions (which would not be unit-tested), and inject mock-functions during unit testing.

    I.e. (slightly abbreviated for better reading):

    def get_web_content(url):
        # Extracted code
        url_handler = urllib2.urlopen(url)
        content = url_handler.read()
        url_handler.close()
        return content
    
    def write_to_file(content, filename, tmpdir):
        # Extracted code
        file_handler = open(os.path.join(tempdir, filename), "w")
        file_handler.write(content)
        file_handler.close()
    
    def download_distribution(url, tempdir):
        # Original code, after extractions
        distribution_contents = get_web_content(url)
        filename = get_file_name(url)
        write_to_file(distribution_contents, filename, tmpdir)
        return True
    

    And, on the test file:

    import module_I_want_to_test
    
    def mock_web_content(url):
        return """Some fake content, useful for testing"""
    def mock_write_to_file(content, filename, tmpdir):
        # In this case, do nothing, as we don't do filesystem meddling while unit testing
        pass
    
    module_I_want_to_test.get_web_content = mock_web_content
    module_I_want_to_test.write_to_file = mock_write_to_file
    
    class SomeTests(unittest.Testcase):
        # And so on...
    

    And then I second Daniel’s suggestion, you should read some more in-depth material on unit testing.

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

Sidebar

Related Questions

I would like to know if an instance implements a specific method. I could
weird que i know, but could something like the following be achived somehow? int
I'd like to know what could be the best approach to develop a Windows
I would like to know if it could be possible to add a file
I would like to know how I could edit mp3 files on the server
I know I could code what I'm trying to achieve, but I'd like to
I would like to know if there is any way that I could build
Do you know any stand-alone client or web tool (like grc ) that could
I am writing some unit tests which I have simplified to the following: class
I have a unit test which depends on some code that uses MEF. When

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.