Testing Your App
Testing DBOS Functions
Because DBOS workflows, steps, and transactions are ordinary Python functions, you can unit test them using any Python testing framework, like pytest or unittest.
You must reset the DBOS runtime between each test like this:
def reset_dbos():
DBOS.destroy()
config: DBOSConfig = {
"name": "my-app",
"application_version": "0.1.0",
"system_database_url": os.environ.get("TESTING_DATABASE_URL"),
}
DBOS(config=config)
DBOS.reset_system_database(truncate=True)
DBOS.launch()
To minimize dependencies during testing, you may want to use a SQLite system database instead of Postgres.
You can do this by setting a SQLite connection string in reset_dbos, for example:
def reset_dbos():
DBOS.destroy()
config: DBOSConfig = {
"name": "my-app",
"application_version": "0.1.0",
"system_database_url": "sqlite:///my_test_db.sqlite",
}
DBOS(config=config)
DBOS.reset_system_database(truncate=True)
DBOS.launch()
First, destroy any existing DBOS instance.
Then, create and configure a new DBOS instance (you may want to use a different database for testing).
Next, reset the internal state of DBOS, cleaning up any state left over from previous tests.
We recommend passing truncate=True, which empties the DBOS system tables instead of dropping and re-creating the system database, as this is substantially faster.
Finally, launch a new DBOS instance.
For example, if using pytest, declare reset_dbos as a fixture and require it from every test of a DBOS function:
import os
import pytest
from dbos import DBOS, DBOSConfig
@pytest.fixture()
def reset_dbos():
DBOS.destroy()
config: DBOSConfig = {
"name": "my-app",
"application_version": "0.1.0",
"system_database_url": os.environ.get("TESTING_DATABASE_URL"),
}
DBOS(config=config)
DBOS.reset_system_database(truncate=True)
DBOS.launch()
from example_app.main import example_workflow
def test_example_workflow(reset_dbos):
example_input = ...
example_output = ...
assert example_workflow(example_input) == example_output
Mocking
It is often useful in testing to mock your workflows and steps.
Because workflows and steps are just Python functions, they can be mocked using popular mocking libraries like unittest.mock.
For example, the widget store app has a checkout_workflow that creates an order, reserves inventory, waits for a payment notification, then either marks the order paid and starts a dispatch workflow or returns the reserved inventory and cancels the order:
@DBOS.workflow()
def checkout_workflow():
# Create a new order
order_id = ds.run_tx_step({"name": "create_order"}, create_order