Last active
May 30, 2019 07:58
-
-
Save jslvtr/de28a530f00e7486b51efa3c17f44276 to your computer and use it in GitHub Desktop.
Introduction to unit testing with Python (https://blog.tecladocode.com/introduction-to-unit-testing-with-python/) — final complete code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unittest import TestCase | |
from typing import List | |
def has_mixed_types(list_: List): | |
first = type(list_[0]) | |
return any(not isinstance(t, first) for t in list_) | |
def reverse_list(original: List) -> List: | |
if has_mixed_types(original): | |
raise ValueError("The list to be reversed should have homogeneous types.") | |
return original[::-1] | |
class ReverseListTest(TestCase): | |
def test_reverses_nonempty_list(self): | |
original = [3, 7, 1, 10] | |
expected = [10, 1, 7, 3] | |
self.assertEqual(reverse_list(original), expected) | |
def test_reverses_varying_elements_raises(self): | |
original = [3, 7, "a"] | |
with self.assertRaises(ValueError): | |
reverse_list(original) | |
def test_reverses_empty_list(self): | |
original = [] | |
expected = [] | |
self.assertEqual(reverse_list(original), expected) | |
def test_reversal_gives_new_list(self): | |
original = [3, 5, 7] | |
expected = [3, 5, 7] | |
reverse_list(original) | |
self.assertEqual(original, expected) | |
def test_reversal_with_duplicates(self): | |
original = [3, 4, 3, 3, 9] | |
expected = [9, 3, 3, 4, 3] | |
self.assertEqual(reverse_list(original), expected) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment