Tweaked report run logic

This commit is contained in:
Jeremy Stretch 2017-09-21 13:49:04 -04:00
parent 8f1607e010
commit 16d1f9aca8
2 changed files with 30 additions and 14 deletions

View File

@ -20,26 +20,39 @@ class Command(BaseCommand):
# Gather all reports to be run # Gather all reports to be run
reports = [] reports = []
for module_name in options['reports']: for module_name in options['reports']:
# Split the report name off if one has been provided.
report_name = None
if '.' in module_name:
module_name, report_name = module_name.split('.', 1)
# Import the report module
try: try:
report_module = importlib.import_module('reports.report_{}'.format(module_name)) report_module = importlib.import_module('reports.report_{}'.format(module_name))
except ImportError: except ImportError:
self.stdout.write( self.stdout.write(
"Report '{}' not found. Ensure that the report has been saved as 'report_{}.py' in the reports " "Report module '{}' not found. Ensure that the report has been saved as 'report_{}.py' in the "
"directory.".format(module_name, module_name) "reports directory.".format(module_name, module_name)
) )
return return
for name, cls in inspect.getmembers(report_module, inspect.isclass):
if cls in Report.__subclasses__(): # If the name of a particular report has been given, run that. Otherwise, run all reports in the module.
reports.append((name, cls)) if report_name is not None:
report_cls = getattr(report_module, report_name)
reports = [(report_name, report_cls)]
else:
for name, report_cls in inspect.getmembers(report_module, inspect.isclass):
if report_cls in Report.__subclasses__():
reports.append((name, report_cls))
# Run reports # Run reports
for name, report in reports: for name, report_cls in reports:
self.stdout.write("[{:%H:%M:%S}] Running report {}...".format(timezone.now(), name)) self.stdout.write("[{:%H:%M:%S}] Running {}...".format(timezone.now(), name))
report = report() report = report_cls()
report.run() results = report.run()
status = self.style.ERROR('FAILED') if report.failed else self.style.SUCCESS('SUCCESS') status = self.style.ERROR('FAILED') if report.failed else self.style.SUCCESS('SUCCESS')
self.stdout.write("[{:%H:%M:%S}] {}: {}".format(timezone.now(), name, status)) self.stdout.write("[{:%H:%M:%S}] {}: {}".format(timezone.now(), name, status))
for test_name, attrs in report.results.items(): for test_name, attrs in results.items():
self.stdout.write(" {}: {} success, {} info, {} warning, {} failed".format( self.stdout.write(" {}: {} success, {} info, {} warning, {} failed".format(
test_name, attrs['success'], attrs['info'], attrs['warning'], attrs['failed'] test_name, attrs['success'], attrs['info'], attrs['warning'], attrs['failed']
)) ))

View File

@ -29,12 +29,13 @@ class Report(object):
} }
} }
""" """
results = OrderedDict()
active_test = None
failed = False
def __init__(self): def __init__(self):
self.results = OrderedDict()
self.active_test = None
self.failed = False
# Compile test methods and initialize results skeleton # Compile test methods and initialize results skeleton
test_methods = [] test_methods = []
for method in dir(self): for method in dir(self):
@ -92,9 +93,11 @@ class Report(object):
def run(self): def run(self):
""" """
Run the report. Each test method will be executed in order. Run the report and return its results. Each test method will be executed in order.
""" """
for method_name in self.test_methods: for method_name in self.test_methods:
self.active_test = method_name self.active_test = method_name
test_method = getattr(self, method_name) test_method = getattr(self, method_name)
test_method() test_method()
return self.results