Skip to content

Commit 6b71241

Browse files
committed
Refactor TestFramework main() into setup/shutdown
Setup and shutdown code now moved into dedicated methods. Test "success" is added as a BitcoinTestFramework member, which can be accessed outside of main. Argument parsing also moved into separate method and called from main.
1 parent ede8b76 commit 6b71241

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

test/functional/test_framework/test_framework.py

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,34 @@ def __init__(self):
105105
def main(self):
106106
"""Main function. This should not be overridden by the subclass test scripts."""
107107

108+
self.parse_args()
109+
110+
try:
111+
self.setup()
112+
self.run_test()
113+
except JSONRPCException:
114+
self.log.exception("JSONRPC error")
115+
self.success = TestStatus.FAILED
116+
except SkipTest as e:
117+
self.log.warning("Test Skipped: %s" % e.message)
118+
self.success = TestStatus.SKIPPED
119+
except AssertionError:
120+
self.log.exception("Assertion failed")
121+
self.success = TestStatus.FAILED
122+
except KeyError:
123+
self.log.exception("Key error")
124+
self.success = TestStatus.FAILED
125+
except Exception:
126+
self.log.exception("Unexpected exception caught during testing")
127+
self.success = TestStatus.FAILED
128+
except KeyboardInterrupt:
129+
self.log.warning("Exiting after keyboard interrupt")
130+
self.success = TestStatus.FAILED
131+
finally:
132+
exit_code = self.shutdown()
133+
sys.exit(exit_code)
134+
135+
def parse_args(self):
108136
parser = argparse.ArgumentParser(usage="%(prog)s [options]")
109137
parser.add_argument("--nocleanup", dest="nocleanup", default=False, action="store_true",
110138
help="Leave bitcoinds and test.* datadir on exit or error")
@@ -135,6 +163,9 @@ def main(self):
135163
self.add_options(parser)
136164
self.options = parser.parse_args()
137165

166+
def setup(self):
167+
"""Call this method to start up the test framework object with options set."""
168+
138169
PortSeed.n = self.options.port_seed
139170

140171
check_json_precision()
@@ -181,33 +212,20 @@ def main(self):
181212
self.network_thread = NetworkThread()
182213
self.network_thread.start()
183214

184-
success = TestStatus.FAILED
215+
if self.options.usecli:
216+
if not self.supports_cli:
217+
raise SkipTest("--usecli specified but test does not support using CLI")
218+
self.skip_if_no_cli()
219+
self.skip_test_if_missing_module()
220+
self.setup_chain()
221+
self.setup_network()
185222

186-
try:
187-
if self.options.usecli:
188-
if not self.supports_cli:
189-
raise SkipTest("--usecli specified but test does not support using CLI")
190-
self.skip_if_no_cli()
191-
self.skip_test_if_missing_module()
192-
self.setup_chain()
193-
self.setup_network()
194-
self.run_test()
195-
success = TestStatus.PASSED
196-
except JSONRPCException:
197-
self.log.exception("JSONRPC error")
198-
except SkipTest as e:
199-
self.log.warning("Test Skipped: %s" % e.message)
200-
success = TestStatus.SKIPPED
201-
except AssertionError:
202-
self.log.exception("Assertion failed")
203-
except KeyError:
204-
self.log.exception("Key error")
205-
except Exception:
206-
self.log.exception("Unexpected exception caught during testing")
207-
except KeyboardInterrupt:
208-
self.log.warning("Exiting after keyboard interrupt")
223+
self.success = TestStatus.PASSED
224+
225+
def shutdown(self):
226+
"""Call this method to shut down the test framework object."""
209227

210-
if success == TestStatus.FAILED and self.options.pdbonfailure:
228+
if self.success == TestStatus.FAILED and self.options.pdbonfailure:
211229
print("Testcase failed. Attaching python debugger. Enter ? for help")
212230
pdb.set_trace()
213231

@@ -225,7 +243,7 @@ def main(self):
225243
should_clean_up = (
226244
not self.options.nocleanup and
227245
not self.options.noshutdown and
228-
success != TestStatus.FAILED and
246+
self.success != TestStatus.FAILED and
229247
not self.options.perf
230248
)
231249
if should_clean_up:
@@ -238,10 +256,10 @@ def main(self):
238256
self.log.warning("Not cleaning up dir {}".format(self.options.tmpdir))
239257
cleanup_tree_on_exit = False
240258

241-
if success == TestStatus.PASSED:
259+
if self.success == TestStatus.PASSED:
242260
self.log.info("Tests successful")
243261
exit_code = TEST_EXIT_PASSED
244-
elif success == TestStatus.SKIPPED:
262+
elif self.success == TestStatus.SKIPPED:
245263
self.log.info("Test skipped")
246264
exit_code = TEST_EXIT_SKIPPED
247265
else:
@@ -251,7 +269,7 @@ def main(self):
251269
logging.shutdown()
252270
if cleanup_tree_on_exit:
253271
shutil.rmtree(self.options.tmpdir)
254-
sys.exit(exit_code)
272+
return exit_code
255273

256274
# Methods to override in subclass test scripts.
257275
def set_test_params(self):

0 commit comments

Comments
 (0)