Skip to content

Improve exception tests #945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions exercises/binary-search/binary_search_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ def test_finds_value_in_array_of_even_length(self):
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__':
Expand Down
12 changes: 8 additions & 4 deletions exercises/binary/binary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,20 @@ def test_binary_10001101000_is_decimal_1128(self):
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__':
Expand Down
4 changes: 2 additions & 2 deletions exercises/meetup/meetup_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ def test_fifth_monday_of_march_2015(self):
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__':
Expand Down
9 changes: 6 additions & 3 deletions exercises/minesweeper/minesweeper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,22 @@ def test_different_len(self):
"|* |",
"| |",
"+-+"]
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__':
Expand Down
19 changes: 11 additions & 8 deletions exercises/ocr-numbers/ocr_numbers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ def test_unknown_char(self):
" "]), '?')

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'), [" _ ",
Expand Down Expand Up @@ -114,7 +116,8 @@ def test_grid3186547290(self):
])

def test_invalid_grid(self):
self.assertRaises(ValueError, grid, '123a')
with self.assertRaises(ValueError):
grid('123a')


if __name__ == '__main__':
Expand Down
3 changes: 2 additions & 1 deletion exercises/pythagorean-triplet/pythagorean_triplet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def test_is_triplet3(self):
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__':
Expand Down
3 changes: 2 additions & 1 deletion exercises/saddle-points/saddle_points_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def test_empty_matrix(self):

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__':
Expand Down
6 changes: 4 additions & 2 deletions exercises/simple-cipher/simple_cipher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ def test_cipher_random_key(self):
'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__':
Expand Down
15 changes: 10 additions & 5 deletions exercises/triangle/triangle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,24 @@ def test_very_small_triangles_are_legal(self):
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__':
Expand Down
13 changes: 8 additions & 5 deletions exercises/wordy/wordy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,20 @@ def test_divide_twice(self):
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__':
Expand Down