This example demonstrates how to test applications that use the Replane SDK using the built-in InMemoryReplaneClient and create_test_client utilities.
- Python 3.10 or higher
- pytest
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txtpytest -vOr with coverage:
pytest -v --cov=appapp.py- Sample application code that uses Replanetest_app.py- Tests demonstrating various testing patterns
from replane.testing import create_test_client
def test_simple():
client = create_test_client({
"feature_enabled": True,
"rate_limit": 100,
})
assert client.get("feature_enabled") is Truefrom replane.testing import InMemoryReplaneClient
def test_with_overrides():
client = InMemoryReplaneClient()
client.set_config(
"rate_limit",
value=100, # Default
overrides=[{
"name": "premium",
"conditions": [
{"operator": "equals", "property": "plan", "expected": "premium"}
],
"value": 1000,
}],
)
assert client.get("rate_limit", context={"plan": "free"}) == 100
assert client.get("rate_limit", context={"plan": "premium"}) == 1000import pytest
from replane.testing import create_test_client
from myapp import MyService
@pytest.fixture
def replane_client():
return create_test_client({"config": "value"})
def test_service(replane_client):
service = MyService(replane_client)
result = service.do_something()
assert result == expecteddef test_subscriptions():
client = create_test_client()
changes = []
client.subscribe(lambda name, config: changes.append(name))
client.set("feature", True)
assert "feature" in changesQuick way to create a test client with initial values:
replane = create_test_client({
"feature": True,
"limit": 100,
})Full-featured test client with support for:
- Setting simple values:
replane.set("key", value) - Setting configs with overrides:
replane.set_config("key", value, overrides=[...]) - Default context:
InMemoryReplaneClient(context={"env": "test"}) - Subscriptions:
client.subscribe(callback) - Context manager:
with client: ...
- Using
create_test_clientfor simple test cases - Using
InMemoryReplaneClientfor complex scenarios - Testing plan-based overrides
- Testing user targeting
- Testing multiple conditions (AND logic)
- Using pytest fixtures for shared test setup
- Testing subscription callbacks
- Testing default values