Changeset 2660


Ignore:
Timestamp:
Jun 15, 2010 1:13:16 AM (11 years ago)
Author:
wehart
Message:

Further evolution of this capability. This still doesn't quite work, but I'm
getting close.

Location:
coopr.testing/trunk
Files:
4 added
5 edited

Legend:

Unmodified
Added
Removed
  • coopr.testing/trunk/coopr/testing/__init__.py

    r2625 r2660  
    1515import yaml_plugin
    1616import driver
     17import coopr_driver
     18import colin_driver
     19import pyomo_driver
    1720
    1821pyutilib.component.core.PluginGlobals.pop_env()
  • coopr.testing/trunk/coopr/testing/driver.py

    r2626 r2660  
    11
    2 __all__ = ['main', 'create_test_suite']
     2__all__ = ['main', 'create_test_suites']
    33
    44import os
     
    66import optparse
    77import plugins
     8from pyutilib.misc import Options
    89from pyutilib.component.core import ExtensionPoint, PluginGlobals
     10import pyutilib.th as unittest
    911
    1012
    11 def create_test_suite(filename):
    12     ep = ExtensionPoint(plugins.ITestDriver)
     13def validate_test_config(suite):
     14    tmp = set(suite.keys())
     15    if not tmp.issubset(set(['python','solvers','problems','suites', 'driver'])):
     16        raise IOError, "Unexpected test sections: "+str(suite.keys())
     17    #
     18    if 'python' in suite:
     19        if not type(suite['python']) is list:
     20            raise IOError, "Expected list of Python expressions"
     21    #
     22    if 'solvers' in suite:
     23        if not type(suite['solvers']) is dict:
     24            raise IOError, "Expected dictionary of solvers"
     25        for key in suite['solvers']:
     26            if not type(suite['solvers'][key]) is dict:
     27                raise IOError, "Expected solvers to have a dictionary of options"
     28    #
     29    if 'problems' in suite:
     30        if not type(suite['problems']) is dict:
     31            raise IOError, "Expected dictionary of problems"
     32        for key in suite['problems']:
     33            if not type(suite['problems'][key]) is dict:
     34                raise IOError, "Expected problems to have a dictionary of options"
     35    #
     36    if 'suites' in suite:
     37        if not type(suite['suites']) is dict:
     38            raise IOError, "Expected dictionary of suites"
     39        for key in suite['suites']:
     40            if not type(suite['suites'][key]) is dict:
     41                raise IOError, "Expected suites to have a dictionary of options"
     42
     43
     44
     45def create_test_suites(filename, _globals, options=Options):
     46    ep = ExtensionPoint(plugins.ITestParser)
    1347    ftype = os.path.splitext(filename)[1]
    1448    if not ftype == '':
     
    1650    service = ep.service(ftype)
    1751    if service is None:
    18         raise IOError, "Unknown file type.  Cannot load suites from file '%s'" % filename
    19     suite = service.load_test_suite(filename)
     52        raise IOError, "Unknown file type.  Cannot load test configuration from file '%s'" % filename
     53    config = service.load_test_config(filename)
     54    #service.print_test_config(config)
     55    validate_test_config(config)
     56    #
     57    # Create test driver, which is put in the global namespace
     58    #
     59    driver = plugins.TestDriverFactory(config['driver'])
     60    if driver is None:
     61        raise IOError, "Unexpected test driver '%s'" % config['driver']
     62    _globals["test_driver"] = driver
     63    #
     64    # Evaluate Python expressions
     65    #
     66    for item in config.get('python',[]):
     67        try:
     68            eval(item, _globals)
     69        except Exception, err:
     70            print "ERROR evaluating '%s'" % item
     71    #
     72    # Generate suite
     73    #
     74    for suite in config.get('suites',{}):
     75        create_test_suite(suite, config, _globals, options)
     76
     77
     78def create_test_suite(suite, config, _globals, options):
     79    #
     80    # Skip suite creation if the options category is not in the list of test suite categories
     81    #
     82    if not (options.category is None or options.category in config['suites'][suite].get('categories',[])):
     83        return
     84    #
     85    # Create test driver
     86    #
     87    print "X",suite
     88    #class tmp(unittest.TestCase): pass
     89    _globals["TestSuite_"+suite] = type("TestSuite_"+suite,(unittest.TestCase,),{})
     90    #tmp = _globals["TestSuite_"+suite]
     91    #
     92    # Create test functions
     93    #
     94    print "HERE",suite
     95    for solver in config['suites'][suite]['solvers']:
     96        if 'name' in solver:
     97            sname = solver['name']
     98        else:
     99            sname = solver['solver']
     100        for problem in config['suites'][suite]['problems']:
     101            test_name = sname+"_"+problem
     102            print test_name
     103            options = Options()
     104            options.solver = sname
     105            options.problem = problem
     106            def fn(name):
     107                return test_driver.run_test(name, fn._options)
     108            fn._options = options
     109            _globals["TestSuite_"+suite].add_fn_test(name=test_name, fn=fn)
     110    #
     111    # Set the name of this class in the global namespace
     112    #
     113    print dir(_globals["TestSuite_"+suite])
     114
    20115
    21116
    22117def main():
     118
    23119    parser = optparse.OptionParser()
    24120
    25121    parser.add_option('-d','--debug',
    26         action='store',
     122        action='store_true',
    27123        dest='debug',
    28124        default=False,
    29125        help='Set debugging flag')
     126
     127    parser.add_option('-v','--verbose',
     128        action='store_true',
     129        dest='verbose',
     130        default=False,
     131        help='Set flag for verbose output')
     132
     133    parser.add_option('-q','--quiet',
     134        action='store_true',
     135        dest='quiet',
     136        default=False,
     137        help='Set flag for quiet output')
     138
     139    parser.add_option('--category',
     140        action='store',
     141        dest='category',
     142        default=None,
     143        help='Define the category of the test suite that is executed')
    30144   
    31145    options, args = parser.parse_args(sys.argv)
     
    36150
    37151    for file in args[1:]:
    38         create_test_suite(file)
     152        create_test_suites(file, globals(), options)
     153
     154    tmp = [args[0]]
     155    if options.quiet:
     156        tmp.append('-q')
     157    if options.verbose or options.debug:
     158        tmp.append('-v')
     159    #tmp += args
     160    sys.argv = tmp
     161    print "X",sys.argv
     162    print "X",globals().keys()
     163    print "X",dir(globals()['TestSuite_ps'])
     164    print dir(globals()['__name__'])
     165    unittest.main(module=globals()['__name__'])
    39166       
    40167       
  • coopr.testing/trunk/coopr/testing/plugins.py

    r2626 r2660  
    11
    2 __all__ = ['ITestDriver']
     2__all__ = ['ITestDriver', 'TestDriverFactory', 'ITestParser']
    33
    44from pyutilib.component.core import *
    55
    6 class ITestDriver(Interface):
     6class ITestParser(Interface):
    77
    8     def load_test_suite(self, filename):
     8    def load_test_config(self, filename):
     9        pass
     10
     11    def print_test_config(self, repn):
    912        pass
    1013
    1114
     15class ITestDriver(Interface):
     16
     17    def run_test(self, options):
     18        pass
     19
     20TestDriverFactory = CreatePluginFactory(ITestDriver)
  • coopr.testing/trunk/coopr/testing/yaml_plugin.py

    r2626 r2660  
    1010
    1111
    12 class YamlTestDriver(SingletonPlugin):
     12class YamlTestParser(SingletonPlugin):
    1313
    14     implements(plugins.ITestDriver)
     14    implements(plugins.ITestParser)
    1515
    1616    def __init__(self, **kwds):
     
    1818        self.name='yml'
    1919
    20     def load_test_suite(self, filename):
     20    def load_test_config(self, filename):
    2121        INPUT = open(filename, 'r')
    2222        repn = yaml.load(INPUT, yaml.SafeLoader)
     
    2424        return repn
    2525
     26    def print_test_config(self, repn):
     27        print repn
     28
  • coopr.testing/trunk/setup.py

    r2625 r2660  
    7070      entry_points="""
    7171        [console_scripts]
    72         coopr_test_driver = coopr.testing.driver:main
     72        coopr_test_driver = coopr.testing.coopr_test_driver:main
    7373      """
    7474      )
Note: See TracChangeset for help on using the changeset viewer.