devtools/cwwindmill.py
changeset 7061 bb2080547722
parent 7059 1d65b235549f
parent 7052 9680cf108821
child 7075 4751d77394b1
equal deleted inserted replaced
7059:1d65b235549f 7061:bb2080547722
    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 `python <test script> -f`
       
    90         """
    70         browser = 'firefox'
    91         browser = 'firefox'
    71         test_dir = osp.join(__file__, 'windmill')
    92 <<<<<<< /home/syt/src/fcubicweb/cubicweb/devtools/cwwindmill.py
    72         edit_test = False
       
    73 
    93 
    74     If you prefer, you can put here the use cases recorded by windmill GUI
    94         edit_test = "-i" in sys.argv # detection for pytest invocation
    75     (services transformer) instead of the windmill sub-directory
    95         # Windmill use case are written with no anonymous user
    76     You can change `test_dir` as following:
    96         anonymous_logged = False
    77 
    97 
    78         test_dir = __file__
    98         tags = CubicWebServerTC.tags & Tags(('windmill',))
    79 
    99 
    80     Instead of toggle `edit_test` value, try `python <test script> -f`
   100         def _test_dir(self):
    81     """
   101             """access to class attribute if possible or make assumption
    82     browser = 'firefox'
   102             of expected directory"""
    83     edit_test = "-f" in sys.argv or "-i" in sys.argv # XXX pytest
   103             try:
    84     # Windmill use case are written with no anonymous user
   104                 return getattr(self, 'test_dir')
    85     anonymous_logged = False
   105             except AttributeError:
       
   106                 if os.path.basename(sys.argv[0]) == "pytest":
       
   107                     test_dir = os.getcwd()
       
   108                 else:
       
   109                     import inspect
       
   110                     test_dir = os.path.dirname(inspect.stack()[-1][1])
       
   111                 return osp.join(test_dir, 'windmill')
    86 
   112 
    87     def _test_dir(self):
   113         def setUp(self):
    88         """access to class attribute if possible or make assumption
   114             # Start CubicWeb session before running the server to populate self.vreg
    89         of expected directory"""
   115             CubicWebServerTC.setUp(self)
    90         try:
   116             # XXX reduce log output (should be done in a cleaner way)
    91             return getattr(self, 'test_dir')
   117             # windmill fu** up our logging configuration
    92         except AttributeError:
   118             for logkey in ('windmill', 'logilab', 'cubicweb'):
    93             if os.path.basename(sys.argv[0]) == "pytest":
   119                 getLogger(logkey).setLevel(ERROR)
    94                 test_dir = os.getcwd()
   120             self.test_dir = self._test_dir()
    95             else:
   121             msg = "provide a valid 'test_dir' as the given test file/dir (current: %s)"
    96                 import inspect
   122             assert os.path.exists(self.test_dir), (msg % self.test_dir)
    97                 test_dir = os.path.dirname(inspect.stack()[-1][1])
   123             # windmill setup
    98             return osp.join(test_dir, 'windmill')
   124             windmill.stdout, windmill.stdin = sys.stdout, sys.stdin
       
   125             configure_global_settings()
       
   126             windmill.settings['TEST_URL'] = self.config['base-url']
       
   127             if hasattr(self,"windmill_settings"):
       
   128                 for (setting,value) in self.windmill_settings.iteritems():
       
   129                     windmill.settings[setting] = value
       
   130             self.windmill_shell_objects = setup()
    99 
   131 
   100     def setUp(self):
   132         def tearDown(self):
   101         # Start CubicWeb session before running the server to populate self.vreg
   133             teardown(self.windmill_shell_objects)
   102         super(CubicWebWindmillUseCase, self).setUp()
   134             CubicWebServerTC.tearDown(self)
   103         # XXX reduce log output (should be done in a cleaner way)
       
   104         # windmill fu** up our logging configuration
       
   105         for logkey in ('windmill', 'logilab', 'cubicweb'):
       
   106             getLogger(logkey).setLevel(ERROR)
       
   107         self.test_dir = self._test_dir()
       
   108         msg = "provide a valid 'test_dir' as the given test file/dir (current: %s)"
       
   109         assert os.path.exists(self.test_dir), (msg % self.test_dir)
       
   110         # windmill setup
       
   111         windmill.stdout, windmill.stdin = sys.stdout, sys.stdin
       
   112         configure_global_settings()
       
   113         windmill.settings['TEST_URL'] = self.config['base-url']
       
   114         if hasattr(self,"windmill_settings"):
       
   115             for (setting,value) in self.windmill_settings.iteritems():
       
   116                 windmill.settings[setting] = value
       
   117         self.windmill_shell_objects = setup()
       
   118 
   135 
   119     def tearDown(self):
   136         def testWindmill(self):
   120         teardown(self.windmill_shell_objects)
   137             if self.edit_test:
   121         super(CubicWebWindmillUseCase, self).tearDown()
   138                 # see windmill.bin.admin_options.Firebug
       
   139                 windmill.settings['INSTALL_FIREBUG'] = 'firebug'
       
   140                 windmill.settings.setdefault('MOZILLA_PLUGINS', []).extend(
       
   141                     ['/usr/share/mozilla-extensions/',
       
   142                      '/usr/share/xul-ext/'])
       
   143             controller = self.windmill_shell_objects['start_' + self.browser]()
       
   144             self.windmill_shell_objects['do_test'](self.test_dir,
       
   145                                                    load=self.edit_test,
       
   146                                                    threaded=False)
       
   147             # set a breakpoint to be able to debug windmill test
       
   148             if self.edit_test:
       
   149                 import pdb; pdb.set_trace()
       
   150                 return
   122 
   151 
   123     def testWindmill(self):
   152             # reporter
   124         if self.edit_test:
   153             for test in unittestreporter.test_list:
   125             # see windmill.bin.admin_options.Firebug
   154                 msg = ""
   126             windmill.settings['INSTALL_FIREBUG'] = 'firebug'
   155                 self._testMethodDoc = getattr(test, "__doc__", None)
   127             windmill.settings.setdefault('MOZILLA_PLUGINS', []).extend(
   156                 self._testMethodName = test.__name__
   128                 ['/usr/share/mozilla-extensions/',
   157                 # try to display a better message in case of failure
   129                  '/usr/share/xul-ext/'])
   158                 if hasattr(test, "tb"):
   130         controller = self.windmill_shell_objects['start_' + self.browser]()
   159                     msg = '\n'.join(test.tb)
   131         self.windmill_shell_objects['do_test'](self.test_dir,
   160                 self.assertEqual(test.result, True, msg=msg)
   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 
   161 
   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