Answers: Strong Code

Contents

Answers: Strong Code#

Provided here are answers to the practice questions at the end of “Strong Code”.

Code Testing#

Code Testing Q1

class Person:

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def birthday(self):
        self.age += 1
        return 'Happy Birthday!'
class TestPerson(unittest.TestCase):
    
    Jonathan = Person('Jonathan', 20)

    def test_class(self):
        self.assertIsInstance(self.Jonathan, Person)
                
    def test_attributes(self):
        self.assertEqual('Jonathan', self.Jonathan.name)
        self.assertEqual(20, self.Jonathan.age)

    def test_birthday(self):
        self.Jonathan.birthday()
        self.assertEqual(21, self.Jonathan.age)

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestPerson)
    unittest.TextTestRunner(verbosity=2).run(suite)