Improve exception tests by making use of Context Managers to resolve #477
This commit is contained in:
committed by
Ilya Khadykin
parent
5933ce81d3
commit
f8f495ca4a
@@ -22,6 +22,7 @@ A list of missing exercise can be found here: http://exercism.io/languages/pytho
|
||||
- We use minimalistic stub files for all exercises (#272).
|
||||
- We use `unittest` (Python Standard Library) and no 3rd-party-framework.
|
||||
- We use the parameter order `self.assertEqual(actual, expected)` (#440).
|
||||
- We use context managers (`with self.assertRaises(\<exception type\>):`) for testing for exceptions (#477).
|
||||
|
||||
|
||||
### Testing
|
||||
|
||||
@@ -27,17 +27,20 @@ class BinarySearchTests(unittest.TestCase):
|
||||
5)
|
||||
|
||||
def test_identifies_value_missing(self):
|
||||
self.assertRaises(ValueError, binary_search, [1, 3, 4, 6, 8, 9, 11], 7)
|
||||
with self.assertRaises(ValueError):
|
||||
binary_search([1, 3, 4, 6, 8, 9, 11], 7)
|
||||
|
||||
def test_value_smaller_than_arrays_minimum(self):
|
||||
self.assertRaises(ValueError, binary_search, [1, 3, 4, 6, 8, 9, 11], 0)
|
||||
with self.assertRaises(ValueError):
|
||||
binary_search([1, 3, 4, 6, 8, 9, 11], 0)
|
||||
|
||||
def test_value_larger_than_arrays_maximum(self):
|
||||
self.assertRaises(ValueError, binary_search, [1, 3, 4, 6, 8, 9, 11],
|
||||
13)
|
||||
with self.assertRaises(ValueError):
|
||||
binary_search([1, 3, 4, 6, 8, 9, 11], 13)
|
||||
|
||||
def test_empty_array(self):
|
||||
self.assertRaises(ValueError, binary_search, [], 1)
|
||||
with self.assertRaises(ValueError):
|
||||
binary_search([], 1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -32,16 +32,20 @@ class BinaryTests(unittest.TestCase):
|
||||
self.assertEqual(parse_binary("10001101000"), 1128)
|
||||
|
||||
def test_invalid_binary_text_only(self):
|
||||
self.assertRaises(ValueError, parse_binary, "carrot")
|
||||
with self.assertRaises(ValueError):
|
||||
parse_binary("carrot")
|
||||
|
||||
def test_invalid_binary_number_not_base2(self):
|
||||
self.assertRaises(ValueError, parse_binary, "102011")
|
||||
with self.assertRaises(ValueError):
|
||||
parse_binary("102011")
|
||||
|
||||
def test_invalid_binary_numbers_with_text(self):
|
||||
self.assertRaises(ValueError, parse_binary, "10nope")
|
||||
with self.assertRaises(ValueError):
|
||||
parse_binary("10nope")
|
||||
|
||||
def test_invalid_binary_text_with_numbers(self):
|
||||
self.assertRaises(ValueError, parse_binary, "nope10")
|
||||
with self.assertRaises(ValueError):
|
||||
parse_binary("nope10")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -399,8 +399,8 @@ class MeetupTest(unittest.TestCase):
|
||||
meetup_day(2015, 3, 'Monday', '5th'), date(2015, 3, 30))
|
||||
|
||||
def test_nonexistent_fifth_monday_of_february_2015(self):
|
||||
self.assertRaises(MeetupDayException, meetup_day, 2015, 2, 'Monday',
|
||||
'5th')
|
||||
with self.assertRaises(MeetupDayException):
|
||||
meetup_day(2015, 2, 'Monday', '5th')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -141,19 +141,22 @@ class MinesweeperTest(unittest.TestCase):
|
||||
"|* |",
|
||||
"| |",
|
||||
"+-+"]
|
||||
self.assertRaises(ValueError, board, inp)
|
||||
with self.assertRaises(ValueError):
|
||||
board(inp)
|
||||
|
||||
def test_faulty_border(self):
|
||||
inp = ["+-----+",
|
||||
"* * |",
|
||||
"+-- --+"]
|
||||
self.assertRaises(ValueError, board, inp)
|
||||
with self.assertRaises(ValueError):
|
||||
board(inp)
|
||||
|
||||
def test_invalid_char(self):
|
||||
inp = ["+-----+",
|
||||
"|X * |",
|
||||
"+-----+"]
|
||||
self.assertRaises(ValueError, board, inp)
|
||||
with self.assertRaises(ValueError):
|
||||
board(inp)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -43,15 +43,17 @@ class OcrTest(unittest.TestCase):
|
||||
" "]), '?')
|
||||
|
||||
def test_too_short_row(self):
|
||||
self.assertRaises(ValueError, number, [" ",
|
||||
" _|",
|
||||
" |",
|
||||
" "])
|
||||
with self.assertRaises(ValueError):
|
||||
number([" ",
|
||||
" _|",
|
||||
" |",
|
||||
" "])
|
||||
|
||||
def test_insufficient_rows(self):
|
||||
self.assertRaises(ValueError, number, [" ",
|
||||
" _|",
|
||||
" X|"])
|
||||
with self.assertRaises(ValueError):
|
||||
number([" ",
|
||||
" _|",
|
||||
" X|"])
|
||||
|
||||
def test_grid0(self):
|
||||
self.assertEqual(grid('0'), [" _ ",
|
||||
@@ -114,7 +116,8 @@ class OcrTest(unittest.TestCase):
|
||||
])
|
||||
|
||||
def test_invalid_grid(self):
|
||||
self.assertRaises(ValueError, grid, '123a')
|
||||
with self.assertRaises(ValueError):
|
||||
grid('123a')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -80,7 +80,8 @@ class PythagoreanTripletTest(unittest.TestCase):
|
||||
self.assertIs(is_triplet((924, 43, 925)), True)
|
||||
|
||||
def test_odd_number(self):
|
||||
self.assertRaises(ValueError, primitive_triplets, 5)
|
||||
with self.assertRaises(ValueError):
|
||||
primitive_triplets(5)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -28,7 +28,8 @@ class SaddlePointTest(unittest.TestCase):
|
||||
|
||||
def test_irregular_matrix(self):
|
||||
inp = [[3, 2, 1], [0, 1], [2, 1, 0]]
|
||||
self.assertRaises(ValueError, saddle_points, inp)
|
||||
with self.assertRaises(ValueError):
|
||||
saddle_points(inp)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -68,8 +68,10 @@ class CipherTest(unittest.TestCase):
|
||||
'All items in the key must be chars and lowercase!')
|
||||
|
||||
def test_cipher_wrong_key(self):
|
||||
self.assertRaises(ValueError, Cipher, 'a1cde')
|
||||
self.assertRaises(ValueError, Cipher, 'aBcde')
|
||||
with self.assertRaises(ValueError):
|
||||
Cipher('a1cde')
|
||||
with self.assertRaises(ValueError):
|
||||
Cipher('aBcde')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -34,19 +34,24 @@ class TriangleTests(unittest.TestCase):
|
||||
self.assertEqual(Triangle(0.4, 0.6, 0.3).kind(), "scalene")
|
||||
|
||||
def test_triangles_with_no_size_are_illegal(self):
|
||||
self.assertRaises(TriangleError, Triangle, 0, 0, 0)
|
||||
with self.assertRaises(TriangleError):
|
||||
Triangle(0, 0, 0)
|
||||
|
||||
def test_triangles_with_negative_sides_are_illegal(self):
|
||||
self.assertRaises(TriangleError, Triangle, 3, 4, -5)
|
||||
with self.assertRaises(TriangleError):
|
||||
Triangle(3, 4, -5)
|
||||
|
||||
def test_triangles_violating_triangle_inequality_are_illegal(self):
|
||||
self.assertRaises(TriangleError, Triangle, 1, 1, 3)
|
||||
with self.assertRaises(TriangleError):
|
||||
Triangle(1, 1, 3)
|
||||
|
||||
def test_triangles_violating_triangle_inequality_are_illegal_2(self):
|
||||
self.assertRaises(TriangleError, Triangle, 2, 4, 2)
|
||||
with self.assertRaises(TriangleError):
|
||||
Triangle(2, 4, 2)
|
||||
|
||||
def test_triangles_violating_triangle_inequality_are_illegal_3(self):
|
||||
self.assertRaises(TriangleError, Triangle, 7, 3, 2)
|
||||
with self.assertRaises(TriangleError):
|
||||
Triangle(7, 3, 2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -49,17 +49,20 @@ class WordyTest(unittest.TestCase):
|
||||
calculate("What is -12000 divided by 25 divided by -30?"), 16)
|
||||
|
||||
def test_invalid_operation(self):
|
||||
self.assertRaises(ValueError, calculate, "What is 4 xor 7?")
|
||||
with self.assertRaises(ValueError):
|
||||
calculate("What is 4 xor 7?")
|
||||
|
||||
def test_missing_operation(self):
|
||||
self.assertRaises(ValueError, calculate, "What is 2 2 minus 3?")
|
||||
with self.assertRaises(ValueError):
|
||||
calculate("What is 2 2 minus 3?")
|
||||
|
||||
def test_missing_number(self):
|
||||
self.assertRaises(ValueError, calculate,
|
||||
"What is 7 plus multiplied by -2?")
|
||||
with self.assertRaises(ValueError):
|
||||
calculate("What is 7 plus multiplied by -2?")
|
||||
|
||||
def test_irrelevant_question(self):
|
||||
self.assertRaises(ValueError, calculate, "Which is greater, 3 or 2?")
|
||||
with self.assertRaises(ValueError):
|
||||
calculate("Which is greater, 3 or 2?")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user