Testing your code#
Code that talks to an ERP is hard to test if every test needs a running site.
frappex-client accepts any httpx transport, so your tests can answer requests
themselves, in memory, in microseconds.
A fake site with MockTransport#
import httpx
from frappex_client import Client, TokenAuth
def fake_site(request: httpx.Request) -> httpx.Response:
if request.url.path == "/api/resource/Customer/Acme":
return httpx.Response(200, json={"data": {"name": "Acme", "territory": "India"}})
return httpx.Response(404, json={"exc_type": "DoesNotExistError"})
client = Client(
"https://erp.test",
TokenAuth("test-key", "test-secret"),
transport=httpx.MockTransport(fake_site),
)
assert client.get_doc("Customer", "Acme")["territory"] == "India"
The same transport works for AsyncClient.
Testing your own function#
Say your code has this function:
def overdue_total(client) -> float:
return sum(
row["outstanding_amount"]
for row in client.iter_list(
"Sales Invoice",
fields=["outstanding_amount"],
filters={"status": "Overdue"},
)
)
A pytest test that checks both the result and the request it sent:
import json
import httpx
import pytest
from frappex_client import Client, TokenAuth
@pytest.fixture
def requests_seen():
return []
@pytest.fixture
def client(requests_seen):
def handler(request: httpx.Request) -> httpx.Response:
requests_seen.append(request)
return httpx.Response(
200,
json={
"data": [
{"outstanding_amount": 100.0},
{"outstanding_amount": 250.5},
]
},
)
with Client(
"https://erp.test", TokenAuth("k", "s"), transport=httpx.MockTransport(handler)
) as c:
yield c
def test_overdue_total(client, requests_seen):
assert overdue_total(client) == 350.5
params = requests_seen[0].url.params
assert json.loads(params["filters"]) == {"status": "Overdue"}
Testing error handling#
Return the status Frappe would send, and frappex-client raises the matching exception:
from frappex_client import ValidationError
def rejecting_site(request):
return httpx.Response(
417,
json={
"exc_type": "MandatoryError",
"_server_messages": json.dumps([json.dumps({"message": "Customer Group is required"})]),
},
)
client = Client(
"https://erp.test", TokenAuth("k", "s"), transport=httpx.MockTransport(rejecting_site)
)
with pytest.raises(ValidationError, match="Customer Group is required"):
client.insert("Customer", {"customer_name": "Acme"})
Network failures can be simulated by raising from the handler, for example
raise httpx.ConnectError("down"), which frappex-client turns into TransportError.
Speed up retry tests
Pass retry=RetryPolicy(backoff_base=0) so retries happen without waiting,
or RetryPolicy(max_attempts=1) to turn them off.
Integration tests against a real site#
Mocks prove your logic; only a real site proves the integration. Run a small suite against a disposable site:
- Use a development or throwaway site, never production.
- Give the test user only the roles the tests need.
- Tag everything the tests create with a unique marker and delete it at the end.
- Skip the suite when the site's settings are not configured, so it does not fail on machines without a site.
frappex-client's own integration suite follows exactly this pattern and runs in CI against fresh Frappe 14, 15 and 16 sites; its test files are a good starting point.