Server Testing

You can write tests for your server-side functions inside the Anvil Editor using pytest.

To quickly get started with writing and running server tests, check out our quickstart guide:

Adding server tests to an app

To add server tests to your app, click the blue plus button + in the Sidebar Menu and choose “Testing”.

Add testing to your app from the Sidebar Menu

Add testing to your app from the Sidebar Menu

This will add a requirements file for your server tests and an example file to your app. The requirements.txt file already has pytest added, which is needed for your tests to run. You can add any other Python packages you need for testing here. The test_examples.py file includes a couple example tests to help you get started.

Adding test files

To add a new test file to your app, click the three dots menu next to the blue “Run tests” button in the Server Tests navigation panel. Here you can add new files and directories.

Add new test files
from the three dots menu

Add new test files
from the three dots menu

You can add as many test files as you need for your app, but they must be named test_<file-name>.py or <file-name>_test.py.

Writing server tests

To test functions written in a Server Module, you need to first import the functions in your test file:

Python
# in a test .py file
from AppName.ServerModule1 import example_func

You can then write tests for the imported functions.

Example: writing a simple test

Let’s write a simple test for a function that calculates the area of a triangle. The following function lives in a Server Module:

Python | Server
# in ServerModule1
def calculate_triangle_area(base, height):
    return (base*height)/2

In a test file, we import the function and write a test to check that calculate_triangle_area returns the correct answer. A triangle with a base of 3 and a height of 2 should have an area of 3. We can write a test that checks if running calculate_triangle_area with 3 and 2 as arguments returns 3:

Python
#in a test .py file
from MyApp.ServerModule1 import calculate_triangle_area

def test_calculate_triangle_area():
    assert calculate_triangle_area(3, 2) == 3

Running tests from the Anvil Editor

You can run all server tests in your app, run all server tests in a specific file or just run an individual test. You can also run tests only if they match a specified string expression.

You can also run tests from the command line on your own computer using the Anvil CLI.

Running all tests

You can run all server tests from the Anvil Editor in two ways:

  • From the Server Tests navigation panel: Click the blue “ Run tests” button to run all tests in all testing files. This opens the Server Tests tab in the Bottom Panel.
  • From the Server Tests tab in the Bottom Panel: Click the green play button to run all tests in all files.

Running all tests in a specific test file

From the Server Tests navigation panel, click the three dots next to the name of the test file then click “Run tests in file” to run all tests in that specific file.

You can run all the tests in a specific file
from that file&rsquo;s three dots menu

You can run all the tests in a specific file
from that file’s three dots menu

Running an individual test

There are a couple ways to run a test on its own:

  • From the .py file: Click the green play button next to the test definition of the test you want to run.
  • From the Server Tests tab in the Bottom Panel: Click the “All tests” tab to show all available tests. Hover over the test you’d like to run and click the play button
If you haven’t run all tests since adding a new test, you’ll need to “discover” new tests before you can run the test individually. Click the three dots menu in the Server Tests navigation panel and select “Discover all server tests”. This will open the Server Tests tab in the Bottom Panel where the new test will be listed. There will now be a green play button next to the new test function.

Checking output

After running server tests, the output from the run will appear in the Server Tests tab in the Bottom Panel. Passed tests will have a green check mark next to the test name whereas failed tests will have a red x and skipped tests will have a grey dash. You can find out more about test output in the section on the Server Tests tab.

The Server Tests tab

Information about the latest test run will be shown in the Server Tests tab in the Bottom Panel.

The Server Tests tab shows information about the latest test run

The Server Tests tab shows information about the latest test run

Run buttons

At the top of the Server Tests tab, there are four icon buttons: run all server tests, re-run the last test run, re-run the failed tests, and clear the test results. After running tests, a status will appear next to the run buttons indicating if the tests passed or if any failed.

The run buttons give you options
for running tests and clearing the test results

The run buttons give you options
for running tests and clearing the test results

Test list

On the left side of the Server Tests tab is the list of tests. There are three buttons at the top of the list that let you toggle between all tests, the latest test run, and the tests that failed the last run. Undiscovered tests will not appear here. See Running an individual test for more information on discovering tests.

Toggle between all tests, the latest test run
and failed tests in the test list.

Toggle between all tests, the latest test run
and failed tests in the test list.

Each test in the list has a colored icon next to the test name. A green check indicates the test passed, a red x indicates the test failed and a grey dash indicates the test was skipped. Hovering over a test reveals two icons. Clicking the icon lets you jump to the test definition in the code and clicking the icon runs that specific test.

Hover over a test in the list to jump to the test definition or run that test

Hover over a test in the list to jump to the test definition or run that test

Results pane

To the right of the test list, you’ll find the results pane with the output from the latest test run.

Run Summary

After you run tests, the results pane will show a summary of the run including:

  • Results: how many tests passed, failed, were skipped or experienced an error
  • Run details: how long the run took, the pytest exit code, the version of pytest used and the arguments passed to the pytest command.
  • Test environment: the status of the server environment used for testing
  • pytest output: The output produced by pytest. Clicking the “App log” button will open the Background Task log for the test run and show the full pytest output.
The run summary of the latest test run

The run summary of the latest test run

Test output

Clicking on an individual test will display any output from the test as well as if the test passed or failed and how long it ran for. You can click “Run summary” to return to the run summary.

The output from the selected test is shown in the results pane.

The output from the selected test is shown in the results pane.

pytest -k expression

Above the results pane, there is a text box where you can add a string expression that will be passed to the pytest command as its -k argument. This argument allows you to only run the tests that contain names that match the string expression.

Adding a string expression as a -k argument

Adding a string expression as a -k argument

Test Environments

Data Tables

When you run tests, each test run executes in its own temporary Deployment Environment, with its own temporary Data Tables database. This means that tests will not see your production data, and tests may update Data Tables without affecting your production data.

App Secrets

If you want to use testing values for your App Secrets, you can provide override values that will be visible when your app is being tested.

Any override value you provide for testing can be accessed by Anvil’s agents. This is because an agent can write arbitrary test code, run the tests and view their output without additional permissions. Therefore, any value provided to test code is marked in the Anvil Editor as “visible to agents”.


Do you still have questions?

Our Community Forum is full of helpful information and Anvil experts.