Testing the Todo App
In the previous section, we constructed a Todo app using the Parameterized
class-based approach. Now, we'll delve into how this approach enables easy testing of your app in Python. Ensuring your app's testability guarantees its extensibility and maintainability over time.
We firmly believe that the simplicity and accessibility of testing Panel components underscore a truly invaluable facet of Panel.
Running the Tests
First, copy the app code above into a file named app.py
, and the test code into a file named test_app.py
.
:::{dropdown} Code: app.py
:::
:::{dropdown} Code: test_app.py
:::
Run the tests with pytest test_app.py
. It should look like this:
Explanation
test_create_task
This test ensures that a Task
instance can be created successfully. It initializes a task with the description "Do this" and marks it as completed. Then, it checks if the task's attributes are correctly set and if the __panel__()
method returns a valid Panel component.
test_can_create_task_list_without_tasks
This test validates the behavior of creating a TaskList
instance without any tasks. It checks if the task list initializes with an empty list and if the status attributes are correctly set to reflect no tasks.
test_can_create_task_list_with_tasks
This test ensures that we can create a TaskList
instance with a predefined list of tasks. It checks if the task list initializes with the provided tasks, calculates the total number of tasks accurately, and sets the status attribute accordingly.
test_can_add_new_task_to_task_list
This test verifies if we can add a new task to the TaskList
instance successfully. It adds a new task to the task list, checks if the task list reflects the addition, and updates the status attribute accordingly when the task is marked as completed.
test_can_replace_tasks
This test validates if we can replace the list of tasks in the TaskList
instance successfully. It replaces the task list with a new list containing a single task, checks if the task list reflects the replacement, and updates the status attribute accordingly when the task is marked as completed.
test_create_task_input
This test ensures that we can create a TaskInput
widget successfully. It checks if the initial value of the widget is None
.
test_enter_text_into_task_input
This test verifies the behavior of entering text into the TaskInput
widget. It sets a text value into the input field, checks if a task is created from the entered text, and if the task value matches the entered text. Additionally, it ensures that the input field is cleared after setting the value.
test_can_create_task_list_editor
This test validates if we can create a TaskListEditor
successfully. It initializes a task list with predefined tasks and creates a task list editor from it, ensuring that the editor is correctly instantiated.
Recap
In this tutorial, you have learned how to test Panel apps built using the class-based approach.
We firmly believe that the simplicity and accessibility of testing Panel components underscore a truly invaluable facet of Panel.
Further Learning
To dive deeper into testing Panel apps, explore the Testing How-To Guides and Panel's own tests.