[testlib] Allow skipTest() and fail() in generative tests

Closes #1716420.

authorRémi Cardona <remi.cardona@logilab.fr>
changesetb643f601a028
branchdefault
phasepublic
hiddenno
parent revision#0e8b141caf64 [testlib] Update __all__
child revision#1254741a9ff2 [tests] Stop using deprecated assert_
files modified by this revision
logilab/common/testlib.py
test/unittest_testlib.py
# HG changeset patch
# User Rémi Cardona <remi.cardona@logilab.fr>
# Date 1441895855 -7200
# Thu Sep 10 16:37:35 2015 +0200
# Node ID b643f601a028c567681ebfe961d2b3850f7815be
# Parent 0e8b141caf6454cae1b20c53bd44c22ecadd241f
[testlib] Allow skipTest() and fail() in generative tests

Closes #1716420.

diff --git a/logilab/common/testlib.py b/logilab/common/testlib.py
@@ -570,10 +570,15 @@
1                      # XXX Don't stop anymore if an error occured
2                      #if status == 2:
3                      #    result.shouldStop = True
4                  if result.shouldStop: # either on error or on exitfirst + error
5                      break
6 +        except self.failureException:
7 +            result.addFailure(self, self.__exc_info())
8 +            success = False
9 +        except SkipTest as e:
10 +            result.addSkip(self, e)
11          except:
12              # if an error occurs between two yield
13              result.addError(self, self.__exc_info())
14              success = False
15          return success
diff --git a/test/unittest_testlib.py b/test/unittest_testlib.py
@@ -342,10 +342,35 @@
16          self.assertEqual(len(result.failures), 1)
17          self.assertEqual(len(result.errors), 0)
18          self.assertEqual(len(result.skipped), 0)
19 
20 
21 +    def test_generative_outer_failure(self):
22 +        class FooTC(TestCase):
23 +            def test_generative(self):
24 +                self.fail()
25 +                yield
26 +
27 +        result = self.runner.run(FooTC('test_generative'))
28 +        self.assertEqual(result.testsRun, 0)
29 +        self.assertEqual(len(result.failures), 1)
30 +        self.assertEqual(len(result.errors), 0)
31 +        self.assertEqual(len(result.skipped), 0)
32 +
33 +    def test_generative_outer_skip(self):
34 +        class FooTC(TestCase):
35 +            def test_generative(self):
36 +                self.skipTest('blah')
37 +                yield
38 +
39 +        result = self.runner.run(FooTC('test_generative'))
40 +        self.assertEqual(result.testsRun, 0)
41 +        self.assertEqual(len(result.failures), 0)
42 +        self.assertEqual(len(result.errors), 0)
43 +        self.assertEqual(len(result.skipped), 1)
44 +
45 +
46  class ExitFirstTC(TestCase):
47      def setUp(self):
48          output = StringIO()
49          self.runner = SkipAwareTextTestRunner(stream=output, exitfirst=True)
50