Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6126b7c

Browse files
[3.13]gh-71339: Use new assertion methods in the urllib tests (GH-129056) (GH-132499)
(cherry picked from commitf98b9b4)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent34d3495 commit6126b7c

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
lines changed

‎Lib/test/test_urllib.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
fromtest.supportimportos_helper
1313
fromtest.supportimportsocket_helper
1414
fromtest.supportimportwarnings_helper
15+
fromtest.support.testcaseimportExtraAssertions
1516
importos
1617
try:
1718
importssl
@@ -139,7 +140,7 @@ def unfakeftp(self):
139140
urllib.request.ftpwrapper=self._ftpwrapper_class
140141

141142

142-
classurlopen_FileTests(unittest.TestCase):
143+
classurlopen_FileTests(unittest.TestCase,ExtraAssertions):
143144
"""Test urlopen() opening a temporary file.
144145
145146
Try to test as much functionality as possible so as to cut down on reliance
@@ -169,9 +170,7 @@ def test_interface(self):
169170
# Make sure object returned by urlopen() has the specified methods
170171
forattrin ("read","readline","readlines","fileno",
171172
"close","info","geturl","getcode","__iter__"):
172-
self.assertTrue(hasattr(self.returned_obj,attr),
173-
"object returned by urlopen() lacks %s attribute"%
174-
attr)
173+
self.assertHasAttr(self.returned_obj,attr)
175174

176175
deftest_read(self):
177176
self.assertEqual(self.text,self.returned_obj.read())
@@ -601,7 +600,7 @@ def test_URLopener_deprecation(self):
601600
urllib.request.URLopener()
602601

603602

604-
classurlopen_DataTests(unittest.TestCase):
603+
classurlopen_DataTests(unittest.TestCase,ExtraAssertions):
605604
"""Test urlopen() opening a data URL."""
606605

607606
defsetUp(self):
@@ -640,9 +639,7 @@ def test_interface(self):
640639
# Make sure object returned by urlopen() has the specified methods
641640
forattrin ("read","readline","readlines",
642641
"close","info","geturl","getcode","__iter__"):
643-
self.assertTrue(hasattr(self.text_url_resp,attr),
644-
"object returned by urlopen() lacks %s attribute"%
645-
attr)
642+
self.assertHasAttr(self.text_url_resp,attr)
646643

647644
deftest_info(self):
648645
self.assertIsInstance(self.text_url_resp.info(),email.message.Message)

‎Lib/test/test_urllib2.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
fromtest.supportimportos_helper
44
fromtest.supportimportrequires_subprocess
55
fromtest.supportimportwarnings_helper
6+
fromtest.support.testcaseimportExtraAssertions
67
fromtestimporttest_urllib
78
fromunittestimportmock
89

@@ -724,7 +725,7 @@ def sanepathname2url(path):
724725
returnurlpath
725726

726727

727-
classHandlerTests(unittest.TestCase):
728+
classHandlerTests(unittest.TestCase,ExtraAssertions):
728729

729730
deftest_ftp(self):
730731
classMockFTPWrapper:
@@ -1179,15 +1180,15 @@ def test_errors(self):
11791180
r=MockResponse(200,"OK", {},"",url)
11801181
newr=h.http_response(req,r)
11811182
self.assertIs(r,newr)
1182-
self.assertFalse(hasattr(o,"proto"))# o.error not called
1183+
self.assertNotHasAttr(o,"proto")# o.error not called
11831184
r=MockResponse(202,"Accepted", {},"",url)
11841185
newr=h.http_response(req,r)
11851186
self.assertIs(r,newr)
1186-
self.assertFalse(hasattr(o,"proto"))# o.error not called
1187+
self.assertNotHasAttr(o,"proto")# o.error not called
11871188
r=MockResponse(206,"Partial content", {},"",url)
11881189
newr=h.http_response(req,r)
11891190
self.assertIs(r,newr)
1190-
self.assertFalse(hasattr(o,"proto"))# o.error not called
1191+
self.assertNotHasAttr(o,"proto")# o.error not called
11911192
# anything else calls o.error (and MockOpener returns None, here)
11921193
r=MockResponse(502,"Bad gateway", {},"",url)
11931194
self.assertIsNone(h.http_response(req,r))
@@ -1402,7 +1403,7 @@ def http_open(self, req):
14021403
response=opener.open('http://example.com/')
14031404
expected=b'GET '+result+b' '
14041405
request=handler.last_buf
1405-
self.assertTrue(request.startswith(expected),repr(request))
1406+
self.assertStartsWith(request,expected)
14061407

14071408
deftest_redirect_head_request(self):
14081409
from_url="http://example.com/a.html"
@@ -1833,7 +1834,7 @@ def test_invalid_closed(self):
18331834
self.assertTrue(conn.fakesock.closed,"Connection not closed")
18341835

18351836

1836-
classMiscTests(unittest.TestCase):
1837+
classMiscTests(unittest.TestCase,ExtraAssertions):
18371838

18381839
defopener_has_handler(self,opener,handler_class):
18391840
self.assertTrue(any(h.__class__==handler_class
@@ -1892,9 +1893,9 @@ def test_HTTPError_interface(self):
18921893
url=code=fp=None
18931894
hdrs='Content-Length: 42'
18941895
err=urllib.error.HTTPError(url,code,msg,hdrs,fp)
1895-
self.assertTrue(hasattr(err,'reason'))
1896+
self.assertHasAttr(err,'reason')
18961897
self.assertEqual(err.reason,'something bad happened')
1897-
self.assertTrue(hasattr(err,'headers'))
1898+
self.assertHasAttr(err,'headers')
18981899
self.assertEqual(err.headers,'Content-Length: 42')
18991900
expected_errmsg='HTTP Error %s: %s'% (err.code,err.msg)
19001901
self.assertEqual(str(err),expected_errmsg)

‎Lib/test/test_urllib2_localnet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
fromtestimportsupport
1212
fromtest.supportimporthashlib_helper
1313
fromtest.supportimportthreading_helper
14+
fromtest.support.testcaseimportExtraAssertions
1415

1516
try:
1617
importssl
@@ -442,7 +443,7 @@ def log_message(self, *args):
442443
returnFakeHTTPRequestHandler
443444

444445

445-
classTestUrlopen(unittest.TestCase):
446+
classTestUrlopen(unittest.TestCase,ExtraAssertions):
446447
"""Tests urllib.request.urlopen using the network.
447448
448449
These tests are not exhaustive. Assuming that testing using files does a
@@ -606,8 +607,7 @@ def test_basic(self):
606607
handler=self.start_server()
607608
withurllib.request.urlopen("http://localhost:%s"%handler.port)asopen_url:
608609
forattrin ("read","close","info","geturl"):
609-
self.assertTrue(hasattr(open_url,attr),"object returned from "
610-
"urlopen lacks the %s attribute"%attr)
610+
self.assertHasAttr(open_url,attr)
611611
self.assertTrue(open_url.read(),"calling 'read' failed")
612612

613613
deftest_info(self):

‎Lib/test/test_urllibnet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
fromtestimportsupport
33
fromtest.supportimportos_helper
44
fromtest.supportimportsocket_helper
5+
fromtest.support.testcaseimportExtraAssertions
56

67
importcontextlib
78
importsocket
@@ -34,7 +35,7 @@ def testURLread(self):
3435
f.read()
3536

3637

37-
classurlopenNetworkTests(unittest.TestCase):
38+
classurlopenNetworkTests(unittest.TestCase,ExtraAssertions):
3839
"""Tests urllib.request.urlopen using the network.
3940
4041
These tests are not exhaustive. Assuming that testing using files does a
@@ -70,8 +71,7 @@ def test_basic(self):
7071
withself.urlopen(self.url)asopen_url:
7172
forattrin ("read","readline","readlines","fileno","close",
7273
"info","geturl"):
73-
self.assertTrue(hasattr(open_url,attr),"object returned from "
74-
"urlopen lacks the %s attribute"%attr)
74+
self.assertHasAttr(open_url,attr)
7575
self.assertTrue(open_url.read(),"calling 'read' failed")
7676

7777
deftest_readlines(self):

‎Lib/test/test_urlparse.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
importunicodedata
33
importunittest
44
importurllib.parse
5+
fromtest.support.testcaseimportExtraAssertions
56

67
RFC1808_BASE="http://a/b/c/d;p?q#f"
78
RFC2396_BASE="http://a/b/c/d;p?q"
@@ -101,7 +102,7 @@
101102
(b"%81=%A9", {b'\x81': [b'\xa9']}),
102103
]
103104

104-
classUrlParseTestCase(unittest.TestCase):
105+
classUrlParseTestCase(unittest.TestCase,ExtraAssertions):
105106

106107
defcheckRoundtrips(self,url,parsed,split,url2=None):
107108
ifurl2isNone:
@@ -1033,14 +1034,13 @@ def test_parse_fragments(self):
10331034
withself.subTest(url=url,function=func):
10341035
result=func(url,allow_fragments=False)
10351036
self.assertEqual(result.fragment,"")
1036-
self.assertTrue(
1037-
getattr(result,attr).endswith("#"+expected_frag))
1037+
self.assertEndsWith(getattr(result,attr),
1038+
"#"+expected_frag)
10381039
self.assertEqual(func(url,"",False).fragment,"")
10391040

10401041
result=func(url,allow_fragments=True)
10411042
self.assertEqual(result.fragment,expected_frag)
1042-
self.assertFalse(
1043-
getattr(result,attr).endswith(expected_frag))
1043+
self.assertNotEndsWith(getattr(result,attr),expected_frag)
10441044
self.assertEqual(func(url,"",True).fragment,
10451045
expected_frag)
10461046
self.assertEqual(func(url).fragment,expected_frag)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp