Advanced Server Testing Options

This page explains less common testing patterns including testing functions that send emails, testing functions that use App Secrets as well as using the Anvil CLI to run tests from your local terminal.

Testing email

If you write any tests that send emails, the emails will not actually be sent to the recipient. You can capture the email that would be sent in a with capture_emails() context handler to check the email.

Example: Testing a function that sends an email

Let’s now write a simple test for a function that sends an email. The following function lives in a Server Module:

Python | Server
# In a Server Module

def send_email(recipient, body):
    anvil.email.send(to=recipient, subject="Hey there!", text=body)

Now we can write a test for the send_email function that verifies the subject line is what we want it to be. The test will capture the email that would have been sent using capture_emails() and check its subject argument.

Python
# In a test .py file
from anvil.testing.email import capture_emails

def test_send_email():
    with capture_emails() as emails:
        send_email(recipient="brooke@anvil.works", body="this is a test")

    email = emails[0]
    assert email.subject == "Hey there!"

Now if we run the test, we should see that it passes.

Testing with App Secrets

By default, tests cannot read the values of your App Secrets. If you are writing tests that read an App Secret, you can allow tests to be able to read the value of that secret or create a testing-specific value. This way, your tests can run without accessing the actual value of your secret.

When setting or changing the value of a secret, check the “Allow agents and tests to read this value” box to allow tests to read the value of the secret.

Allowing tests to read the value of a secret

Allowing tests to read the value of a secret

To add a testing-specific value for an App Secret, click the three dots menu next to the secret and select “Override when running tests”. This will allow you to set a new secret value readable by tests.

Create a testing-specific value for your App Secret

Create a testing-specific value for your App Secret

An App Secret with a different value for testing purposes

An App Secret with a different value for testing purposes

Running tests from your local terminal

You can also use the Anvil CLI to run tests from your command line if you have your Anvil app checked out locally.

Before running tests, you need to make sure any local changes are synchronised with Anvil. We suggest running anvil watch in a separate terminal window to keep your local app automatically in sync.

After running a test with the Anvil CLI, the pytest output will be printed to your terminal. The command exits with pytest’s exit code, for example 0 if every test passed and 1 if any failed.

Running all tests

To run all of your app’s tests with the Anvil CLI, run anvil test.

Bash
> anvil test
============================= test session starts ==============================
platform linux -- Python 3.14.10, pytest-9.2.0, pluggy-1.7.0
rootdir: /tmp/anvil-server-tests-pql8i5ux
plugins: platformdirs-4.13.5
collected 3 items

server_tests/test_examples.py ...                                        [100%]

============================== 3 passed in 0.11s ===============================

Running a specific test

To run a specific test, you need to specify the path to the test, relative to the current directory. Server tests are found in <AppName>/tests/server_tests

Bash
> anvil test tests/server_tests/test_examples.py::test_send_email
============================= test session starts ==============================
platform linux -- Python 3.14.10, pytest-9.2.0, pluggy-1.7.0
rootdir: /tmp/anvil-server-tests-3cnirhsw
plugins: platformdirs-4.13.5
collected 3 items / 2 deselected / 1 selected

server_tests/test_examples.py .                                          [100%]

======================= 1 passed, 2 deselected in 0.09s ========================

For more information on the anvil test command, see the documentation on Anvil CLI commands.


Do you still have questions?

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