Skip to content

Commit

Permalink
Refactor TestFramework main() into setup/shutdown
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jachiang committed Nov 3, 2019
1 parent ede8b76 commit 6b71241
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions test/functional/test_framework/test_framework.py
Expand Up @@ -105,6 +105,34 @@ def __init__(self):
def main(self):
"""Main function. This should not be overridden by the subclass test scripts."""

self.parse_args()

try:
self.setup()
self.run_test()
except JSONRPCException:
self.log.exception("JSONRPC error")
self.success = TestStatus.FAILED
except SkipTest as e:
self.log.warning("Test Skipped: %s" % e.message)
self.success = TestStatus.SKIPPED
except AssertionError:
self.log.exception("Assertion failed")
self.success = TestStatus.FAILED
except KeyError:
self.log.exception("Key error")
self.success = TestStatus.FAILED
except Exception:
self.log.exception("Unexpected exception caught during testing")
self.success = TestStatus.FAILED
except KeyboardInterrupt:
self.log.warning("Exiting after keyboard interrupt")
self.success = TestStatus.FAILED
finally:
exit_code = self.shutdown()
sys.exit(exit_code)

def parse_args(self):
parser = argparse.ArgumentParser(usage="%(prog)s [options]")
parser.add_argument("--nocleanup", dest="nocleanup", default=False, action="store_true",
help="Leave bitcoinds and test.* datadir on exit or error")
Expand Down Expand Up @@ -135,6 +163,9 @@ def main(self):
self.add_options(parser)
self.options = parser.parse_args()

def setup(self):
"""Call this method to start up the test framework object with options set."""

PortSeed.n = self.options.port_seed

check_json_precision()
Expand Down Expand Up @@ -181,33 +212,20 @@ def main(self):
self.network_thread = NetworkThread()
self.network_thread.start()

success = TestStatus.FAILED
if self.options.usecli:
if not self.supports_cli:
raise SkipTest("--usecli specified but test does not support using CLI")
self.skip_if_no_cli()
self.skip_test_if_missing_module()
self.setup_chain()
self.setup_network()

try:
if self.options.usecli:
if not self.supports_cli:
raise SkipTest("--usecli specified but test does not support using CLI")
self.skip_if_no_cli()
self.skip_test_if_missing_module()
self.setup_chain()
self.setup_network()
self.run_test()
success = TestStatus.PASSED
except JSONRPCException:
self.log.exception("JSONRPC error")
except SkipTest as e:
self.log.warning("Test Skipped: %s" % e.message)
success = TestStatus.SKIPPED
except AssertionError:
self.log.exception("Assertion failed")
except KeyError:
self.log.exception("Key error")
except Exception:
self.log.exception("Unexpected exception caught during testing")
except KeyboardInterrupt:
self.log.warning("Exiting after keyboard interrupt")
self.success = TestStatus.PASSED

def shutdown(self):
"""Call this method to shut down the test framework object."""

if success == TestStatus.FAILED and self.options.pdbonfailure:
if self.success == TestStatus.FAILED and self.options.pdbonfailure:
print("Testcase failed. Attaching python debugger. Enter ? for help")
pdb.set_trace()

Expand All @@ -225,7 +243,7 @@ def main(self):
should_clean_up = (
not self.options.nocleanup and
not self.options.noshutdown and
success != TestStatus.FAILED and
self.success != TestStatus.FAILED and
not self.options.perf
)
if should_clean_up:
Expand All @@ -238,10 +256,10 @@ def main(self):
self.log.warning("Not cleaning up dir {}".format(self.options.tmpdir))
cleanup_tree_on_exit = False

if success == TestStatus.PASSED:
if self.success == TestStatus.PASSED:
self.log.info("Tests successful")
exit_code = TEST_EXIT_PASSED
elif success == TestStatus.SKIPPED:
elif self.success == TestStatus.SKIPPED:
self.log.info("Test skipped")
exit_code = TEST_EXIT_SKIPPED
else:
Expand All @@ -251,7 +269,7 @@ def main(self):
logging.shutdown()
if cleanup_tree_on_exit:
shutil.rmtree(self.options.tmpdir)
sys.exit(exit_code)
return exit_code

# Methods to override in subclass test scripts.
def set_test_params(self):
Expand Down

0 comments on commit 6b71241

Please sign in to comment.