From 052a9fff05c926db10f964a073a2f0a07da544bd Mon Sep 17 00:00:00 2001 From: Chip Kent <5250374+chipkent@users.noreply.github.com> Date: Fri, 23 Oct 2026 23:53:56 -0600 Subject: [PATCH] test: stop pook mocks leaking between REST test classes BaseTest turned pook on and registered its mocks in setUpClass but never turned pook off, so every class's mocks stayed active for the rest of the run. That hid a problem in two tests. test_list_stocks_dividends_ticker_any_of and test_get_snapshot_indices each send the same request twice, but each mock answers only once. Their second request was answered by a copy that an earlier class had registered, so both fail with PookNoMatches when their module runs on its own. Turn pook off in tearDownClass. Let a test class name the mocks that must answer more than once, and register those with persist. Other mocks still answer once, because the pagination tests rely on it: a first-page mock also matches the request for the next page. --- test_rest/base.py | 17 ++++++++++++++++- test_rest/test_dividends.py | 3 +++ test_rest/test_snapshots.py | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/test_rest/base.py b/test_rest/base.py index 40854146..41b652d4 100644 --- a/test_rest/base.py +++ b/test_rest/base.py @@ -3,6 +3,7 @@ import os import re import unittest +from typing import Set import pook # type: ignore # mocks are stored in file tree @@ -32,6 +33,10 @@ class BaseTest(unittest.TestCase): + # mocks that answer more than once, for tests that repeat a request; + # others answer once, which the pagination tests rely on + persistent_mocks: Set[str] = set() + @classmethod def setUpClass(cls): cls.maxDiff = None @@ -40,4 +45,14 @@ def setUpClass(cls): for m in mocks: url = cls.c.BASE + m[0] # print("register", url) - pook.get(url, reply=200, response_body=m[1]) + pook.get( + url, + reply=200, + response_body=m[1], + persist=m[0] in cls.persistent_mocks, + ) + + @classmethod + def tearDownClass(cls): + # stop pook and drop the mocks, so they don't leak into later classes + pook.off() diff --git a/test_rest/test_dividends.py b/test_rest/test_dividends.py index 7b6575d0..8549df9d 100644 --- a/test_rest/test_dividends.py +++ b/test_rest/test_dividends.py @@ -3,6 +3,9 @@ class DividendsTest(BaseTest): + # test_list_stocks_dividends_ticker_any_of requests this twice + persistent_mocks = {"/stocks/v1/dividends?ticker.any_of=AAPL%2CVZ"} + def test_list_dividends(self): dividends = [d for d in self.c.list_dividends()] expected = [ diff --git a/test_rest/test_snapshots.py b/test_rest/test_snapshots.py index 3ead4e15..4472970a 100644 --- a/test_rest/test_snapshots.py +++ b/test_rest/test_snapshots.py @@ -26,6 +26,9 @@ class SnapshotsTest(BaseTest): + # test_get_snapshot_indices requests this twice + persistent_mocks = {"/v3/snapshot/indices?ticker.any_of=SPX%2CAPx%2CAPy"} + def test_list_universal_snapshots(self): expected = [ UniversalSnapshot(