Python SDK
Testing your Python integration
watchtower15.testing holds fakes for your tests. They hold only placeholder credentials, never sleep, and never reach a host.
from watchtower15.testing import FakeClient, MockTransport
def test_reports_critical_servers() -> None: client = FakeClient() client.queue("public.servers.list", MockTransport.envelope([{"uuid": "u1", "name": "web-1"}])) report_critical_servers(client) client.assert_called("public.servers.list", times=1)FakeClientis aClient, andAsyncFakeClientanAsyncClient. Ingest pushes are answered with an accepted envelope; any other operation is answered fromqueue(operation_id, answer), else the call raises naming the operation.calls(),assert_called(operation_id, where=None, times=None),assert_not_called(),pushed_points(),pushed_lines(),assert_pushed(kind=None)andassert_nothing_pushed()read what happened.MockTransportis the transport behind them (AsyncMockTransportfor the async one):append(), and the buildersjson(),problem(),envelope(),metrics_accepted()andlogs_accepted(). Recorded requests keep theAuthorizationmasked and the body decoded.FakeAggregatorrecordsincrements()andgauges()(assert_incremented(),assert_gauge_set(),assert_nothing_recorded()); itsflush()is always final, into itsFakeClient, so the open minute reachesclient.pushed_points()with no waiting.FakeClockis a clock youadvance().
A failed assertion raises AssertionError, which pytest and unittest both report with the recorded calls in the message. The integrations accept fake=True to build these fakes for you: see each
framework’s page.
A pytest fixture
Section titled “A pytest fixture”import pytestfrom watchtower15.testing import FakeAggregator, FakeClient
@pytest.fixturedef wt15() -> FakeClient: return FakeClient()
@pytest.fixturedef metrics(wt15: FakeClient) -> FakeAggregator: return FakeAggregator(wt15)Pass wt15 and metrics to the code under test in place of the real ones, then assert: metrics.assert_incremented("orders.created", times=1).