2 from twisted.trial import unittest
5 from twisted.trial import unittest
7 class FooTest(unittest.TestCase):
12 dosModule = testModule.replace('\n', '\r\n')
16 '''This module is used by test_loader to test the Trial test loading
17 functionality. Do NOT change the number of tests in this module.
18 Do NOT change the names the tests in this module.
21 import unittest as pyunit
22 from twisted.trial import unittest
24 class FooTest(unittest.TestCase):
32 class PyunitTest(pyunit.TestCase):
40 class NotATest(object):
45 class AlphabetTest(unittest.TestCase):
56 testInheritanceSample = """
57 '''This module is used by test_loader to test the Trial test loading
58 functionality. Do NOT change the number of tests in this module.
59 Do NOT change the names the tests in this module.
62 from twisted.trial import unittest
69 class A(unittest.TestCase, X):
72 class B(unittest.TestCase, X):
77 class PackageTest(unittest.TestCase):
79 ('badpackage/__init__.py', 'frotz\n'),
80 ('badpackage/test_module.py', ''),
81 ('package2/__init__.py', ''),
82 ('package2/test_module.py', 'import frotz\n'),
83 ('package/__init__.py', ''),
84 ('package/frotz.py', 'frotz\n'),
85 ('package/test_bad_module.py',
86 'raise ZeroDivisionError("fake error")'),
87 ('package/test_dos_module.py', dosModule),
88 ('package/test_import_module.py', 'import frotz'),
89 ('package/test_module.py', testModule),
90 ('goodpackage/__init__.py', ''),
91 ('goodpackage/test_sample.py', testSample),
92 ('goodpackage/sub/__init__.py', ''),
93 ('goodpackage/sub/test_sample.py', testSample),
94 ('inheritancepackage/__init__.py', ''),
95 ('inheritancepackage/test_x.py', testInheritanceSample),
98 def _toModuleName(self, filename):
99 name = os.path.splitext(filename)[0]
100 segs = name.split('/')
101 if segs[-1] == '__init__':
103 return '.'.join(segs)
105 def getModules(self):
106 return map(self._toModuleName, zip(*self.files)[0])
108 def cleanUpModules(self):
109 modules = self.getModules()
112 for module in modules:
114 del sys.modules[module]
118 def createFiles(self, files, parentDir='.'):
119 for filename, contents in self.files:
120 filename = os.path.join(parentDir, filename)
121 self._createDirectory(filename)
122 fd = open(filename, 'w')
126 def _createDirectory(self, filename):
127 directory = os.path.dirname(filename)
128 if not os.path.exists(directory):
129 os.makedirs(directory)
131 def setUp(self, parentDir=None):
132 if parentDir is None:
133 parentDir = self.mktemp()
134 self.parent = parentDir
135 self.createFiles(self.files, parentDir)
138 self.cleanUpModules()
140 class SysPathManglingTest(PackageTest):
141 def setUp(self, parent=None):
142 self.oldPath = sys.path[:]
143 self.newPath = sys.path[:]
145 parent = self.mktemp()
146 PackageTest.setUp(self, parent)
147 self.newPath.append(self.parent)
148 self.mangleSysPath(self.newPath)
151 PackageTest.tearDown(self)
152 self.mangleSysPath(self.oldPath)
154 def mangleSysPath(self, pathVar):
155 sys.path[:] = pathVar