~ellisonbg/ipython/trunk-dev

« back to all changes in this revision

Viewing changes to IPython/testing/_paramtestpy3.py

  • Committer: Brian Granger
  • Date: 2010-01-31 23:57:46 UTC
  • mfrom: (1109.1.113 ipython)
  • Revision ID: ellisonbg@gmail.com-20100131235746-rj81qa8klooepq2m
Merging from upstream trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Implementation of the parametric test support for Python 3.x.
 
2
 
 
3
Thanks for the py3 version to Robert Collins, from the Testing in Python
 
4
mailing list.
 
5
"""
 
6
 
 
7
#-----------------------------------------------------------------------------
 
8
#  Copyright (C) 2009  The IPython Development Team
 
9
#
 
10
#  Distributed under the terms of the BSD License.  The full license is in
 
11
#  the file COPYING, distributed as part of this software.
 
12
#-----------------------------------------------------------------------------
 
13
 
 
14
#-----------------------------------------------------------------------------
 
15
# Imports
 
16
#-----------------------------------------------------------------------------
 
17
 
 
18
import unittest
 
19
from unittest import TestSuite
 
20
 
 
21
#-----------------------------------------------------------------------------
 
22
# Classes and functions
 
23
#-----------------------------------------------------------------------------
 
24
 
 
25
 
 
26
def isgenerator(func):
 
27
    return hasattr(func,'_generator')
 
28
 
 
29
 
 
30
class IterCallableSuite(TestSuite):
 
31
   def __init__(self, iterator, adapter):
 
32
       self._iter = iterator
 
33
       self._adapter = adapter
 
34
   def __iter__(self):
 
35
       yield self._adapter(self._iter.__next__)
 
36
 
 
37
class ParametricTestCase(unittest.TestCase):
 
38
   """Write parametric tests in normal unittest testcase form.
 
39
 
 
40
   Limitations: the last iteration misses printing out a newline when
 
41
   running in verbose mode.
 
42
   """
 
43
 
 
44
   def run(self, result=None):
 
45
       testMethod = getattr(self, self._testMethodName)
 
46
       # For normal tests, we just call the base class and return that
 
47
       if isgenerator(testMethod):
 
48
           def adapter(next_test):
 
49
               return unittest.FunctionTestCase(next_test,
 
50
                                                self.setUp,
 
51
                                                self.tearDown)
 
52
 
 
53
           return IterCallableSuite(testMethod(),adapter).run(result)
 
54
       else:
 
55
           return super(ParametricTestCase, self).run(result)
 
56
 
 
57
 
 
58
def parametric(func):
 
59
   """Decorator to make a simple function into a normal test via
 
60
unittest."""
 
61
   # Hack, until I figure out how to write isgenerator() for python3!!
 
62
   func._generator = True
 
63
 
 
64
   class Tester(ParametricTestCase):
 
65
       test = staticmethod(func)
 
66
 
 
67
   Tester.__name__ = func.__name__
 
68
 
 
69
   return Tester