~jayvdb/testscenarios/0.4

« back to all changes in this revision

Viewing changes to lib/testscenarios/testcase.py

  • Committer: Robert Collins
  • Date: 2012-04-04 10:45:22 UTC
  • mfrom: (18.2.1 testcase-mixin)
  • Revision ID: robertc@robertcollins.net-20120404104522-7fho5wxapc2uqmw5
* ``TestWithScenarios`` is now backed by a mixin - WithScenarios - which can be
  mixed into different unittest implementations more cleanly (e.g. unittest2).
  (James Polley, Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
__all__ = [
18
18
    'TestWithScenarios',
 
19
    'WithScenarios',
19
20
    ]
20
21
 
21
22
import unittest
24
25
 
25
26
from testscenarios.scenarios import generate_scenarios
26
27
 
27
 
class TestWithScenarios(unittest.TestCase):
28
 
    """A TestCase with support for scenarios via a scenarios attribute.
29
 
    
30
 
    When a test object which is an instance of TestWithScenarios is run,
31
 
    and there is a non-empty scenarios attribute on the object, the test is
32
 
    multiplied by the run method into one test per scenario. For this to work
33
 
    reliably the TestWithScenarios.run method must not be overriden in a
34
 
    subclass (or overridden compatibly with TestWithScenarios).
 
28
_doc = """
 
29
    When a test object which inherits from WithScenarios is run, and there is a
 
30
    non-empty scenarios attribute on the object, the test is multiplied by the
 
31
    run method into one test per scenario. For this to work reliably the
 
32
    WithScenarios.run method must not be overriden in a subclass (or overridden
 
33
    compatibly with WithScenarios).
35
34
    """
36
35
 
 
36
class WithScenarios(object):
 
37
    __doc__ = """A mixin for TestCase with support for declarative scenarios.
 
38
    """ + _doc
 
39
 
37
40
    def _get_scenarios(self):
38
41
        return getattr(self, 'scenarios', None)
39
42
 
50
53
            for test in generate_scenarios(self):
51
54
                test.debug()
52
55
        else:
53
 
            return super(TestWithScenarios, self).debug()
 
56
            return super(WithScenarios, self).debug()
54
57
 
55
58
    def run(self, result=None):
56
59
        scenarios = self._get_scenarios()
59
62
                test.run(result)
60
63
            return
61
64
        else:
62
 
            return super(TestWithScenarios, self).run(result)
 
65
            return super(WithScenarios, self).run(result)
 
66
 
 
67
 
 
68
class TestWithScenarios(WithScenarios, unittest.TestCase):
 
69
    __doc__ = """Unittest TestCase with support for declarative scenarios.
 
70
    """ + _doc