Minor fixes for cafe-brew runfile runner

* Cleans up some unittests/makes Brew more testable
  * Cleans up __repr__ output on some classes
  * Removes unneeded imports

Change-Id: Ib33bdcc21c2f554f2312f9ed92de8e500e8356f2
This commit is contained in:
Jose Idar 2016-05-11 15:06:34 -05:00 committed by Anna Eilering
parent 2b0ec92892
commit 60c014bd7c
4 changed files with 31 additions and 31 deletions

View File

@ -58,7 +58,7 @@ class ArgumentParser(argparse.ArgumentParser):
choices=[1, 2, 3], choices=[1, 2, 3],
default=2, default=2,
type=int, type=int,
help="Set unittest output verbosity") help="Set unittest output verbosity.")
self.add_argument( self.add_argument(
"--parallel", "--parallel",

View File

@ -8,8 +8,8 @@ import unittest
from collections import OrderedDict from collections import OrderedDict
from six import string_types from six import string_types
from six.moves import configparser from six.moves import configparser
from types import ModuleType
import types import types
from cafe.drivers.unittest.decorators import DataDrivenClass from cafe.drivers.unittest.decorators import DataDrivenClass
from cafe.common.reporting import cclogging from cafe.common.reporting import cclogging
@ -107,7 +107,7 @@ class _ImportablePathWrapper(object):
class _Brew(object): class _Brew(object):
""" """
Returns a module object containing all generated classes Returns a module object containing all generated classes, named 'name'
""" """
def __init__( def __init__(
@ -133,20 +133,16 @@ class _Brew(object):
self.mixin_test_classes = [ self.mixin_test_classes = [
_ImportablePathWrapper(tc) for tc in mixin_test_classes] _ImportablePathWrapper(tc) for tc in mixin_test_classes]
self.automodule_name = "{name}_automodule".format(name=self.name)
def __repr__(self): def __repr__(self):
return ( return (
"\n{name}:\n\t" "\nmodule name: {name}\n"
"automodule: {automodule_name}\n\t" "{fixture_attr}: {fixture}\n"
"{fixture_attr}: {fixture}\n\t" "{dsl_attr}: {dsl}\n"
"{dsl_attr}: {dsl}\n\t"
"{testclasses_attr}:\n{tcs}\n".format( "{testclasses_attr}:\n{tcs}\n".format(
fixture_attr=FIXTURE_ATTR, fixture_attr=FIXTURE_ATTR,
dsl_attr=DATASETLIST_ATTR, dsl_attr=DATASETLIST_ATTR,
testclasses_attr=TESTCLASSES_ATTR, testclasses_attr=TESTCLASSES_ATTR,
name=self.name, name=self.name,
automodule_name=self.automodule_name,
fixture=self.fixture_class, fixture=self.fixture_class,
dsl=self.dsl, dsl=self.dsl,
tcs='\n'.join(["\t\t{s}{t}".format( tcs='\n'.join(["\t\t{s}{t}".format(
@ -182,7 +178,7 @@ class _Brew(object):
bases.append(tc.import_class()) bases.append(tc.import_class())
# Create the new test class from the bases list and register it as a # Create the new test class from the bases list and register it as a
# member of the automodule so that it can be properly imported later # member of the module so that it can be properly imported later
# (type requires that bases be a tuple) # (type requires that bases be a tuple)
class_dict = {} class_dict = {}
@ -197,15 +193,15 @@ class _Brew(object):
all data generated test classes. all data generated test classes.
Returns the module object""" Returns the module object"""
# Generate the automodule # Generate the module
automodule = self._generate_module(self.automodule_name) automodule = self._generate_module(self.name)
# add it to sys.modules # add it to sys.modules
self._register_module(automodule) self._register_module(automodule)
# Generate the aggregate test class # Generate the aggregate test class
test_class = self._generate_test_class( test_class = self._generate_test_class(
module_name=self.automodule_name) module_name=self.name)
if self.dsl is not None: if self.dsl is not None:
# Instantiate the DataDrivenClass decorator with an instance of the # Instantiate the DataDrivenClass decorator with an instance of the
@ -272,20 +268,23 @@ class BrewFile(object):
files='\n'.join(["{space}{file}".format( files='\n'.join(["{space}{file}".format(
space="\t", file=f)for f in self.files])) space="\t", file=f)for f in self.files]))
def brew_list(self): def brew_names(self):
"""Return a list of non-reserved section names"""
return [ return [
s for s in self._data.sections() section for section in self._data.sections()
if s.lower() not in RESERVED_SECTION_NAMES] if section.lower() not in RESERVED_SECTION_NAMES]
def iterbrews(self): def iterbrews(self, brew_name_postfix='_automodule'):
""" Iterates through runfile sections and yields each individual """ Iterates through runfile sections and yields each individual
section as a Brew object. You have to call .brew() on the individual section as a Brew object. You have to call .brew() on the individual
Brews to get them to generate a module that contains the aggregate Brews to get them to generate a module that contains the aggregate
test class, so these should be safe to store in a list regardless of test class, so these should be safe to store in a list regardless of
dataset size. dataset size.
""" """
for s in self.brew_list(): for s in self.brew_names():
attr_dict = dict(name=s) brew_name = "{name}{postfix}".format(
name=s, postfix=brew_name_postfix)
attr_dict = dict(name=brew_name)
for attr in BREW_SECTION_ATTR_LIST: for attr in BREW_SECTION_ATTR_LIST:
try: try:
attr_dict[attr] = self._data.get(s, attr) attr_dict[attr] = self._data.get(s, attr)

View File

@ -27,6 +27,9 @@ class BrewRunner(UnittestRunner):
self.datagen_start = time.time() self.datagen_start = time.time()
self.run_file = BrewFile(self.cl_args.runfiles) self.run_file = BrewFile(self.cl_args.runfiles)
# Log the runfile here so that it appears in the logs before any tests
self._log.debug("\n" + str(self.run_file))
# TODO: Once the parallel_runner is changed to a yielding model, # TODO: Once the parallel_runner is changed to a yielding model,
# change this to yielding brews instead of generating a list # change this to yielding brews instead of generating a list
self.suites = SuiteBuilder( self.suites = SuiteBuilder(
@ -34,24 +37,23 @@ class BrewRunner(UnittestRunner):
dry_run=self.cl_args.dry_run, dry_run=self.cl_args.dry_run,
exit_on_error=True).get_suites() exit_on_error=True).get_suites()
self.print_configuration(self.test_env, runfile=self.run_file) self.print_configuration(self.test_env, brewfile=self.run_file)
def print_configuration(self, test_env, repos=None, runfile=None): def print_configuration(self, test_env, repos=None, brewfile=None):
"""Prints the config/logs/repo/data_directory""" """Prints the config/logs/repo/data_directory/brewfiles"""
print("=" * 150) print("=" * 150)
print("Percolated Configuration") print("Percolated Configuration")
print("-" * 150) print("-" * 150)
if runfile: if brewfile:
print("BREW FILES........:") print("BREW FILES........:")
print("\t\t " + "\n\t\t ".join(runfile.files)) print("\t\t" + "\n\t\t ".join(brewfile.files))
if self.cl_args.verbose == 3: if self.cl_args.verbose >= 2:
print("BREWS............:") print("BREWS............:")
print "\t" + "\n\t".join(runfile.brews_to_strings()) print "\t" + "\n\t".join(brewfile.brews_to_strings())
if repos: if repos:
print("BREWING FROM: ....: {0}".format(repos[0])) print("BREWING FROM: ....: {0}".format(repos[0]))
for repo in repos[1:]: for repo in repos[1:]:
print("{0}{1}".format(" " * 20, repo)) print("{0}{1}".format(" " * 20, repo))
self._log.debug(str(runfile))
print("ENGINE CONFIG FILE: {0}".format(test_env.engine_config_path)) print("ENGINE CONFIG FILE: {0}".format(test_env.engine_config_path))
print("TEST CONFIG FILE..: {0}".format(test_env.test_config_file_path)) print("TEST CONFIG FILE..: {0}".format(test_env.test_config_file_path))
print("DATA DIRECTORY....: {0}".format(test_env.test_data_directory)) print("DATA DIRECTORY....: {0}".format(test_env.test_data_directory))
@ -60,7 +62,7 @@ class BrewRunner(UnittestRunner):
@staticmethod @staticmethod
def print_mug(): def print_mug():
"""Prints the cafe mug""" """Prints the cafe 'mug'"""
print(""" print("""
/~~~~~~~~~~~~~~~~~~~~~~~/| /~~~~~~~~~~~~~~~~~~~~~~~/|
/ /######/ / | / /######/ / |

View File

@ -48,8 +48,7 @@ class Brew_Tests(unittest.TestCase):
def test_call_brew_initialized_with_only_a_name(self): def test_call_brew_initialized_with_only_a_name(self):
b = _Brew(self.module_name) b = _Brew(self.module_name)
module_ = b() module_ = b()
new_name = "FakeBrew_automodule" self.assertEqual(module_.__name__, self.module_name)
self.assertEquals(module_.__name__, new_name)
def test_init_raises_BrewMissingTestClassesError_non_iterable(self): def test_init_raises_BrewMissingTestClassesError_non_iterable(self):
self.assertRaises( self.assertRaises(