|
1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """this module contains base classes for windmill integration |
|
19 |
|
20 :todo: |
|
21 |
|
22 * import CubicWeb session object into windmill scope to be able to run RQL |
|
23 * manage command line option from pytest to run specific use tests only |
|
24 """ |
|
25 |
|
26 |
|
27 import os, os.path as osp |
|
28 from logging import getLogger, ERROR |
|
29 import sys |
|
30 |
|
31 # imported by default to simplify further import statements |
|
32 from logilab.common.testlib import TestCase, unittest_main |
|
33 |
|
34 import windmill |
|
35 from windmill.dep import functest |
|
36 from windmill.bin.admin_lib import configure_global_settings, setup, teardown |
|
37 |
|
38 from cubicweb.devtools.httptest import CubicWebServerTC, CubicWebServerConfig |
|
39 |
|
40 |
|
41 # Excerpt from :ref:`windmill.authoring.unit` |
|
42 class UnitTestReporter(functest.reports.FunctestReportInterface): |
|
43 def summary(self, test_list, totals_dict, stdout_capture): |
|
44 self.test_list = test_list |
|
45 |
|
46 unittestreporter = UnitTestReporter() |
|
47 functest.reports.register_reporter(unittestreporter) |
|
48 |
|
49 class CubicWebWindmillUseCase(CubicWebServerTC): |
|
50 """basic class for Windmill use case tests |
|
51 |
|
52 If you want to change cubicweb test server parameters, define a new |
|
53 :class:`CubicWebServerConfig` and override the :var:`configcls` |
|
54 attribute: |
|
55 |
|
56 configcls = CubicWebServerConfig |
|
57 |
|
58 From Windmill configuration: |
|
59 |
|
60 .. attribute:: browser |
|
61 identification string (firefox|ie|safari|chrome) (firefox by default) |
|
62 .. attribute :: edit_test |
|
63 load and edit test for debugging (False by default) |
|
64 .. attribute:: test_dir (optional) |
|
65 testing file path or directory (windmill directory under your unit case |
|
66 file by default) |
|
67 |
|
68 Examples: |
|
69 |
|
70 browser = 'firefox' |
|
71 test_dir = osp.join(__file__, 'windmill') |
|
72 edit_test = False |
|
73 |
|
74 If you prefer, you can put here the use cases recorded by windmill GUI |
|
75 (services transformer) instead of the windmill sub-directory |
|
76 You can change `test_dir` as following: |
|
77 |
|
78 test_dir = __file__ |
|
79 |
|
80 Instead of toggle `edit_test` value, try `pytest -i` |
|
81 """ |
|
82 browser = 'firefox' |
|
83 edit_test = "-i" in sys.argv # detection for pytest invocation |
|
84 # Windmill use case are written with no anonymous user |
|
85 anonymous_logged = False |
|
86 |
|
87 def _test_dir(self): |
|
88 """access to class attribute if possible or make assumption |
|
89 of expected directory""" |
|
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 |
|
100 def setUp(self): |
|
101 # Start CubicWeb session before running the server to populate self.vreg |
|
102 CubicWebServerTC.setUp(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 |
|
119 def tearDown(self): |
|
120 teardown(self.windmill_shell_objects) |
|
121 CubicWebServerTC.tearDown(self) |
|
122 |
|
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 |