How do I unit test a module that relies on urllib2?

前端 未结 7 1219
孤街浪徒
孤街浪徒 2020-12-08 05:16

I\'ve got a piece of code that I can\'t figure out how to unit test! The module pulls content from external XML feeds (twitter, flickr, youtube, etc.) with urllib2. Here\'s

相关标签:
7条回答
  • 2020-12-08 05:40

    Why not just mock a website that returns the response you expect? then start the server in a thread in setup and kill it in the teardown. I ended up doing this for testing code that would send email by mocking an smtp server and it works great. Surely something more trivial could be done for http...

    from smtpd import SMTPServer
    from time import sleep
    import asyncore
    SMTP_PORT = 6544
    
    class MockSMTPServer(SMTPServer):
        def __init__(self, localaddr, remoteaddr, cb = None):
            self.cb = cb
            SMTPServer.__init__(self, localaddr, remoteaddr)
    
        def process_message(self, peer, mailfrom, rcpttos, data):
            print (peer, mailfrom, rcpttos, data)
            if self.cb:
                self.cb(peer, mailfrom, rcpttos, data)
            self.close()
    
    def start_smtp(cb, port=SMTP_PORT):
    
        def smtp_thread():
            _smtp = MockSMTPServer(("127.0.0.1", port), (None, 0), cb)
            asyncore.loop()
            return Thread(None, smtp_thread)
    
    
    def test_stuff():
            #.......snip noise
            email_result = None
    
            def email_back(*args):
                email_result = args
    
            t = start_smtp(email_back)
            t.start()
            sleep(1)
    
            res.form["email"]= self.admin_email
            res = res.form.submit()
            assert res.status_int == 302,"should've redirected"
    
    
            sleep(1)
            assert email_result is not None, "didn't get an email"
    
    0 讨论(0)
提交回复
热议问题