test: avoid flaky behaviour on test ordering

If a test clears local.response and another test depends on it being
fresh inited then tests can fail.

E.g. response.docs might not exist.
This commit is contained in:
Ankush Menat 2025-01-04 11:02:54 +05:30
parent 13304cd36d
commit 47af97661a
3 changed files with 14 additions and 16 deletions

View file

@ -24,7 +24,7 @@ if click_ctx:
TEST_WEIGHT_OVERRIDES = { TEST_WEIGHT_OVERRIDES = {
# XXX: command tests are significantly overweight, need a better heuristic than test count # XXX: command tests are significantly overweight, need a better heuristic than test count
# Possible better solution: stats from previous test runs. # Possible better solution: stats from previous test runs.
"test_commands.py": 20, "test_commands.py": 10,
} }

View file

@ -63,7 +63,7 @@ class IntegrationTestCase(UnitTestCase):
frappe.db.before_commit.add(_commit_watcher) frappe.db.before_commit.add(_commit_watcher)
# enqueue teardown actions (executed in LIFO order) # enqueue teardown actions (executed in LIFO order)
cls.addClassCleanup(_restore_thread_locals, copy.deepcopy(frappe.local.flags)) cls.addClassCleanup(_restore_ctx_locals, copy.deepcopy(frappe.local.flags))
cls.addClassCleanup(_rollback_db) cls.addClassCleanup(_rollback_db)
cls._integration_test_case_class_setup_done = True cls._integration_test_case_class_setup_done = True
@ -183,12 +183,13 @@ def _rollback_db():
frappe.db.rollback() frappe.db.rollback()
def _restore_thread_locals(flags): def _restore_ctx_locals(flags):
frappe.local.flags = flags frappe.local.flags = flags
frappe.local.error_log = [] frappe.local.error_log = []
frappe.local.message_log = [] frappe.local.message_log = []
frappe.local.debug_log = [] frappe.local.debug_log = []
frappe.local.conf = frappe._dict(frappe.get_site_config()) frappe.local.conf = frappe._dict(frappe.get_site_config())
frappe.local.response = frappe._dict({"docs": []})
frappe.local.cache = {} frappe.local.cache = {}
frappe.local.lang = "en" frappe.local.lang = "en"
frappe.local.preload_assets = {"style": [], "script": [], "icons": []} frappe.local.preload_assets = {"style": [], "script": [], "icons": []}

View file

@ -74,19 +74,16 @@ class TestClient(IntegrationTestCase):
def test_run_doc_method(self): def test_run_doc_method(self):
from frappe.handler import execute_cmd from frappe.handler import execute_cmd
if not frappe.db.exists("Report", "Test Run Doc Method"): report = frappe.get_doc(
report = frappe.get_doc( {
{ "doctype": "Report",
"doctype": "Report", "ref_doctype": "User",
"ref_doctype": "User", "report_name": frappe.generate_hash(),
"report_name": "Test Run Doc Method", "report_type": "Query Report",
"report_type": "Query Report", "is_standard": "No",
"is_standard": "No", "roles": [{"role": "System Manager"}],
"roles": [{"role": "System Manager"}], }
} ).insert()
).insert()
else:
report = frappe.get_doc("Report", "Test Run Doc Method")
frappe.local.request = frappe._dict() frappe.local.request = frappe._dict()
frappe.local.request.method = "GET" frappe.local.request.method = "GET"