From e85df155403e1a5dc262e00e6a97270f25db6a0f Mon Sep 17 00:00:00 2001 From: Ben Hauser Date: Sat, 9 May 2020 13:23:11 +0400 Subject: [PATCH] feat: expose global namespace in pytest interactive console --- brownie/test/managers/runner.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/brownie/test/managers/runner.py b/brownie/test/managers/runner.py index 7db88f62d..672bad855 100644 --- a/brownie/test/managers/runner.py +++ b/brownie/test/managers/runner.py @@ -238,11 +238,26 @@ def pytest_exception_interact(self, report, call): tw = TerminalWriter() report.longrepr.toterminal(tw) + # get global namespace + globals_dict = call.excinfo.traceback[-1].frame.f_globals + + # filter python internals and pytest internals + globals_dict = {k: v for k, v in globals_dict.items() if not k.startswith("__")} + globals_dict = {k: v for k, v in globals_dict.items() if not k.startswith("@")} + + # filter test functions + test_names = self.node_map[report.location[0]] + globals_dict = {k: v for k, v in globals_dict.items() if k not in test_names} + + # get local namespace locals_dict = call.excinfo.traceback[-1].locals locals_dict = {k: v for k, v in locals_dict.items() if not k.startswith("@")} + + namespace = {"_callinfo": call, **globals_dict, **locals_dict} + try: CONFIG.argv["cli"] = "console" - shell = Console(self.project, extra_locals={"_callinfo": call, **locals_dict}) + shell = Console(self.project, extra_locals=namespace) shell.interact( banner=f"\nInteractive mode enabled. Use quit() to continue running tests.", exitmsg="",