Skip to content
Prev Previous commit
Next Next commit
Use assertRaises as a context manager in the tests
  • Loading branch information
tzabal committed Apr 25, 2020
commit 50a843fceff657a2107b37a7563deeec46a2e7b2
16 changes: 10 additions & 6 deletions Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,8 @@ def test_nan(self):
self.assertTrue(_nan_equal(x, nan))

def test_raise_type_error(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename this test_invalid_input_type()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

self.assertRaises(TypeError, statistics._convert, None, float)
with self.assertRaises(TypeError):
statistics._convert(None, float)


class FailNegTest(unittest.TestCase):
Expand Down Expand Up @@ -1045,7 +1046,8 @@ def test_raise_value_error(self):
([1, 2], 3), # non-existing max value triggers `i != len(a)`
([1, 3], 2) # non-existing value triggers `a[i] == x`
]:
self.assertRaises(ValueError, statistics._find_lteq, a, x)
with self.assertRaises(ValueError):
statistics._find_lteq(a, x)

def test_locate_successfully(self):
for a, x, expected_i in [
Expand All @@ -1064,7 +1066,8 @@ def test_raise_value_error(self):
([1], 2, 1), # when l=len(a)+1 triggers `i != (len(a)+1)`
([1, 3], 0, 2) # non-existing value triggers `a[i-1] == x`
]:
self.assertRaises(ValueError, statistics._find_rteq, a, l, x)
with self.assertRaises(ValueError):
statistics._find_rteq(a, l, x)

def test_locate_successfully(self):
for a, l, x, expected_i in [
Expand Down Expand Up @@ -1496,9 +1499,6 @@ class TestHarmonicMean(NumericTestCase, AverageMixin, UnivariateTypeMixin):
def setUp(self):
self.func = statistics.harmonic_mean

def test_single_value_unsupported_type(self):
self.assertRaises(TypeError, self.func, ['3.14'])

def prepare_data(self):
# Override mixin method.
values = super().prepare_data()
Expand All @@ -1521,6 +1521,10 @@ def test_negative_error(self):
with self.subTest(values=values):
self.assertRaises(exc, self.func, values)

def test_single_value_unsupported_type(self):
with self.assertRaises(TypeError):
self.func(['3.14'])

def test_ints(self):
# Test harmonic mean with ints.
data = [2, 4, 4, 8, 16, 16]
Expand Down