- Introduction
- How to run the tool
- Test file
- Examples and templates
- Generative AI usage statement
- More documentation
This tool provides a Python library for property-based testing of Daml
contracts, executing them via the Daml JSON API.
The daml_pbt module integrates with Hypothesis (data generation and
shrinking) and pytest (execution and reporting), allowing you to validate
invariants, pre/postconditions, and stateful workflows with
automatically generated examples and reproducible counterexamples.
Key features
- Helpers to create contracts, exercise choices, and run queries.
- Automatic input generation with Hypothesis and counterexample shrinking.
- Per-test party isolation (prevents interference between cases).
- Integration with pytest to run locally and in continuous integration (CI).
What you’ll find in this repository
- The
daml_pbtmodule with the helpers. - Concrete examples of Daml contracts with reference properties.
- Reusable templates to quickly bootstrap new tests.
Prerequisites
- Daml SDK running locally (Sandbox and JSON API).
- Python 3.x with
pytest,hypothesis, andrequests. - A DAR file for your Daml project.
For all commands, be in the Daml project folder (where daml.yaml is).
If you don’t have a virtual environment yet, run:
python3 -m venv .venv
source .venv/bin/activate
pip install -U pip pytest hypothesis requestsIf you don’t have a DAR file yet, run:
daml buildYou’ll need 3 terminals to run the tests.
Run:
daml sandbox --port 6865Run:
daml json-api --ledger-host localhost --ledger-port 6865 --http-port 7575Upload the DAR:
daml ledger upload-dar --host localhost --port 6865 .daml/dist/TestBankTemplate-1.0.0.darAfter ensuring the virtual environment exists (first step), run:
source .venv/bin/activateTo run a single test:
pytest -q -s tests/test_bank_template.py::test_deposit_increases_balanceTo run all tests in the file:
pytest -q -s tests/test_bank_template.pyThere are some example test files in this repo but here are two quick tips.
How to import the library:
from daml_pbt import make_request, make_auth, make_admin_auth, ensure_ok, allocate_party, allocate_unique_partyYou need to obtain the package_id. Run:
daml damlc inspect-dar .daml/dist/TestBankTemplate-1.0.0.darYou’ll see something like in the output:
DAR archive contains the following packages: TestBankTemplate-1.0.0-c6f004b1cd672ae532964d33767186c66d1b0673ce87a0e05b35e7b78c2fc514 "c6f004b1cd672ae532964d33767186c66d1b0673ce87a0e05b35e7b78c2fc514"
The constant PKG is the value inside quotes:
PKG = "c6f004b1cd672ae532964d33767186c66d1b0673ce87a0e05b35e7b78c2fc514"There are 8 concrete examples in this repository, located at:
miguel-valido-repo/benchmark/daml_contracts/
-
AssetTransfer Buy/sell workflow with states (e.g.,
Active,OfferPlaced,PendingInspection,Inspected,Accepted/Rejected) and roles (owner, buyer, inspector, appraiser). Properties: valid transitions, accept/reject guards, role-based permissions. -
BorrowAndLending Loan pool: deposit (lend), withdraw, borrow, and repay. Properties:
Lendaccumulates balances;Withdrawrespects available balance;Borrow/Repayround-trip restores state correctly. -
DefectiveComponentCounter Counting defective components with manufacturer-only permissions. Properties:
ComputeTotalpreserves the sum; only the manufacturer is authorized. -
DigitalLocker Digital locker for sharing documents with requests and revocations. Properties:
UploadDocumentssets fields;Request→Accept→Releasecycle;Share→Revokeround-trip restores state correctly. -
FrequentFlier Frequent-flier miles program with accrual and reward rules. Properties:
AddMilesupdates miles/rewards; only the flier is authorized. -
SimpleMarket Simple marketplace with offer/accept/reject and state changes. Properties:
MakeOfferonly fromItemAvailable;MakeOffermoves toOfferPlaced; only the owner may accept/reject; guards enforced. -
WhitelistedRegistry Registry with an owner and a whitelist of authorized parties. Properties: only the owner may change owner/whitelist;
SetWhitelistedtoggles membership;IsWhitelistedreflects the actual state. -
ZeroTokenBank Minimal “bank” without a native token: open account, deposit, withdraw, check balance. Properties: deposit increases balance correctly; withdraw is forbidden at zero balance.
There are 3 templates, available at:
miguel-valido-repo/benchmark/daml_contracts/templates
-
BorrowLendingTemplate Base for lending pools:
Lend,Withdraw,Borrow,Repay, per-party balances, and limit validations. Useful to test accounting invariants, collateral rules, and borrow/repay round-trips. -
WhitelistedRegistryTemplate Access-control pattern with owner and whitelist:
SetWhitelisted(toggle),IsWhitelisted(query), and owner transfer. A base for scenarios where authorizations and roles change over time. -
ZeroTokenBankTemplate Minimal custodial “bank”: open account, deposit, withdraw, query. Ideal for simple invariants (balance never negative, consistent sum of balances) and basic permission tests.
Tip: each template and concrete example includes a small starter set of properties in its corresponding
tests/directory, which you can use as a baseline.
I used generative AI tools to help implement and execute the property-based tests. All AI-suggested content was reviewed, adapted, and validated before being included. Responsibility for the properties, tests, and code is entirely mine.
For further detail please refer to the Smart Tests for Smart Contract Languages.pdf, the author’s Master’s thesis.