Rewrite test_retry.py to be pytest-style
MODIFY
test/test_retry.py
test/test_retry.py
-import unittest
+import pytest
from urllib3.response import HTTPResponse
from urllib3.packages.six.moves import xrange
@@ -11,17 +11,15 @@ from urllib3.exceptions import (
)
-class RetryTest(unittest.TestCase):
+class TestRetry(object):
def test_string(self):
""" Retry string representation looks the way we expect """
retry = Retry()
- self.assertEqual(str(retry),
- 'Retry(total=10, connect=None, read=None, redirect=None, status=None)')
+ assert str(retry) == 'Retry(total=10, connect=None, read=None, redirect=None, status=None)'
for _ in range(3):
retry = retry.increment(method='GET')
- self.assertEqual(str(retry),
- 'Retry(total=7, connect=None, read=None, redirect=None, status=None)')
+ assert str(retry) == 'Retry(total=7, connect=None, read=None, redirect=None, status=None)'
def test_retry_both_specified(self):
"""Total can win if it's lower than the connect value"""
@@ -29,11 +27,9 @@ class RetryTest(unittest.TestCase):
retry = Retry(connect=3, total=2)
retry = retry.increment(error=error)
retry = retry.increment(error=error)
- try:
+ with pytest.raises(MaxRetryError) as e:
retry.increment(error=error)
- self.fail("Failed to raise error.")
- except MaxRetryError as e:
- self.assertEqual(e.reason, error)
+ assert e.value.reason == error
def test_retry_higher_total_loses(self):
""" A lower connect timeout than the total is honored """
@@ -41,7 +37,8 @@ class RetryTest(unittest.TestCase):
retry = Retry(connect=2, total=3)
retry = retry.increment(error=error)
retry = retry.increment(error=error)
- self.assertRaises(MaxRetryError, retry.increment, error=error)
+ with pytest.raises(MaxRetryError):
+ retry.increment(error=error)
def test_retry_higher_total_loses_vs_read(self):
""" A lower read timeout than the total is honored """
@@ -49,7 +46,8 @@ class RetryTest(unittest.TestCase):
retry = Retry(read=2, total=3)
retry = retry.increment(method='GET', error=error)
retry = retry.increment(method='GET', error=error)
- self.assertRaises(MaxRetryError, retry.increment, method='GET', error=error)
+ with pytest.raises(MaxRetryError):
+ retry.increment(method='GET', error=error)
def test_retry_total_none(self):
""" if Total is none, connect error should take precedence """
@@ -57,105 +55,99 @@ class RetryTest(unittest.TestCase):
retry = Retry(connect=2, total=None)
retry = retry.increment(error=error)
retry = retry.increment(error=error)
- try:
+ with pytest.raises(MaxRetryError) as e:
retry.increment(error=error)
- self.fail("Failed to raise error.")
- except MaxRetryError as e:
- self.assertEqual(e.reason, error)
+ assert e.value.reason == error
error = ReadTimeoutError(None, "/", "read timed out")
retry = Retry(connect=2, total=None)
retry = retry.increment(method='GET', error=error)
retry = retry.increment(method='GET', error=error)
retry = retry.increment(method='GET', error=error)
- self.assertFalse(retry.is_exhausted())
+ assert not retry.is_exhausted()
def test_retry_default(self):
""" If no value is specified, should retry connects 3 times """
retry = Retry()
- self.assertEqual(retry.total, 10)
- self.assertEqual(retry.connect, None)
- self.assertEqual(retry.read, None)
- self.assertEqual(retry.redirect, None)
+ assert retry.total == 10
+ assert retry.connect is None
+ assert retry.read is None
+ assert retry.redirect is None
error = ConnectTimeoutError()
retry = Retry(connect=1)
retry = retry.increment(error=error)
- self.assertRaises(MaxRetryError, retry.increment, error=error)
+ with pytest.raises(MaxRetryError):
+ retry.increment(error=error)
retry = Retry(connect=1)
retry = retry.increment(error=error)
- self.assertFalse(retry.is_exhausted())
+ assert not retry.is_exhausted()
- self.assertTrue(Retry(0).raise_on_redirect)
- self.assertFalse(Retry(False).raise_on_redirect)
+ assert Retry(0).raise_on_redirect
+ assert not Retry(False).raise_on_redirect
def test_retry_read_zero(self):
""" No second chances on read timeouts, by default """
error = ReadTimeoutError(None, "/", "read timed out")
retry = Retry(read=0)
- try:
+ with pytest.raises(MaxRetryError) as e:
retry.increment(method='GET', error=error)
- self.fail("Failed to raise error.")
- except MaxRetryError as e:
- self.assertEqual(e.reason, error)
+ assert e.value.reason == error
def test_status_counter(self):
resp = HTTPResponse(status=400)
retry = Retry(status=2)
retry = retry.increment(response=resp)
retry = retry.increment(response=resp)
- try:
+ with pytest.raises(MaxRetryError) as e:
retry.increment(response=resp)
- self.fail("Failed to raise error.")
- except MaxRetryError as e:
- self.assertEqual(str(e.reason),
- ResponseError.SPECIFIC_ERROR.format(status_code=400))
+ assert str(e.value.reason) == ResponseError.SPECIFIC_ERROR.format(status_code=400)
def test_backoff(self):
""" Backoff is computed correctly """
max_backoff = Retry.BACKOFF_MAX
retry = Retry(total=100, backoff_factor=0.2)
- self.assertEqual(retry.get_backoff_time(), 0) # First request
+ assert retry.get_backoff_time() == 0 # First request
retry = retry.increment(method='GET')
- self.assertEqual(retry.get_backoff_time(), 0) # First retry
+ assert retry.get_backoff_time() == 0 # First retry
retry = retry.increment(method='GET')
- self.assertEqual(retry.backoff_factor, 0.2)
- self.assertEqual(retry.total, 98)
- self.assertEqual(retry.get_backoff_time(), 0.4) # Start backoff
+ assert retry.backoff_factor == 0.2
+ assert retry.total == 98
+ assert retry.get_backoff_time() == 0.4 # Start backoff
retry = retry.increment(method='GET')
- self.assertEqual(retry.get_backoff_time(), 0.8)
+ assert retry.get_backoff_time() == 0.8
retry = retry.increment(method='GET')
- self.assertEqual(retry.get_backoff_time(), 1.6)
+ assert retry.get_backoff_time() == 1.6
- for i in xrange(10):
+ for _ in xrange(10):
retry = retry.increment(method='GET')
- self.assertEqual(retry.get_backoff_time(), max_backoff)
+ assert retry.get_backoff_time() == max_backoff
def test_zero_backoff(self):
retry = Retry()
- self.assertEqual(retry.get_backoff_time(), 0)
+ assert retry.get_backoff_time() == 0
retry = retry.increment(method='GET')
retry = retry.increment(method='GET')
- self.assertEqual(retry.get_backoff_time(), 0)
+ assert retry.get_backoff_time() == 0
def test_backoff_reset_after_redirect(self):
retry = Retry(total=100, redirect=5, backoff_factor=0.2)
retry = retry.increment(method='GET')
retry = retry.increment(method='GET')
- self.assertEqual(retry.get_backoff_time(), 0.4)
+ assert retry.get_backoff_time() == 0.4
redirect_response = HTTPResponse(status=302, headers={'location': 'test'})
retry = retry.increment(method='GET', response=redirect_response)
- self.assertEqual(retry.get_backoff_time(), 0)
+ assert retry.get_backoff_time() == 0
retry = retry.increment(method='GET')
retry = retry.increment(method='GET')
- self.assertEqual(retry.get_backoff_time(), 0.4)
+ assert retry.get_backoff_time() == 0.4
def test_sleep(self):
# sleep a very small amount of time so our code coverage is happy
@@ -166,101 +158,94 @@ class RetryTest(unittest.TestCase):
def test_status_forcelist(self):
retry = Retry(status_forcelist=xrange(500, 600))
- self.assertFalse(retry.is_retry('GET', status_code=200))
- self.assertFalse(retry.is_retry('GET', status_code=400))
- self.assertTrue(retry.is_retry('GET', status_code=500))
+ assert not retry.is_retry('GET', status_code=200)
+ assert not retry.is_retry('GET', status_code=400)
+ assert retry.is_retry('GET', status_code=500)
retry = Retry(total=1, status_forcelist=[418])
- self.assertFalse(retry.is_retry('GET', status_code=400))
- self.assertTrue(retry.is_retry('GET', status_code=418))
+ assert not retry.is_retry('GET', status_code=400)
+ assert retry.is_retry('GET', status_code=418)
# String status codes are not matched.
retry = Retry(total=1, status_forcelist=['418'])
- self.assertFalse(retry.is_retry('GET', status_code=418))
+ assert not retry.is_retry('GET', status_code=418)
def test_method_whitelist_with_status_forcelist(self):
# Falsey method_whitelist means to retry on any method.
retry = Retry(status_forcelist=[500], method_whitelist=None)
- self.assertTrue(retry.is_retry('GET', status_code=500))
- self.assertTrue(retry.is_retry('POST', status_code=500))
+ assert retry.is_retry('GET', status_code=500)
+ assert retry.is_retry('POST', status_code=500)
# Criteria of method_whitelist and status_forcelist are ANDed.
retry = Retry(status_forcelist=[500], method_whitelist=['POST'])
- self.assertFalse(retry.is_retry('GET', status_code=500))
- self.assertTrue(retry.is_retry('POST', status_code=500))
+ assert not retry.is_retry('GET', status_code=500)
+ assert retry.is_retry('POST', status_code=500)
def test_exhausted(self):
- self.assertFalse(Retry(0).is_exhausted())
- self.assertTrue(Retry(-1).is_exhausted())
- self.assertEqual(Retry(1).increment(method='GET').total, 0)
+ assert not Retry(0).is_exhausted()
+ assert Retry(-1).is_exhausted()
+ assert Retry(1).increment(method='GET').total == 0
- def test_disabled(self):
- self.assertRaises(MaxRetryError, Retry(-1).increment, method='GET')
- self.assertRaises(MaxRetryError, Retry(0).increment, method='GET')
+ @pytest.mark.parametrize('total', [-1, 0])
+ def test_disabled(self, total):
+ with pytest.raises(MaxRetryError):
+ Retry(total).increment(method='GET')
def test_error_message(self):
retry = Retry(total=0)
- try:
+ with pytest.raises(MaxRetryError) as e:
retry = retry.increment(method='GET',
error=ReadTimeoutError(None, "/", "read timed out"))
- raise AssertionError("Should have raised a MaxRetryError")
- except MaxRetryError as e:
- assert 'Caused by redirect' not in str(e)
- self.assertEqual(str(e.reason), 'None: read timed out')
+ assert 'Caused by redirect' not in str(e.value)
+ assert str(e.value.reason) == 'None: read timed out'
retry = Retry(total=1)
- try:
+ with pytest.raises(MaxRetryError) as e:
retry = retry.increment('POST', '/')
retry = retry.increment('POST', '/')
- raise AssertionError("Should have raised a MaxRetryError")
- except MaxRetryError as e:
- assert 'Caused by redirect' not in str(e)
- self.assertTrue(isinstance(e.reason, ResponseError),
- "%s should be a ResponseError" % e.reason)
- self.assertEqual(str(e.reason), ResponseError.GENERIC_ERROR)
+ assert 'Caused by redirect' not in str(e.value)
+ assert isinstance(e.value.reason, ResponseError)
+ assert str(e.value.reason) == ResponseError.GENERIC_ERROR
retry = Retry(total=1)
- try:
response = HTTPResponse(status=500)
+ with pytest.raises(MaxRetryError) as e:
retry = retry.increment('POST', '/', response=response)
retry = retry.increment('POST', '/', response=response)
- raise AssertionError("Should have raised a MaxRetryError")
- except MaxRetryError as e:
- assert 'Caused by redirect' not in str(e)
+ assert 'Caused by redirect' not in str(e.value)
msg = ResponseError.SPECIFIC_ERROR.format(status_code=500)
- self.assertEqual(str(e.reason), msg)
+ assert str(e.value.reason) == msg
retry = Retry(connect=1)
- try:
+ with pytest.raises(MaxRetryError) as e:
retry = retry.increment(error=ConnectTimeoutError('conntimeout'))
retry = retry.increment(error=ConnectTimeoutError('conntimeout'))
- raise AssertionError("Should have raised a MaxRetryError")
- except MaxRetryError as e:
- assert 'Caused by redirect' not in str(e)
- self.assertEqual(str(e.reason), 'conntimeout')
+ assert 'Caused by redirect' not in str(e.value)
+ assert str(e.value.reason) == 'conntimeout'
def test_history(self):
retry = Retry(total=10, method_whitelist=frozenset(['GET', 'POST']))
- self.assertEqual(retry.history, tuple())
+ assert retry.history == tuple()
connection_error = ConnectTimeoutError('conntimeout')
retry = retry.increment('GET', '/test1', None, connection_error)
history = (RequestHistory('GET', '/test1', connection_error, None, None),)
- self.assertEqual(retry.history, history)
+ assert retry.history == history
read_error = ReadTimeoutError(None, "/test2", "read timed out")
retry = retry.increment('POST', '/test2', None, read_error)
history = (RequestHistory('GET', '/test1', connection_error, None, None),
RequestHistory('POST', '/test2', read_error, None, None))
- self.assertEqual(retry.history, history)
+ assert retry.history == history
response = HTTPResponse(status=500)
retry = retry.increment('GET', '/test3', response, None)
history = (RequestHistory('GET', '/test1', connection_error, None, None),
RequestHistory('POST', '/test2', read_error, None, None),
RequestHistory('GET', '/test3', None, 500, None))
- self.assertEqual(retry.history, history)
+ assert retry.history == history
def test_retry_method_not_in_whitelist(self):
error = ReadTimeoutError(None, "/", "read timed out")
retry = Retry()
- self.assertRaises(ReadTimeoutError, retry.increment, method='POST', error=error)
+ with pytest.raises(ReadTimeoutError):
+ retry.increment(method='POST', error=error)

Rewrite test_retry.py to be pytest-style