diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-05-10 17:56:10 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-05-10 18:29:40 +0300 |
commit | 118766db19e3de6f619ccf39daf64bf66e77b7f5 (patch) | |
tree | 7c076cff04c37324539cac18fd56e39c0f31a9bf /templates | |
parent | 6e3e20dc3ee22abf740b81da5e93413248676c61 (diff) | |
download | subplot-118766db19e3de6f619ccf39daf64bf66e77b7f5.tar.gz |
feat: make it easier to debug generated Python test programs
The generated test program now has --log and --save-on-failure
options, and automatically logs every scenario, step, and change to
context.
Diffstat (limited to 'templates')
-rw-r--r-- | templates/python/template.py | 64 |
1 files changed, 60 insertions, 4 deletions
diff --git a/templates/python/template.py b/templates/python/template.py index 87ced78..62b1fc9 100644 --- a/templates/python/template.py +++ b/templates/python/template.py @@ -7,12 +7,14 @@ ############################################################################# # Helper code generated by Subplot. +import argparse import base64 import logging import os import random import shutil import sys +import tarfile import tempfile # Store context between steps. @@ -31,6 +33,7 @@ class Context: return self._vars[key] def __setitem__(self, key, value): + logging.info('Context: {}={!r}'.format(key, value)) self._vars[key] = value # Decode a base64 encoded string. Result is binary or unicode string. @@ -85,6 +88,7 @@ os.chdir(_datadir) def scenario_{{ loop.index }}(): title = decode_str('{{ scenario.title | base64 }}') print('scenario: {}'.format(title)) + logging.info("Scenario: {}".format(title)) _scendir = tempfile.mkdtemp(dir=_datadir) os.chdir(_scendir) ctx = Context() @@ -92,6 +96,7 @@ def scenario_{{ loop.index }}(): # Step: {{ step.text }} step = decode_str('{{ step.text | base64 }}') print(' step: {{ step.kind | lower }} {}'.format(step)) + logging.info(' step: {{ step.kind | lower }} {}'.format(step)) args = {} {% for part in step.parts %}{% if part.CapturedText is defined -%} name = decode_str('{{ part.CapturedText.name | base64 }}') @@ -110,20 +115,70 @@ _scenarios = { } +def parse_command_line(): + p = argparse.ArgumentParser() + p.add_argument("--log") + p.add_argument("--save-on-failure") + p.add_argument("patterns", nargs="*") + return p.parse_args() + + +def setup_logging(args): + if args.log: + fmt = "%(asctime)s %(levelname)s %(message)s" + datefmt = "%Y-%m-%d %H:%M:%S" + formatter = logging.Formatter(fmt, datefmt) + + filename = os.path.abspath(os.path.join(srcdir, args.log)) + handler = logging.FileHandler(filename) + handler.setFormatter(formatter) + else: + handler = logging.NullHandler() + + logger = logging.getLogger() + logger.addHandler(handler) + logger.setLevel(logging.DEBUG) + + +def save_directory(dirname, tarname): + print('tarname', tarname) + logging.info("Saving {} to {}".format(dirname, tarname)) + tar = tarfile.open(tarname, "w") + tar.add(dirname, arcname="datadir") + tar.close() + + def main(): - if len(sys.argv) == 1: + args = parse_command_line() + setup_logging(args) + logging.info("Test program starts") + + logging.info("patterns: {}".format(args.patterns)) + if len(args.patterns) == 0: + logging.info("Executing all scenarios") funcs = list(_scenarios.values()) random.shuffle(funcs) else: - patterns = [arg.lower() for arg in sys.argv[1:]] + logging.info("Executing requested scenarios only: {}".format(args.patterns)) + patterns = [arg.lower() for arg in args.patterns] funcs = [ func for title, func in _scenarios.items() if any(pattern in title.lower() for pattern in patterns) ] - for func in funcs: - func() + try: + for func in funcs: + func() + except Exception as e: + logging.error(str(e), exc_info=True) + if args.save_on_failure: + print(args.save_on_failure) + filename = os.path.abspath(os.path.join(srcdir, args.save_on_failure)) + print(filename) + save_directory(_datadir, filename) + raise + main() @@ -132,3 +187,4 @@ main() shutil.rmtree(_datadir) print('OK, all scenarios finished successfully') +logging.info("OK, all scenarios finished successfully") |