.. include:: global.rst .. _Overview: *************************** Python Testing *************************** Testing Notes ======================= * 1) Create a file to run your test on * 2) Import the unittest package and the function you want to test from your file .. code-block:: python import unittest from [file name] import [function name] * 3) Create a class that inherits from unittest.TestCase that will test your function .. code-block:: python class TestFunction(unittest.TestCase): * 4) Create a method that tests a specific instance of the function .. code-block:: python def test_example(self): * 5) Use the following methods to check if the expected output of a function matches with the corresponding input * .assertEqual(a, b) - Checks if a is equal to b * .assertNotEqual(a, b) - Checks if a is unequal to b * .assertTrue(x) - Checks if x is true * .assertFalse(x) - Checks if x is false .. code-block:: python # Example using the "add()" function: self.assertEqual(add(2,5), 7) * 6) To run through multiple instances, create a for loop and then create a subset using unittest's built in method .. code-block:: python for a in [2, 4, 6, -8, -10, -12]: with self.subTest(a=a): self.assertTrue(isEven(a)) * 7) Once you've created all the methods to test all types of instances, run unittest.main in main .. code-block:: python if __name__ == "__main__": unittest.main() This should give you the results of the test and any errors that don't match the intended result AI Queue Management Example ============================== .. literalinclude:: ./python/queuemanagement.py AI Testing Example ======================= .. literalinclude:: ./python/testQueuemanagement.py AI Explanation ======================= I used the standard unittest library to create a robust suite. Here is a breakdown of what each test case verifies: * 1) setUp(self): This method runs before every single test. It ensures we start with a brand-new, empty Queue instance (self.queue) for consistency. * 2) test_initial_state: Verifies that a newly created queue is correctly reported as empty and has a size of zero. * 3) test_enqueue_and_size: Confirms that the enqueue operation successfully adds an item, and the size() and is_empty() methods update their state correctly. * 4) test_dequeue_fifo_principle: This is the most critical test. It enqueues three elements ("A", "B", "C") and checks that when dequeue is called, they are returned in the exact same order they were added (A then B then C), ensuring the FIFO behavior. * 5) test_peek_functionality: Ensures that peek() correctly identifies the front element but does not remove it, leaving the queue size and state unchanged. * 6) test_dequeue_on_empty: Handles the edge case where a user attempts to remove an item from an empty queue, verifying the function returns None and the program doesn't crash. * 7) test_peek_on_empty: Handles the edge case where a user attempts to peek at an empty queue, verifying the function returns None. * 8) test_mixed_enqueue_and_dequeue: Tests a more realistic scenario involving alternating additions and removals to ensure the internal state remains consistent across complex operations. Running Sum Class (Example Program) ========================================== .. literalinclude:: ./python/runningsum.py Testing Running Sum ========================================== .. literalinclude:: ./python/testRunningsum.py