class PhoneBookTest(unittest.TestCase):
    def setUp(self) -> None:
        self.phonebook = PhoneBook()

    def test_look_up_name(self):
        self.phonebook.add("Bob", "12345")
        number = self.phonebook.lookup("Bob")
        self.assertEqual("12345", number)

    def test_missing_name(self):
        with self.assertRaises(KeyError):
            self.phonebook.lookup("missing")

    def test_empty_phonebook_is_consistent(self):
        self.assertTrue(self.phonebook.is_consistent())

    def test_is_consistent_with_different_entries(self):
        self.phonebook.add("Bob", "12345")
        self.phonebook.add("Anna", "012345")
        self.assertTrue(self.phonebook.is_consistent())

    def test_inconsistent_with_duplicate_entries(self):
        self.phonebook.add("Bob", "12345")
        self.phonebook.add("Sue", "12345")
        self.assertFalse(self.phonebook.is_consistent())

    def test_inconsistent_with_duplicate_prefix(self):
        self.phonebook.add("Bob", "12345")
        self.phonebook.add("Sue", "123")
        self.assertFalse(self.phonebook.is_consistent())

    def test_phonebook_adds_names_and_numbers(self):
        self.phonebook.add("Sue", "123343")
        self.assertIn("Sue", self.phonebook.get_names())
        self.assertIn("123343", self.phonebook.get_numbers())
class PhoneBookTest(unittest.TestCase):
    # Test fixture
    def setUp(self):
        self.phonebook = PhoneBook()

    # Test fixture
    # Tear down resources that were initialized in setUp
    def tearDown(self):
        pass

    # Test case
    def test_lookup_by_name(self):  # Test case name
        self.phonebook.add("Bob", "12345")  # Arrange
        number = self.phonebook.lookup("Bob")  # Act
        self.assertEqual("12345", number)  # assert

    def test_missing_name(self):
        with self.assertRaises(KeyError):
            self.phonebook.lookup("missing")

    def test_is_consistent_with_empty_phonebook(self):
        self.assertTrue(self.phonebook.is_consistent())

    # Not good test case design. We'll miss a lot of the tests if there is a failure early in the test.
    # Testing stops when an exception is encountered
    # Name of test case isn't specific
    @unittest.skip("Bad test case design.")
    def test_is_consistent(self):
        self.phonebook.add("Bob", "12345")
        self.assertTrue(self.phonebook.is_consistent())
        self.phonebook.add("Anna", "012345")
        self.assertTrue(self.phonebook.is_consistent())
        self.phonebook.add("Sue", "12345")  # identitcal to Bob
        self.assertFalse(self.phonebook.is_consistent())
        self.phonebook.add("Sue", "123")  # prefix of Bob
        self.assertFalse(self.phonebook.is_consistent())

    def test_is_consistent_with_different_entries(self):
        self.phonebook.add("Bob", "12345")
        self.phonebook.add("Anna", "012345")
        self.assertTrue(self.phonebook.is_consistent())

    def test_inconsistent_with_duplicate_entries(self):
        self.phonebook.add("Bob", "12345")
        self.phonebook.add("Sue", "12345")
        self.assertFalse(self.phonebook.is_consistent())

    def test_inconsistent_with_duplicate_prefix(self):
        self.phonebook.add("Bob", "12345")
        self.phonebook.add("Sue", "123")
        self.assertFalse(self.phonebook.is_consistent())

    def test_phonebook_adds_names_and_numbers(self):
        self.phonebook.add("Sue", "123343")  # Act
        self.assertIn(
            "Sue", self.phonebook.get_names())  # assert made on the same Act
        self.assertIn(
            "123343",
            self.phonebook.get_numbers())  # assert made on the same Act
class PhoneBookTest(unittest.TestCase):
    # Runs before EVERY test
    def setUp(self) -> None:
        self.phonebook = PhoneBook()

    # Runs after EVERY test
    def tearDown(self) -> None:
        pass

    def test_lookup_by_name(self):
        self.phonebook.add("Bob", "12345")
        number = self.phonebook.lookup("Bob")
        self.assertEqual("12345", number)

    def test_missing_name_raises_error(self):
        # This will test that any of the code below the "with" stmt throws a KeyError
        with self.assertRaises(KeyError):
            self.phonebook.lookup("fake number")

    def test_is_consistent_when_empty(self):
        self.assertTrue(self.phonebook.is_consistent())

    def test_is_consistent_when_all_nums_different(self):
        self.phonebook.add("Bob", "1234567")
        self.phonebook.add("Sue", "6171830")
        self.assertTrue(self.phonebook.is_consistent())

    def test_is_NOT_consistent_when_duplicates(self):
        self.phonebook.add("Bob", "1234567")
        self.phonebook.add("Sue", "1234567")
        self.assertFalse(self.phonebook.is_consistent())

    def test_is_NOT_consistent_when_duplicate_prefixes(self):
        self.phonebook.add("Bob", "1234567")
        self.phonebook.add("Sue", "123")
        self.assertFalse(self.phonebook.is_consistent())

    def test_phonebook_adds_names_and_numbers(self):
        self.phonebook.add("Sue", "12345")
        self.phonebook.add("Bob", "1234567")
        self.assertIn("Sue", self.phonebook.get_names())
        self.assertIn("12345", self.phonebook.get_numbers())
        self.assertIn("Bob", self.phonebook.get_names())
        self.assertIn("1234567", self.phonebook.get_numbers())
Beispiel #4
0
class PhoneBookTest(unittest.TestCase):
    def setUp(self) -> None:
        self.phonebook = PhoneBook()

    def test_lookup_by_name(self):
        self.phonebook.add("Bob", "12345")
        number = self.phonebook.lookup("Bob")
        self.assertEqual("12345", number)

    def test_missing_name_raises_error(self):
        with self.assertRaises(KeyError):
            self.phonebook.lookup("missing")

    def test_empty_phonebook_is_consistent(self):
        self.assertTrue(self.phonebook.is_consistent())

    def test_is_consistent_with_different_entries(self):
        self.phonebook.add("Bob", "12345")
        self.phonebook.add("Anna", "012345")
        self.assertTrue(self.phonebook.is_consistent())

    def test_inconsistent_with_duplicate_entries(self):
        self.phonebook.add("Bob", "12345")
        self.phonebook.add("Sue", "12345")
        self.assertFalse(self.phonebook.is_consistent())

    def test_inconsistent_with_duplicate_prefix(self):
        self.phonebook.add("Bob", "12345")
        self.phonebook.add("Sue", "123")
        self.assertFalse(self.phonebook.is_consistent())

    # Unusually, to put two cases assertion in one test case!
    def test_phonebook_adds_names_and_numbers(self):
        self.phonebook.add("Sue", '123343')
        self.assertIn("Sue", self.phonebook.get_names())
        self.assertIn("123343", self.phonebook.get_numbers())


# python -m unittest   unit test in command line
Beispiel #5
0
class PhoneBookTest(unittest.TestCase):
    
    def setUp(self):
        self.phonebook = PhoneBook()

    def test_lookup_by_name(self):
        self.phonebook.add('Bob','12345')
        number = self.phonebook.lookup('Bob')
        self.assertEqual(number,'12345')

    def test_missing_name_raises_error(self):
        with self.assertRaises(KeyError):
            self.phonebook.lookup('missing_name')
    
    # @unittest.skip('WIP')
    def test_empty_phonebook_is_consistent(self):
        self.assertTrue(self.phonebook.is_consistent())

    def test_is_consistent_with_different_entries(self):
        self.phonebook.add('Bob','12345')
        self.phonebook.add('Anna','012345')
        self.assertTrue(self.phonebook.is_consistent())

    def test_inconsistent_with_duplicate_entries(self):
        self.phonebook.add('Bob','12345')
        self.phonebook.add('Sue','12345')
        self.assertFalse(self.phonebook.is_consistent())

    def test_inconsistent_with_duplicate_prefix(self):
        self.phonebook.add('Sue','123')
        self.phonebook.add('Bob','12345')
        self.assertFalse(self.phonebook.is_consistent())

    def test_phonebook_add_names_and_numbers(self):
        self.phonebook.add('Sue','12345')
        self.assertIn('Sue',self.phonebook.get_names())
        self.assertIn('123343',self.phonebook.get_numbers())