devtools/cwwindmill.py
brancholdstable
changeset 7074 e4580e5f0703
parent 7071 db7608cb32bc
child 7075 4751d77394b1
equal deleted inserted replaced
6749:48f468f33704 7074:e4580e5f0703
    27 import os, os.path as osp
    27 import os, os.path as osp
    28 from logging import getLogger, ERROR
    28 from logging import getLogger, ERROR
    29 import sys
    29 import sys
    30 
    30 
    31 # imported by default to simplify further import statements
    31 # imported by default to simplify further import statements
    32 from logilab.common.testlib import TestCase, unittest_main
    32 from logilab.common.testlib import TestCase, unittest_main, Tags
    33 
    33 
    34 import windmill
    34 try:
    35 from windmill.dep import functest
    35     import windmill
    36 from windmill.bin.admin_lib import configure_global_settings, setup, teardown
    36     from windmill.dep import functest
       
    37     from windmill.bin.admin_lib import configure_global_settings, setup, teardown
       
    38 except ImportError, ex:
       
    39     windmill = None
    37 
    40 
    38 from cubicweb.devtools.httptest import CubicWebServerTC, CubicWebServerConfig
    41 from cubicweb.devtools.httptest import CubicWebServerTC, CubicWebServerConfig
    39 
    42 
       
    43 if windmill is None:
       
    44     class CubicWebWindmillUseCase(CubicWebServerTC):
       
    45         tags = CubicWebServerTC.tags & Tags(('windmill',))
    40 
    46 
    41 # Excerpt from :ref:`windmill.authoring.unit`
    47         def testWindmill(self):
    42 class UnitTestReporter(functest.reports.FunctestReportInterface):
    48             self.skipTest("can't import windmill %s" % ex)
    43     def summary(self, test_list, totals_dict, stdout_capture):
    49 else:
    44         self.test_list = test_list
    50     # Excerpt from :ref:`windmill.authoring.unit`
       
    51     class UnitTestReporter(functest.reports.FunctestReportInterface):
       
    52         def summary(self, test_list, totals_dict, stdout_capture):
       
    53             self.test_list = test_list
    45 
    54 
    46 unittestreporter = UnitTestReporter()
    55     unittestreporter = UnitTestReporter()
    47 functest.reports.register_reporter(unittestreporter)
    56     functest.reports.register_reporter(unittestreporter)
    48 
    57 
    49 class CubicWebWindmillUseCase(CubicWebServerTC):
    58     class CubicWebWindmillUseCase(CubicWebServerTC):
    50     """basic class for Windmill use case tests
    59         """basic class for Windmill use case tests
    51 
    60 
    52     If you want to change cubicweb test server parameters, define a new
    61         If you want to change cubicweb test server parameters, define a new
    53     :class:`CubicWebServerConfig` and override the :var:`configcls`
    62         :class:`CubicWebServerConfig` and override the :var:`configcls`
    54     attribute:
    63         attribute:
    55 
    64 
    56         configcls = CubicWebServerConfig
    65             configcls = CubicWebServerConfig
    57 
    66 
    58     From Windmill configuration:
    67         From Windmill configuration:
    59 
    68 
    60     .. attribute:: browser
    69         .. attribute:: browser
    61         identification string (firefox|ie|safari|chrome) (firefox by default)
    70             identification string (firefox|ie|safari|chrome) (firefox by default)
    62     .. attribute :: edit_test
    71         .. attribute :: edit_test
    63         load and edit test for debugging (False by default)
    72             load and edit test for debugging (False by default)
    64     .. attribute:: test_dir (optional)
    73         .. attribute:: test_dir (optional)
    65         testing file path or directory (windmill directory under your unit case
    74             testing file path or directory (windmill directory under your unit case
    66         file by default)
    75             file by default)
    67 
    76 
    68     Examples:
    77         Examples:
    69 
    78 
       
    79             browser = 'firefox'
       
    80             test_dir = osp.join(__file__, 'windmill')
       
    81             edit_test = False
       
    82 
       
    83         If you prefer, you can put here the use cases recorded by windmill GUI
       
    84         (services transformer) instead of the windmill sub-directory
       
    85         You can change `test_dir` as following:
       
    86 
       
    87             test_dir = __file__
       
    88 
       
    89         Instead of toggle `edit_test` value, try `pytest -i`
       
    90         """
    70         browser = 'firefox'
    91         browser = 'firefox'
    71         test_dir = osp.join(__file__, 'windmill')
    92         edit_test = "-i" in sys.argv # detection for pytest invocation
    72         edit_test = False
    93         # Windmill use case are written with no anonymous user
       
    94         anonymous_allowed = False
    73 
    95 
    74     If you prefer, you can put here the use cases recorded by windmill GUI
    96         tags = CubicWebServerTC.tags & Tags(('windmill',))
    75     (services transformer) instead of the windmill sub-directory
       
    76     You can change `test_dir` as following:
       
    77 
    97 
    78         test_dir = __file__
    98         def _test_dir(self):
       
    99             """access to class attribute if possible or make assumption
       
   100             of expected directory"""
       
   101             try:
       
   102                 return getattr(self, 'test_dir')
       
   103             except AttributeError:
       
   104                 if os.path.basename(sys.argv[0]) == "pytest":
       
   105                     test_dir = os.getcwd()
       
   106                 else:
       
   107                     import inspect
       
   108                     test_dir = os.path.dirname(inspect.stack()[-1][1])
       
   109                 return osp.join(test_dir, 'windmill')
    79 
   110 
    80     Instead of toggle `edit_test` value, try `pytest -i`
   111         def setUp(self):
    81     """
   112             # Start CubicWeb session before running the server to populate self.vreg
    82     browser = 'firefox'
   113             CubicWebServerTC.setUp(self)
    83     edit_test = "-i" in sys.argv # detection for pytest invocation
   114             # XXX reduce log output (should be done in a cleaner way)
    84     # Windmill use case are written with no anonymous user
   115             # windmill fu** up our logging configuration
    85     anonymous_logged = False
   116             for logkey in ('windmill', 'logilab', 'cubicweb'):
       
   117                 getLogger(logkey).setLevel(ERROR)
       
   118             self.test_dir = self._test_dir()
       
   119             msg = "provide a valid 'test_dir' as the given test file/dir (current: %s)"
       
   120             assert os.path.exists(self.test_dir), (msg % self.test_dir)
       
   121             # windmill setup
       
   122             windmill.stdout, windmill.stdin = sys.stdout, sys.stdin
       
   123             configure_global_settings()
       
   124             windmill.settings['TEST_URL'] = self.config['base-url']
       
   125             if hasattr(self,"windmill_settings"):
       
   126                 for (setting,value) in self.windmill_settings.iteritems():
       
   127                     windmill.settings[setting] = value
       
   128             self.windmill_shell_objects = setup()
    86 
   129 
    87     def _test_dir(self):
   130         def tearDown(self):
    88         """access to class attribute if possible or make assumption
   131             teardown(self.windmill_shell_objects)
    89         of expected directory"""
   132             CubicWebServerTC.tearDown(self)
    90         try:
       
    91             return getattr(self, 'test_dir')
       
    92         except AttributeError:
       
    93             if os.path.basename(sys.argv[0]) == "pytest":
       
    94                 test_dir = os.getcwd()
       
    95             else:
       
    96                 import inspect
       
    97                 test_dir = os.path.dirname(inspect.stack()[-1][1])
       
    98             return osp.join(test_dir, 'windmill')
       
    99 
   133 
   100     def setUp(self):
   134         def testWindmill(self):
   101         # Start CubicWeb session before running the server to populate self.vreg
   135             if self.edit_test:
   102         CubicWebServerTC.setUp(self)
   136                 # see windmill.bin.admin_options.Firebug
   103         # XXX reduce log output (should be done in a cleaner way)
   137                 windmill.settings['INSTALL_FIREBUG'] = 'firebug'
   104         # windmill fu** up our logging configuration
   138                 windmill.settings.setdefault('MOZILLA_PLUGINS', []).extend(
   105         for logkey in ('windmill', 'logilab', 'cubicweb'):
   139                     ['/usr/share/mozilla-extensions/',
   106             getLogger(logkey).setLevel(ERROR)
   140                      '/usr/share/xul-ext/'])
   107         self.test_dir = self._test_dir()
   141             controller = self.windmill_shell_objects['start_' + self.browser]()
   108         msg = "provide a valid 'test_dir' as the given test file/dir (current: %s)"
   142             self.windmill_shell_objects['do_test'](self.test_dir,
   109         assert os.path.exists(self.test_dir), (msg % self.test_dir)
   143                                                    load=self.edit_test,
   110         # windmill setup
   144                                                    threaded=False)
   111         windmill.stdout, windmill.stdin = sys.stdout, sys.stdin
   145             # set a breakpoint to be able to debug windmill test
   112         configure_global_settings()
   146             if self.edit_test:
   113         windmill.settings['TEST_URL'] = self.config['base-url']
   147                 import pdb; pdb.set_trace()
   114         if hasattr(self,"windmill_settings"):
   148                 return
   115             for (setting,value) in self.windmill_settings.iteritems():
       
   116                 windmill.settings[setting] = value
       
   117         self.windmill_shell_objects = setup()
       
   118 
   149 
   119     def tearDown(self):
   150             # reporter
   120         teardown(self.windmill_shell_objects)
   151             for test in unittestreporter.test_list:
   121         CubicWebServerTC.tearDown(self)
   152                 msg = ""
       
   153                 self._testMethodDoc = getattr(test, "__doc__", None)
       
   154                 self._testMethodName = test.__name__
       
   155                 # try to display a better message in case of failure
       
   156                 if hasattr(test, "tb"):
       
   157                     msg = '\n'.join(test.tb)
       
   158                 self.assertEqual(test.result, True, msg=msg)
   122 
   159 
   123     def testWindmill(self):
       
   124         if self.edit_test:
       
   125             # see windmill.bin.admin_options.Firebug
       
   126             windmill.settings['INSTALL_FIREBUG'] = 'firebug'
       
   127             windmill.settings.setdefault('MOZILLA_PLUGINS', []).extend(
       
   128                 ['/usr/share/mozilla-extensions/',
       
   129                  '/usr/share/xul-ext/'])
       
   130         controller = self.windmill_shell_objects['start_' + self.browser]()
       
   131         self.windmill_shell_objects['do_test'](self.test_dir,
       
   132                                                load=self.edit_test,
       
   133                                                threaded=False)
       
   134         # set a breakpoint to be able to debug windmill test
       
   135         if self.edit_test:
       
   136             import pdb; pdb.set_trace()
       
   137             return
       
   138 
       
   139         # reporter
       
   140         for test in unittestreporter.test_list:
       
   141             msg = ""
       
   142             self._testMethodDoc = getattr(test, "__doc__", None)
       
   143             self._testMethodName = test.__name__
       
   144             # try to display a better message in case of failure
       
   145             if hasattr(test, "tb"):
       
   146                 msg = '\n'.join(test.tb)
       
   147             self.assertEqual(test.result, True, msg=msg)
       
   148