Changeset 897ca7f in sasmodels


Ignore:
Timestamp:
Sep 12, 2016 1:27:52 AM (5 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
7e6bea81
Parents:
733a3e1
Message:

add run_one() to run tests on model from sasview console

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/model_test.py

    r40a87fa r897ca7f  
    5050import numpy as np  # type: ignore 
    5151 
    52 from .core import list_models, load_model_info, build_model, HAVE_OPENCL 
     52from . import core 
     53from .core import list_models, load_model_info, build_model 
    5354from .direct_model import call_kernel, call_ER, call_VR 
    5455from .exception import annotate_exception 
     
    9899        if is_py:  # kernel implemented in python 
    99100            test_name = "Model: %s, Kernel: python"%model_name 
    100             test_method_name = "test_%s_python" % model_name 
     101            test_method_name = "test_%s_python" % model_info.id 
    101102            test = ModelTestCase(test_name, model_info, 
    102103                                 test_method_name, 
     
    106107        else:   # kernel implemented in C 
    107108            # test using opencl if desired and available 
    108             if 'opencl' in loaders and HAVE_OPENCL: 
     109            if 'opencl' in loaders and core.HAVE_OPENCL: 
    109110                test_name = "Model: %s, Kernel: OpenCL"%model_name 
    110                 test_method_name = "test_%s_opencl" % model_name 
     111                test_method_name = "test_%s_opencl" % model_info.id 
    111112                # Using dtype=None so that the models that are only 
    112113                # correct for double precision are not tested using 
     
    122123            if 'dll' in loaders: 
    123124                test_name = "Model: %s, Kernel: dll"%model_name 
    124                 test_method_name = "test_%s_dll" % model_name 
     125                test_method_name = "test_%s_dll" % model_info.id 
    125126                test = ModelTestCase(test_name, model_info, 
    126127                                     test_method_name, 
     
    249250    return abs(target-actual)/shift < 1.5*10**-digits 
    250251 
    251 def main(): 
    252     # type: () -> int 
    253     """ 
    254     Run tests given is sys.argv. 
     252def run_one(model): 
     253    # type: (str) -> None 
     254    """ 
     255    Run the tests for a single model, printing the results to stdout. 
     256 
     257    *model* can by a python file, which is handy for checking user defined 
     258    plugin models. 
     259    """ 
     260    # Note that running main() directly did not work from within the 
     261    # wxPython pycrust console.  Instead of the results appearing in the 
     262    # window they were printed to the underlying console. 
     263    from unittest.runner import TextTestResult, _WritelnDecorator 
     264 
     265    # Build a object to capture and print the test results 
     266    stream = _WritelnDecorator(sys.stdout)  # Add writeln() method to stream 
     267    verbosity = 2 
     268    descriptions = True 
     269    result = TextTestResult(stream, descriptions, verbosity) 
     270 
     271    # Build a test suite containing just the model 
     272    loaders = ['opencl'] 
     273    models = [model] 
     274    try: 
     275        suite = make_suite(loaders, models) 
     276    except Exception: 
     277        import traceback 
     278        stream.writeln(traceback.format_exc()) 
     279        return 
     280 
     281    # Run the test suite 
     282    suite.run(result) 
     283 
     284    # Print the failures and errors 
     285    for _, tb in result.errors: 
     286        stream.writeln(tb) 
     287    for _, tb in result.failures: 
     288        stream.writeln(tb) 
     289 
     290    # Check if there are user defined tests. 
     291    # Yes, it is naughty to peek into the structure of the test suite, and 
     292    # to assume that it contains only one test. 
     293    if not suite._tests[0].info.tests: 
     294        stream.writeln("Note: %s has no user defined tests."%model) 
     295 
     296 
     297def main(*models): 
     298    # type: (*str) -> int 
     299    """ 
     300    Run tests given is models. 
    255301 
    256302    Returns 0 if success or 1 if any tests fail. 
     
    263309        test_args = {} 
    264310 
    265     models = sys.argv[1:] 
    266311    if models and models[0] == '-v': 
    267312        verbosity = 2 
     
    270315        verbosity = 1 
    271316    if models and models[0] == 'opencl': 
    272         if not HAVE_OPENCL: 
     317        if not core.HAVE_OPENCL: 
    273318            print("opencl is not available") 
    274319            return 1 
     
    318363 
    319364if __name__ == "__main__": 
    320     sys.exit(main()) 
     365    sys.exit(main(*sys.argv[1:])) 
Note: See TracChangeset for help on using the changeset viewer.