Page 390 - Computer_Science_F5
P. 390

(ii)  Example using ‘unittest’:

                        import unittest
                        class TestLibrarySystem(unittest.TestCase):
                            def test_book_checkout(self):                                          Chapter Five: Object oriented programming with Java
          FOR ONLINE READING ONLY
                                book = Book(“Test Book”, “Author”, “12345”, 2)
                                self.assertTrue(book.checkout())
                                self.assertEqual(book.available_copies, 1)

                            def test_user_checkout_book(self):
                                book = Book(“Test Book”, “Author”, “12345”, 1)

                                user = User(“Alice”, “001”)
                                user.checkout_book(book)
                                self.assertIn(book, user.checked_out_books)


                        if __name__ == ‘__main__’:
                            unittest.main()


                (b) Integration tests:
                    Ensure the ‘Library’, ‘Book’, and ‘User’ classes work together smoothly.


                      library = Library()
                      book1 = Book(“Python Programming”, “ Sarai Mkuvangwa “, “1234567890”, 3)
                      user1 = User(“Alice”, “001”)

                      library.add_book(book1)

                      library.add_user(user1)

                      user1.checkout_book(book1)
                      user1.return_book(book1)



              2. Debugging
                (a) Print statements: Add print statements within methods to track flow and
                    variable states.
                (b) Debugging Tools:
                    (i)   Use pdb for step-by-step debugging.
                    (ii) Set breakpoints in your IDE to monitor execution.


                                                    381
               Student’s Book  Form Five



     Computer Science Form 5.indd   381                                                     23/07/2024   12:34
   385   386   387   388   389   390   391   392   393   394   395