Developer Reference

Python Testing

Step-by-step guide for setting up your environment and running tests after every git pull.

Python · pytest
AWS Secrets
Local Stage
01

Prerequisites

💡
Virtual environment check: Ensure (venv) appears as a prefix in your terminal prompt before proceeding. If it doesn't, run the activation command below from your project root.
bash
# Run from project root directory
source venv/Scripts/activate
02

Setup Steps

01
Pull Environment Variables
Fetch secrets from AWS Secrets Manager and write them to your local env file.
bash
aws secretsmanager get-secret-value \
    --secret-id local_master_secret \
  | jq -r '.SecretString' \
  | jq 'to_entries' \
  | jq -r 'map("\(.key)=\(.value|@sh)")[]' \
  > src/environments/.env.local
02
Navigate to Source Directory
bash
cd src
03
Install Dev Dependencies
bash
pip install -r requirements/requirements.dev.txt
04
Install Test Dependencies
bash
pip install -r requirements/requirements.test.txt
03

Running Tests

Base Command
bash
export STAGE='local' && python -m pytest {path/to/test_file.py} -vv
Flag Reference
FlagDescription
STAGE='local'Environment flag — use local for local development runs
-vvVerbose output — prints each test name and pass/fail result
-sDisables stdout/stderr capture; use when you need to see print() output
{path}Relative path to the target test file from the src/ directory
04

Example Commands

Example 1
Order Eligibility Use-Case Test
bash
export STAGE="local" && python -m pytest -s \
    src/use_cases/orders/verify_order_eligibility_use_case_test.py
Example 2
Encryption Service Test
bash
export STAGE='local' && python -m pytest \
    tests/common/infrastructure/frameworks/encryption_service_test.py -vv
05

Local Debugging

Interactive Python Interpreter
bash
# Launch interpreter
py

# Exit interpreter
exit()
06

JSON Dumps

📂
Insert these snippets inside test methods to write repository data to disk for inspection. Add output files (invoice_data.json, payments.json, orders_data.json) to .gitignore to avoid accidental commits.
Payment Data
python
with open("payments.json", "w") as f:
    json.dump(
        [r.to_dict() for r in self.payment_repo.get_payments()], f
    )
Invoice Data
python
test_invoice_data = self.invoice_repo.get_invoices_as_dict().get("invoice_data")

with open("invoice_data.json", "w") as f:
    from json import dump
    dump(test_invoice_data, f)
Order Item Data · Future Reference
python
# Use while order_item testing
with open("orders_data.json", "w") as f:
    from json import dump
    dump(self.order_repo.get_orders_as_dict(), f)