Best Python code snippet using selene_python
test_urllibnet.py
Source: test_urllibnet.py
1#!/โusr/โbin/โenv python2import unittest3from test import test_support4import socket5import urllib6import sys7import os8import mimetools9def _open_with_retry(func, host, *args, **kwargs):10 # Connecting to remote hosts is flaky. Make it more robust11 # by retrying the connection several times.12 for i in range(3):13 try:14 return func(host, *args, **kwargs)15 except IOError, last_exc:16 continue17 except:18 raise19 raise last_exc20class URLTimeoutTest(unittest.TestCase):21 TIMEOUT = 10.022 def setUp(self):23 socket.setdefaulttimeout(self.TIMEOUT)24 def tearDown(self):25 socket.setdefaulttimeout(None)26 def testURLread(self):27 f = _open_with_retry(urllib.urlopen, "http:/โ/โwww.python.org/โ")28 x = f.read()29class urlopenNetworkTests(unittest.TestCase):30 """Tests urllib.urlopen using the network.31 These tests are not exhaustive. Assuming that testing using files does a32 good job overall of some of the basic interface features. There are no33 tests exercising the optional 'data' and 'proxies' arguments. No tests34 for transparent redirection have been written.35 setUp is not used for always constructing a connection to36 http:/โ/โwww.python.org/โ since there a few tests that don't use that address37 and making a connection is expensive enough to warrant minimizing unneeded38 connections.39 """40 def urlopen(self, *args):41 return _open_with_retry(urllib.urlopen, *args)42 def test_basic(self):43 # Simple test expected to pass.44 open_url = self.urlopen("http:/โ/โwww.python.org/โ")45 for attr in ("read", "readline", "readlines", "fileno", "close",46 "info", "geturl"):47 self.assert_(hasattr(open_url, attr), "object returned from "48 "urlopen lacks the %s attribute" % attr)49 try:50 self.assert_(open_url.read(), "calling 'read' failed")51 finally:52 open_url.close()53 def test_readlines(self):54 # Test both readline and readlines.55 open_url = self.urlopen("http:/โ/โwww.python.org/โ")56 try:57 self.assert_(isinstance(open_url.readline(), basestring),58 "readline did not return a string")59 self.assert_(isinstance(open_url.readlines(), list),60 "readlines did not return a list")61 finally:62 open_url.close()63 def test_info(self):64 # Test 'info'.65 open_url = self.urlopen("http:/โ/โwww.python.org/โ")66 try:67 info_obj = open_url.info()68 finally:69 open_url.close()70 self.assert_(isinstance(info_obj, mimetools.Message),71 "object returned by 'info' is not an instance of "72 "mimetools.Message")73 self.assertEqual(info_obj.getsubtype(), "html")74 def test_geturl(self):75 # Make sure same URL as opened is returned by geturl.76 URL = "http:/โ/โwww.python.org/โ"77 open_url = self.urlopen(URL)78 try:79 gotten_url = open_url.geturl()80 finally:81 open_url.close()82 self.assertEqual(gotten_url, URL)83 def test_getcode(self):84 # test getcode() with the fancy opener to get 404 error codes85 URL = "http:/โ/โwww.python.org/โXXXinvalidXXX"86 open_url = urllib.FancyURLopener().open(URL)87 try:88 code = open_url.getcode()89 finally:90 open_url.close()91 self.assertEqual(code, 404)92 def test_fileno(self):93 if (sys.platform in ('win32',) or94 not hasattr(os, 'fdopen')):95 # On Windows, socket handles are not file descriptors; this96 # test can't pass on Windows.97 return98 # Make sure fd returned by fileno is valid.99 open_url = self.urlopen("http:/โ/โwww.python.org/โ")100 fd = open_url.fileno()101 FILE = os.fdopen(fd)102 try:103 self.assert_(FILE.read(), "reading from file created using fd "104 "returned by fileno failed")105 finally:106 FILE.close()107 def test_bad_address(self):108 # Make sure proper exception is raised when connecting to a bogus109 # address.110 self.assertRaises(IOError,111 # SF patch 809915: In Sep 2003, VeriSign started112 # highjacking invalid .com and .net addresses to113 # boost traffic to their own site. This test114 # started failing then. One hopes the .invalid115 # domain will be spared to serve its defined116 # purpose.117 # urllib.urlopen, "http:/โ/โwww.sadflkjsasadf.com/โ")118 urllib.urlopen, "http:/โ/โsadflkjsasf.i.nvali.d/โ")119class urlretrieveNetworkTests(unittest.TestCase):120 """Tests urllib.urlretrieve using the network."""121 def urlretrieve(self, *args):122 return _open_with_retry(urllib.urlretrieve, *args)123 def test_basic(self):124 # Test basic functionality.125 file_location,info = self.urlretrieve("http:/โ/โwww.python.org/โ")126 self.assert_(os.path.exists(file_location), "file location returned by"127 " urlretrieve is not a valid path")128 FILE = file(file_location)129 try:130 self.assert_(FILE.read(), "reading from the file location returned"131 " by urlretrieve failed")132 finally:133 FILE.close()134 os.unlink(file_location)135 def test_specified_path(self):136 # Make sure that specifying the location of the file to write to works.137 file_location,info = self.urlretrieve("http:/โ/โwww.python.org/โ",138 test_support.TESTFN)139 self.assertEqual(file_location, test_support.TESTFN)140 self.assert_(os.path.exists(file_location))141 FILE = file(file_location)142 try:143 self.assert_(FILE.read(), "reading from temporary file failed")144 finally:145 FILE.close()146 os.unlink(file_location)147 def test_header(self):148 # Make sure header returned as 2nd value from urlretrieve is good.149 file_location, header = self.urlretrieve("http:/โ/โwww.python.org/โ")150 os.unlink(file_location)151 self.assert_(isinstance(header, mimetools.Message),152 "header is not an instance of mimetools.Message")153def test_main():154 test_support.requires('network')155 from warnings import filterwarnings, catch_warnings156 with catch_warnings():157 filterwarnings('ignore', '.*urllib\.urlopen.*Python 3.0',158 DeprecationWarning)159 test_support.run_unittest(URLTimeoutTest,160 urlopenNetworkTests,161 urlretrieveNetworkTests)162if __name__ == "__main__":...
Check out the latest blogs from LambdaTest on this topic:
There are times when developers get stuck with a problem that has to do with version changes. Trying to run the code or test without upgrading the package can result in unexpected errors.
Automating testing is a crucial step in the development pipeline of a software product. In an agile development environment, where there is continuous development, deployment, and maintenance of software products, automation testing ensures that the end software products delivered are error-free.
Web applications continue to evolve at an unbelievable pace, and the architecture surrounding web apps get more complicated all of the time. With the growth in complexity of the web application and the development process, web application testing also needs to keep pace with the ever-changing demands.
โTest frequently and early.โ If youโve been following my testing agenda, youโre probably sick of hearing me repeat that. However, it is making sense that if your tests detect an issue soon after it occurs, it will be easier to resolve. This is one of the guiding concepts that makes continuous integration such an effective method. Iโve encountered several teams who have a lot of automated tests but donโt use them as part of a continuous integration approach. There are frequently various reasons why the team believes these tests cannot be used with continuous integration. Perhaps the tests take too long to run, or they are not dependable enough to provide correct results on their own, necessitating human interpretation.
Hey LambdaTesters! Weโve got something special for you this week. ????
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!