Test UI rendering
This guide addresses how to test the UI with Pytest and Playwright.
Testing is key to developing robust and performant applications. Particularly when you build complex UIs you will want to ensure that it behaves as expected. Unit tests will allow you test that the logic on the backend behaves correctly, but it is also useful to test that the UI is rendered correctly and responds appropriately.
For testing the UI we recommend the framework Playwright. Panel itself is tested with this framework.
Before you get started ensure you have installed the required dependencies:
and ensure playwright
sets up the browsers it will use to display the applications:
Create the app
Let's create a simple data app for testing. The app sleeps 0.5 seconds (default) when loaded and when the button is clicked.
Create the file app.py
and add the code below (don't worry about the contents of the app for now):
:::{card} app.py
:::
Serve the app via panel serve app.py
and open http://localhost:5006/app in your browser to see what it does.
Create a conftest.py
The conftest.py
file should be placed alongside your tests and will be loaded automatically by pytest. It is often used to declare fixtures that allow you declare reusable components. It will:
provide us with an available
port
.clean up the Panel state after each test.
Create the file conftest.py
and add the code below.
:::{card} conftest.py
:::
For more inspiration see the Panel conftest.py
file
Test the app UI
Now let us actually set up some UI tests, we will want to assert that the app:
Responds when we make an initial request
Renders a Run button
Updates as expected when the Run button is clicked
Create the file test_app_frontend.py
and add the code below.
:::{card} test_app_frontend.py
:::
Let's run pytest
. We will add the --headed
and --slowmo
arguments to see what is going on in the browser. This is very illustrative and also helpful for debugging purposes.
Record the test code
Writing code to test complex UIs can be quite cumbersome, thankfully there is an easier way. Playwright allows you to record UI interactions as you navigate your live app and translates these interactions as code (see the Playwright codegen documentation for more detail).
You can try it yourself by launching the app again:
and starting the Playwright recorder: