cubicweb/devtools/qunit.py
changeset 11076 403a901b6b1e
parent 11057 0b59724cb3f2
child 11165 c6fe858f6b90
equal deleted inserted replaced
11073:d7e8912549cd 11076:403a901b6b1e
    23 from subprocess import Popen, PIPE, STDOUT
    23 from subprocess import Popen, PIPE, STDOUT
    24 
    24 
    25 from six.moves.queue import Queue, Empty
    25 from six.moves.queue import Queue, Empty
    26 
    26 
    27 # imported by default to simplify further import statements
    27 # imported by default to simplify further import statements
    28 from logilab.common.testlib import unittest_main, with_tempdir, InnerTest, Tags
    28 from logilab.common.testlib import unittest_main, with_tempdir, Tags
    29 import webtest.http
    29 import webtest.http
    30 
    30 
    31 import cubicweb
    31 import cubicweb
    32 from cubicweb.view import View
    32 from cubicweb.view import View
    33 from cubicweb.web.controller import Controller
    33 from cubicweb.web.controller import Controller
   107             test_file = args[0]
   107             test_file = args[0]
   108             if len(args) > 1:
   108             if len(args) > 1:
   109                 depends = args[1]
   109                 depends = args[1]
   110             else:
   110             else:
   111                 depends = ()
   111                 depends = ()
   112             for js_test in self._test_qunit(test_file, depends):
   112             for name, func, args in self._test_qunit(test_file, depends):
   113                 yield js_test
   113                 with self.subTest(name=name):
       
   114                     func(*args)
   114 
   115 
   115     @with_tempdir
   116     @with_tempdir
   116     def _test_qunit(self, test_file, depends=(), timeout=10):
   117     def _test_qunit(self, test_file, depends=(), timeout=10):
   117         QUnitView.test_file = test_file
   118         QUnitView.test_file = test_file
   118         QUnitView.depends = depends
   119         QUnitView.depends = depends
   125         if not isavailable:
   126         if not isavailable:
   126             self.fail('firefox not available or not working properly (%s)' % reason)
   127             self.fail('firefox not available or not working properly (%s)' % reason)
   127         browser.start(self.config['base-url'] + "?vid=qunit")
   128         browser.start(self.config['base-url'] + "?vid=qunit")
   128         test_count = 0
   129         test_count = 0
   129         error = False
   130         error = False
   130         def raise_exception(cls, *data):
   131 
   131             raise cls(*data)
   132         def runtime_error(*data):
       
   133             raise RuntimeError(*data)
       
   134 
   132         while not error:
   135         while not error:
   133             try:
   136             try:
   134                 result, test_name, msg = self.test_queue.get(timeout=timeout)
   137                 result, test_name, msg = self.test_queue.get(timeout=timeout)
   135                 test_name = '%s (%s)' % (test_name, test_file)
   138                 test_name = '%s (%s)' % (test_name, test_file)
   136                 self.set_description(test_name)
   139                 self.set_description(test_name)
   137                 if result is None:
   140                 if result is None:
   138                     break
   141                     break
   139                 test_count += 1
   142                 test_count += 1
   140                 if result:
   143                 if result:
   141                     yield InnerTest(test_name, lambda : 1)
   144                     yield test_name, lambda *args: 1, ()
   142                 else:
   145                 else:
   143                     yield InnerTest(test_name, self.fail, msg)
   146                     yield test_name, self.fail, (msg, )
   144             except Empty:
   147             except Empty:
   145                 error = True
   148                 error = True
   146                 msg = '%s inactivity timeout (%is). %i test results received'
   149                 msg = '%s inactivity timeout (%is). %i test results received'
   147                 yield InnerTest(test_file, raise_exception, RuntimeError,
   150                 yield test_file, runtime_error, (msg % (test_file, timeout, test_count), )
   148                                  msg % (test_file, timeout, test_count))
       
   149         browser.stop()
   151         browser.stop()
   150         if test_count <= 0 and not error:
   152         if test_count <= 0 and not error:
   151             yield InnerTest(test_name, raise_exception, RuntimeError,
   153             yield test_name, runtime_error, ('No test yielded by qunit for %s' % test_file, )
   152                             'No test yielded by qunit for %s' % test_file)
       
   153 
   154 
   154 class QUnitResultController(Controller):
   155 class QUnitResultController(Controller):
   155 
   156 
   156     __regid__ = 'qunit_result'
   157     __regid__ = 'qunit_result'
   157 
   158