[devtools/testlib] Conditionally by-pass logilab-common's TestCase.__call__ method

The latter claims to be a copy-paste of unittest.TestCase.__call__, but it appears that the copy-paste is a bit outdated and no longer valid as far as Python 3.4 unittest is concerned where subTest API got introduced. In particular the doCleanups method is not called resulting in errors such as

[...]
  File "/usr/lib/python3.4/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/usr/lib/python3.4/unittest/case.py", line 487, in subTest
    if not self._outcome.result_supports_subtests:
AttributeError: 'NoneType' object has no attribute 'result_supports_subtests'

So by-pass lgc.TestCase.__call__ method unless we are explicitly in a generative tests (in the sense of logilab-common) situation.

authorDenis Laxalde <denis.laxalde@logilab.fr>
changeset4483a4cc197f
branchdefault
phasedraft
hiddenyes
parent revision#2dafcdd19c99 pep8 bits
child revision<not specified>
files modified by this revision
cubicweb/devtools/testlib.py
# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1455787385 -3600
# Thu Feb 18 10:23:05 2016 +0100
# Node ID 4483a4cc197f03c27651207051e84b442656cb42
# Parent 2dafcdd19c9918f6d0bde86b6afdcaf07c1722b3
[devtools/testlib] Conditionally by-pass logilab-common's TestCase.__call__ method

The latter claims to be a copy-paste of unittest.TestCase.__call__, but it
appears that the copy-paste is a bit outdated and no longer valid as far as
Python 3.4 unittest is concerned where subTest API got introduced. In
particular the `doCleanups` method is not called resulting in errors such as

::

[...]
File "/usr/lib/python3.4/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/usr/lib/python3.4/unittest/case.py", line 487, in subTest
if not self._outcome.result_supports_subtests:
AttributeError: 'NoneType' object has no attribute 'result_supports_subtests'

So by-pass lgc.TestCase.__call__ method unless we are explicitly in a
generative tests (in the sense of logilab-common) situation.

diff --git a/cubicweb/devtools/testlib.py b/cubicweb/devtools/testlib.py
@@ -21,10 +21,11 @@
1  import sys
2  import re
3  from os.path import dirname, join, abspath
4  from math import log
5  from contextlib import contextmanager
6 +from inspect import isgeneratorfunction
7  from itertools import chain
8 
9  from six import text_type, string_types
10  from six.moves import range
11  from six.moves.urllib.parse import urlparse, parse_qs, unquote as urlunquote
@@ -293,10 +294,21 @@
12          self._admin_session = None
13          self.repo = None
14          self._open_access = set()
15          super(CubicWebTC, self).__init__(*args, **kwargs)
16 
17 +    def __call__(self, *args, **kwds):
18 +        testMethod = self._get_test_method()
19 +        if not isgeneratorfunction(testMethod):
20 +            # By-pass lgc.TestCase.__call__ as it is not compatible with py3.4
21 +            # subTest (does not call doCleanups in particular).
22 +            return super(BaseTestCase, self).__call__(*args, **kwds)
23 +        else:
24 +            # Unless the test method is explicitely generative in the sense of
25 +            # lgc.testlib.
26 +            return super(CubicWebTC, self).__call__(*args, **kwds)
27 +
28      # repository connection handling ###########################################
29 
30      def new_access(self, login):
31          """provide a new RepoAccess object for a given user
32