Skip to content

Commit 648ea79

Browse files
author
Ed Schofield
committed
Swap out Py2.7-specific unittest uses with py.test
1 parent c9a85ac commit 648ea79

11 files changed

Lines changed: 53 additions & 42 deletions

future/tests/test_builtins.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
from subprocess import Popen, PIPE
1111
from numbers import Integral
1212
import unittest
13+
import pytest
1314

1415

1516
class TestBuiltins(unittest.TestCase):
16-
@unittest.expectedFailure
17+
@pytest.mark.xfail
1718
def test_isinstance_int(self):
1819
"""
1920
Redefining ``int`` to ``long`` on Py2 would make this
@@ -27,7 +28,7 @@ def test_isinstance_Integral(self):
2728
"""
2829
self.assertTrue(isinstance(0, Integral))
2930

30-
@unittest.expectedFailure # Py2's long doesn't inherit from int!
31+
@pytest.mark.xfail # Py2's long doesn't inherit from int!
3132
def test_long(self):
3233
self.assertEqual(isinstance(10**100, int))
3334
if not PY3:
@@ -55,7 +56,7 @@ def test_round(self):
5556
self.assertTrue(isinstance(round(123.5, 0), float))
5657
self.assertTrue(isinstance(round(123.5), Integral))
5758

58-
@unittest.skip('negative ndigits not implemented yet')
59+
@pytest.mark.xfail('negative ndigits not implemented yet')
5960
def test_round_negative_ndigits(self):
6061
self.assertEqual(round(10.1350, 0), 10.0)
6162
self.assertEqual(round(10.1350, -1), 10.0)

future/tests/test_bytes.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from numbers import Integral
1111
import unittest
12+
import pytest
1213

1314
TEST_UNICODE_STR = u'ℝεα∂@ßʟ℮ ☂ℯṧт υηḯ¢☺ḓ℮'
1415
# Tk icon as a .gif:
@@ -45,7 +46,7 @@ def test_bytes_int(self):
4546
with self.assertRaises(ValueError):
4647
bytes(int(-1))
4748

48-
@unittest.skipIf(utils.PY3, 'test not needed on Py3: all ints are long')
49+
@pytest.mark.skipif(utils.PY3, 'test not needed on Py3: all ints are long')
4950
def test_bytes_long(self):
5051
"""
5152
As above, but explicitly feeding in a long on Py2. Note that
@@ -88,7 +89,7 @@ def test_bytes_fromhex(self):
8889
def test_isinstance_bytes(self):
8990
self.assertTrue(isinstance(bytes(b'blah'), bytes))
9091

91-
@unittest.expectedFailure
92+
@pytest.mark.xfail
9293
def test_isinstance_oldbytestrings_bytes(self):
9394
"""
9495
Watch out for this. Byte-strings produced in various places in Py2
@@ -105,7 +106,7 @@ def test_bytes_getitem(self):
105106
self.assertEqual(b[0:1], b'A')
106107
self.assertEqual(b[:], b'ABCD')
107108

108-
@unittest.expectedFailure
109+
@pytest.mark.xfail
109110
def test_b_literal_creates_newbytes_object(self):
110111
"""
111112
It would nice if the b'' literal syntax could be coaxed into producing
@@ -326,7 +327,7 @@ def test_hash(self):
326327
self.assertEqual(len(d), 2)
327328
self.assertEqual(set(d.keys()), set([s, b]))
328329

329-
@unittest.expectedFailure
330+
@pytest.mark.xfail
330331
def test_hash_with_native_types(self):
331332
# Warning: initializing the dict with native Py2 types throws the
332333
# hashing out:

future/tests/test_decorators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from future.utils import implements_iterator, python_2_unicode_compatible
1010

1111
import unittest
12+
import pytest
1213

1314

1415
class TestDecorators(unittest.TestCase):
@@ -25,7 +26,7 @@ def __str__(self):
2526
assert hasattr(a, '__unicode__')
2627
self.assertEqual(str(a), my_unicode_str)
2728

28-
@unittest.expectedFailure
29+
@pytest.mark.xfail
2930
def test_str_encode_returns_bytes(self):
3031
" The following fails but should ideally pass: "
3132
self.assertTrue(isinstance(str(a).encode('utf-8'), bytes))

future/tests/test_futurize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_renamed_modules(self):
6868
'''
6969
self.simple_convert_and_run(code)
7070

71-
@unittest.skip('not implemented yet')
71+
@pytest.mark.skip('not implemented yet')
7272
def test_download_pypi_package_and_test(self, package_name='future'):
7373
URL = 'http://pypi.python.org/pypi/{}/json'
7474

future/tests/test_httplib.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import errno
1919

2020
import unittest
21+
import pytest
2122
TestCase = unittest.TestCase
2223

2324
from test import support
@@ -410,8 +411,8 @@ def test_filenoattr(self):
410411

411412
# Test lines overflowing the max line size (_MAXLINE in http.client)
412413

414+
pytest.mark.skip('disabled for HTTP 0.9 support')
413415
def test_overflowing_status_line(self):
414-
self.skipTest("disabled for HTTP 0.9 support")
415416
body = "HTTP/1.1 200 Ok" + "k" * 65536 + "\r\n"
416417
resp = client.HTTPResponse(FakeSocket(body))
417418
self.assertRaises((client.LineTooLong, client.BadStatusLine), resp.begin)
@@ -471,8 +472,8 @@ def testHTTPConnectionSourceAddress(self):
471472
self.conn.connect()
472473
self.assertEqual(self.conn.sock.getsockname()[1], self.source_port)
473474

474-
@unittest.skipIf(not hasattr(client, 'HTTPSConnection'),
475-
'http.client.HTTPSConnection not defined')
475+
@pytest.mark.skipif(not hasattr(client, 'HTTPSConnection'),
476+
'http.client.HTTPSConnection not defined')
476477
def testHTTPSConnectionSourceAddress(self):
477478
self.conn = client.HTTPSConnection(HOST, self.port,
478479
source_address=('', self.source_port))
@@ -535,7 +536,7 @@ def test_attributes(self):
535536
h = client.HTTPSConnection(HOST, TimeoutTest.PORT, timeout=30)
536537
self.assertEqual(h.timeout, 30)
537538

538-
@unittest.skipIf(not hasattr(client, 'HTTPSConnection'), 'http.client.HTTPSConnection not available')
539+
@pytest.mark.skipif(not hasattr(client, 'HTTPSConnection'), 'http.client.HTTPSConnection not available')
539540
def test_host_port(self):
540541
# Check invalid host_port
541542

future/tests/test_httpservers.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
from test import support
3535
threading = support.import_module('threading')
3636

37+
import pytest
38+
39+
3740
TRAVIS_MSG = 'These tests sporadically fail on travis-ci for some reason. (Threading?)'
3841

3942
class NoLogRequestHandler(object):
@@ -65,7 +68,7 @@ def stop(self):
6568
self.server.shutdown()
6669

6770

68-
@unittest.skipIf('/home/travis' in __file__, TRAVIS_MSG)
71+
@pytest.mark.skipif('/home/travis' in __file__, TRAVIS_MSG)
6972
class BaseTestCase(unittest.TestCase):
7073
def setUp(self):
7174
self._threads = support.threading_setup()
@@ -87,7 +90,7 @@ def request(self, uri, method='GET', body=None, headers={}):
8790
return self.connection.getresponse()
8891

8992

90-
@unittest.skipIf('/home/travis' in __file__, TRAVIS_MSG)
93+
@pytest.mark.skipif('/home/travis' in __file__, TRAVIS_MSG)
9194
class BaseHTTPServerTestCase(BaseTestCase):
9295
class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler):
9396
protocol_version = 'HTTP/1.1'
@@ -219,7 +222,7 @@ def test_return_custom_status(self):
219222
res = self.con.getresponse()
220223
self.assertEqual(res.status, 999)
221224

222-
@unittest.skip('Unicode bug in Py2.7 email.parser.parsestr ?')
225+
@pytest.mark.skip('Unicode bug in Py2.7 email.parser.parsestr ?')
223226
def test_latin1_header(self):
224227
self.con.request('LATINONEHEADER', '/', headers={
225228
'X-Special-Incoming': 'Ärger mit Unicode'
@@ -229,7 +232,7 @@ def test_latin1_header(self):
229232
self.assertEqual(res.read(), 'Ärger mit Unicode'.encode('utf-8'))
230233

231234

232-
@unittest.skipIf('/home/travis' in __file__, TRAVIS_MSG)
235+
@pytest.mark.skipif('/home/travis' in __file__, TRAVIS_MSG)
233236
class SimpleHTTPServerTestCase(BaseTestCase):
234237
class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler):
235238
pass
@@ -259,7 +262,7 @@ def check_status_and_reason(self, response, status, data=None):
259262
body = response.read()
260263
self.assertTrue(response)
261264
self.assertEqual(response.status, status)
262-
self.assertIsNotNone(response.reason)
265+
self.assertFalse(response.reason is None)
263266
if data:
264267
self.assertEqual(data, body)
265268

@@ -326,8 +329,8 @@ def test_invalid_requests(self):
326329
"""
327330

328331

329-
@unittest.skipIf('/home/travis' in __file__, TRAVIS_MSG)
330-
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
332+
@pytest.mark.skipif('/home/travis' in __file__, TRAVIS_MSG)
333+
@pytest.mark.skipif(hasattr(os, 'geteuid') and os.geteuid() == 0,
331334
"This test can't be run reliably as root (issue #13308).")
332335
class CGIHTTPServerTestCase(BaseTestCase):
333336
class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler):
@@ -429,13 +432,13 @@ def test_url_collapse_path(self):
429432
msg='path = %r\nGot: %r\nWanted: %r' %
430433
(path, actual, expected))
431434

432-
@unittest.expectedFailure
435+
@pytest.mark.xfail
433436
def test_headers_and_content(self):
434437
res = self.request('/cgi-bin/file1.py')
435438
self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
436439
(res.read(), res.getheader('Content-type'), res.status))
437440

438-
@unittest.expectedFailure
441+
@pytest.mark.xfail
439442
def test_post(self):
440443
# Was: params = urllib.parse.urlencode(
441444
params = urllib.urlencode(
@@ -450,22 +453,22 @@ def test_invaliduri(self):
450453
res.read()
451454
self.assertEqual(res.status, 404)
452455

453-
@unittest.expectedFailure
456+
@pytest.mark.xfail
454457
def test_authorization(self):
455458
headers = {bytes(b'Authorization') : bytes(b'Basic ') +
456459
base64.b64encode(bytes(b'username:pass'))}
457460
res = self.request('/cgi-bin/file1.py', 'GET', headers=headers)
458461
self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
459462
(res.read(), res.getheader('Content-type'), res.status))
460463

461-
@unittest.expectedFailure
464+
@pytest.mark.xfail
462465
def test_no_leading_slash(self):
463466
# http://bugs.python.org/issue2254
464467
res = self.request('cgi-bin/file1.py')
465468
self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
466469
(res.read(), res.getheader('Content-type'), res.status))
467470

468-
@unittest.expectedFailure
471+
@pytest.mark.xfail
469472
def test_os_environ_is_not_altered(self):
470473
signature = "Test CGI Server"
471474
os.environ['SERVER_SOFTWARE'] = signature
@@ -475,7 +478,7 @@ def test_os_environ_is_not_altered(self):
475478
self.assertEqual(os.environ['SERVER_SOFTWARE'], signature)
476479

477480

478-
@unittest.skipIf('/home/travis' in __file__, TRAVIS_MSG)
481+
@pytest.mark.skipif('/home/travis' in __file__, TRAVIS_MSG)
479482
class SocketlessRequestHandler(SimpleHTTPRequestHandler):
480483
def __init__(self):
481484
self.get_called = False
@@ -491,7 +494,7 @@ def do_GET(self):
491494
def log_message(self, format, *args):
492495
pass
493496

494-
@unittest.skipIf('/home/travis' in __file__, TRAVIS_MSG)
497+
@pytest.mark.skipif('/home/travis' in __file__, TRAVIS_MSG)
495498
class RejectingSocketlessRequestHandler(SocketlessRequestHandler):
496499
def handle_expect_100(self):
497500
self.send_error(417)
@@ -514,7 +517,7 @@ def numWrites(self):
514517
return len(self.datas)
515518

516519

517-
@unittest.skipIf('/home/travis' in __file__, TRAVIS_MSG)
520+
@pytest.mark.skipif('/home/travis' in __file__, TRAVIS_MSG)
518521
class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
519522
"""Test the functionality of the BaseHTTPServer.
520523
@@ -673,7 +676,7 @@ def test_header_length(self):
673676
self.assertFalse(self.handler.get_called)
674677

675678

676-
@unittest.skipIf('/home/travis' in __file__, TRAVIS_MSG)
679+
@pytest.mark.skipif('/home/travis' in __file__, TRAVIS_MSG)
677680
class SimpleHTTPRequestHandlerTestCase(unittest.TestCase):
678681
""" Test url parsing """
679682
def setUp(self):

future/tests/test_import_star.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_python3_stdlib_imports(self):
5353
import socketserver
5454

5555
def test_str(self):
56-
self.assertIsNot(str, bytes) # Py2: assertIsNot only in 2.7
56+
self.assertFalse(str is bytes)
5757
self.assertEqual(str('blah'), u'blah') # Py3.3 and Py2 only
5858

5959
def test_python_2_unicode_compatible_decorator(self):

future/tests/test_int.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""
2+
import pyte
23
int tests from Py3.3
34
"""
45

@@ -10,6 +11,7 @@
1011

1112
import unittest
1213
from test import support
14+
import pytest
1315

1416
L = [
1517
('0', 0),
@@ -225,7 +227,7 @@ def test_basic(self):
225227
self.assertEqual(int('2br45qc', 35), 4294967297)
226228
self.assertEqual(int('1z141z5', 36), 4294967297)
227229

228-
@unittest.expectedFailure # fails on Py2
230+
@pytest.mark.xfail # fails on Py2
229231
@support.cpython_only
230232
def test_small_ints(self):
231233
# Bug #3236: Return small longs from PyLong_FromString
@@ -368,7 +370,7 @@ def __trunc__(self):
368370

369371
# Exception messages in Py2 are 8-bit strings. The following fails,
370372
# even if the testlist strings are wrapped in str() calls...
371-
@unittest.expectedFailure
373+
@pytest.mark.xfail
372374
def test_error_message(self):
373375
testlist = ('\xbd', '123\xbd', ' 123 456 ')
374376
for s in testlist:

future/tests/test_standard_library_renames.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from future.standard_library import RENAMES, REPLACED_MODULES
1515
from future.tests.base import CodeHandler
16+
import pytest
1617

1718

1819
class TestStandardLibraryRenames(CodeHandler, unittest.TestCase):
@@ -21,7 +22,7 @@ def setUp(self):
2122
self.interpreter = 'python'
2223
self.tempdir = tempfile.mkdtemp() + os.path.sep
2324

24-
@unittest.skipIf(utils.PY3, 'generic import tests are for Py2 only')
25+
@pytest.mark.skipif(utils.PY3, reason='generic import tests are for Py2 only')
2526
def test_all(self):
2627
"""
2728
Tests whether all of the old imports in RENAMES are accessible
@@ -39,7 +40,7 @@ def test_all(self):
3940
if '.' not in oldname:
4041
self.assertEqual(oldmod, newmod)
4142

42-
@unittest.skipIf(utils.PY3, 'not testing for old urllib on Py3')
43+
@pytest.mark.skipif(utils.PY3, 'not testing for old urllib on Py3')
4344
def test_old_urllib_import(self):
4445
"""
4546
Tests whether an imported module can import the old urllib package.
@@ -151,15 +152,15 @@ def test_reprlib(self):
151152
def test_socketserver(self):
152153
import socketserver
153154

154-
@unittest.skip("Not testing tkinter import (it may be installed separately from Python)")
155+
@pytest.mark.skip("Not testing tkinter import (it may be installed separately from Python)")
155156
def test_tkinter(self):
156157
import tkinter
157158

158159
def test_builtins(self):
159160
import builtins
160161
self.assertTrue(hasattr(builtins, 'tuple'))
161162

162-
@unittest.skip("skipping in case there's no net connection")
163+
@pytest.mark.skip("skipping in case there's no net connection")
163164
def test_urllib_request(self):
164165
import urllib.request
165166
from pprint import pprint
@@ -177,14 +178,14 @@ def test_http_client_import(self):
177178
import http.client
178179
self.assertTrue(True)
179180

180-
@unittest.expectedFailure
181+
@pytest.mark.xfail
181182
def test_http_imports(self):
182183
import http
183184
import http.server
184185
import http.cookies
185186
import http.cookiejar
186187

187-
@unittest.expectedFailure
188+
@pytest.mark.xfail
188189
def test_urllib_imports(self):
189190
import urllib
190191
import urllib.parse
@@ -194,7 +195,7 @@ def test_urllib_imports(self):
194195
import urllib.response
195196
self.assertTrue(True)
196197

197-
@unittest.expectedFailure
198+
@pytest.mark.xfail
198199
def test_urllib_parse(self):
199200
import urllib.parse
200201
URL = 'http://pypi.python.org/test_url/spaces oh no/'

0 commit comments

Comments
 (0)