[Tutor] urllib2 and tests

RJ Ewing ewing.rj at gmail.com
Sun May 5 05:27:05 CEST 2013


When I run the following test.py, I get the following error:

RROR: test_fetch_file (__main__.TestFileFetcher)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_filefetcher.py", line 12, in test_fetch_file
    fetched_file = filefetcher.fetch_file(URL)
  File "/Users/rjewing/Documents/Work/filefetcher.py", line 7, in fetch_file
    return urllib2.urlopen(url).read()
  File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",
line 126, in urlopen
    return _opener.open(url, data, timeout)
  File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",
line 392, in open
    protocol = req.get_type()
AttributeError: 'TestFileFetcher' object has no attribute 'get_type'

----------------------------------------------------------------------

Test.py:

class TestFileFetcher(unittest.TestCase):

    def test_fetch_file(URL):
        phrase = 'position = support-intern'

        fetched_file = filefetcher.fetch_file(URL)
        unittest.assertIsNone(fetched_file,
                              'The file was not fetched correctly')

        text = filefetcher.add_phrase(fetched_file)
        unittest.assertNotIn(phrase, text, 'The phrase is not in the file')


fetch_file():

def fetch_file(url):
    print 'Fetching file from %s' % url
    return urllib2.urlopen(url).read()


If I run the fetch_file function outside of the test, it works fine. Any
ideas?
Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130504/998aff5c/attachment.html>


More information about the Tutor mailing list