File: _paramtestpy3.py

package info (click to toggle)
ipython 0.13.1-2%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,752 kB
  • sloc: python: 69,537; makefile: 355; lisp: 272; sh: 80; objc: 37
file content (71 lines) | stat: -rw-r--r-- 2,382 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Implementation of the parametric test support for Python 3.x.

Thanks for the py3 version to Robert Collins, from the Testing in Python
mailing list.
"""

#-----------------------------------------------------------------------------
#  Copyright (C) 2009-2011  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import unittest
from unittest import TestSuite

#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------


def isgenerator(func):
    return hasattr(func,'_generator')


class IterCallableSuite(TestSuite):
   def __init__(self, iterator, adapter):
       self._iter = iterator
       self._adapter = adapter
   def __iter__(self):
       yield self._adapter(self._iter.__next__)

class ParametricTestCase(unittest.TestCase):
   """Write parametric tests in normal unittest testcase form.

   Limitations: the last iteration misses printing out a newline when
   running in verbose mode.
   """

   def run(self, result=None):
       testMethod = getattr(self, self._testMethodName)
       # For normal tests, we just call the base class and return that
       if isgenerator(testMethod):
           def adapter(next_test):
               ftc = unittest.FunctionTestCase(next_test,
                                               self.setUp,
                                               self.tearDown)
               self._nose_case = ftc   # Nose 1.0 rejects the test without this
               return ftc

           return IterCallableSuite(testMethod(),adapter).run(result)
       else:
           return super(ParametricTestCase, self).run(result)


def parametric(func):
   """Decorator to make a simple function into a normal test via
unittest."""
   # Hack, until I figure out how to write isgenerator() for python3!!
   func._generator = True

   class Tester(ParametricTestCase):
       test = staticmethod(func)

   Tester.__name__ = func.__name__

   return Tester