How to Test Your Dash App in Python
Tags: Pytest, Programming, Web App Development, Python, Dash

Testing a Dash app with pytest and selenium

A Simpler Way to Test Dash Apps

The recommended way of testing a Dash app, as specified in the documentation, is to instantiate your app within a test function.

How the Dash documentation suggests you write tests

This is kind of a pain because it requires you to rewrite your whole app within the test function!

But there is a simpler way…

Using Import App

First of all, credit goes to takaakifuruse of ploty forum fame for this very elegant solution.

To do this, you use the import_app module from dash.testing.application_runners . This way you don’t have to rewrite the whole app and can instead just import it into your test function.

But before you do this, there are a few steps you need to take first:

Make a new directory! I called mine dash_test. In this directory, create an empty __init__.py file, an app.py file into which you copy your whole app, and a test_one.py file we’ll fill in later. Run pip install "dash[testing]" and pip install pytest . Then you need to install a webdriver since dash.testing uses selenium to test if your app loads correctly. This can be kind of fiddly, so here’s how to do it on a mac… Download the mac version of the webdriver from this site; at the time of writing it had to be version 84. Then move it to the usr/local/bin directory so it’s on path.

Write your Test

Now it’s time to write your actual test…

Here’s the code I wrote for this, which I’ll go through blow by blow.

A test file for a dash app

First, your import the import_app function to make your life much easier. Then you write a custom test function, the name of which must be in a pytest test case id format — this is an abbreviation in the pattern of mmffdd where m stands for module, f for file, and d for three digits which convey the number of your test case.

I don’t think this format maps on that well to a Dash app, so for some extra clarity in the example above I wrote bb for Black Lives Matter which is the theme of my app, aaa for App since the file name I was testing was app.py and 001 since it was the first test.

Next you write dash_duo.start_server(app) to fire up a selenium instance which will load your dash app in a browser session; this way you can check if it’s all working well. Pretty nifty! If you’re app is already hosted, then you can instead write dash_br.server_url = "Hosted URL" .

After this you need to specify some text and its corresponding html tag; this makes sure you don’t start testing the app until its finished loading properly.

Once this is done you can write asserts to test specific expectations and values you’re interested in, as in my two examples above.

How to Run Your Test

To let it rip, make sure you’re in the root directory of your Dash project and then type python -m pytest dash_test/ or whatever you have called the name of your dash testing directory.

You may need to install a few more pytest dependencies for this to work on your machine; hopefully this should be fairly straightforward if you followed the steps above to get everything set up right.

Python In Plain English

Did you know that we have three publications and a YouTube channel? Find links to everything at plainenglish.io!