1 from twisted.trial import unittest
2 from twisted.trial.runner import TestSuite, suiteVisit
4 pyunit = __import__('unittest')
8 class MockVisitor(object):
13 def __call__(self, testCase):
14 self.calls.append(testCase)
18 class TestTestVisitor(unittest.TestCase):
20 self.visitor = MockVisitor()
23 def test_visitCase(self):
25 Test that C{visit} works for a single test case.
27 testCase = TestTestVisitor('test_visitCase')
28 testCase.visit(self.visitor)
29 self.assertEqual(self.visitor.calls, [testCase])
32 def test_visitSuite(self):
34 Test that C{visit} hits all tests in a suite.
36 tests = [TestTestVisitor('test_visitCase'),
37 TestTestVisitor('test_visitSuite')]
38 testSuite = TestSuite(tests)
39 testSuite.visit(self.visitor)
40 self.assertEqual(self.visitor.calls, tests)
43 def test_visitEmptySuite(self):
45 Test that C{visit} on an empty suite hits nothing.
47 TestSuite().visit(self.visitor)
48 self.assertEqual(self.visitor.calls, [])
51 def test_visitNestedSuite(self):
53 Test that C{visit} recurses through suites.
55 tests = [TestTestVisitor('test_visitCase'),
56 TestTestVisitor('test_visitSuite')]
57 testSuite = TestSuite([TestSuite([test]) for test in tests])
58 testSuite.visit(self.visitor)
59 self.assertEqual(self.visitor.calls, tests)
62 def test_visitPyunitSuite(self):
64 Test that C{suiteVisit} visits stdlib unittest suites
66 test = TestTestVisitor('test_visitPyunitSuite')
67 suite = pyunit.TestSuite([test])
68 suiteVisit(suite, self.visitor)
69 self.assertEqual(self.visitor.calls, [test])
72 def test_visitPyunitCase(self):
74 Test that a stdlib test case in a suite gets visited.
76 class PyunitCase(pyunit.TestCase):
79 test = PyunitCase('test_foo')
80 TestSuite([test]).visit(self.visitor)
82 [call.id() for call in self.visitor.calls], [test.id()])