5 from nose.pyversion import unbound_method
6 # don't import * -- some util functions look testlike
11 class TestUtils(unittest.TestCase):
13 def test_file_like(self):
14 file_like = util.file_like
15 assert file_like('a/file')
16 assert file_like('file.py')
17 assert file_like('/some/file.py')
18 assert not file_like('a.file')
19 assert not file_like('some.package')
20 assert file_like('a-file')
21 assert not file_like('test')
23 def test_split_test_name(self):
24 split_test_name = util.split_test_name
25 assert split_test_name('a.package:Some.method') == \
26 (None, 'a.package', 'Some.method')
27 assert split_test_name('some.module') == \
28 (None, 'some.module', None)
29 assert split_test_name('this/file.py:func') == \
30 (np('this/file.py'), None, 'func')
31 assert split_test_name('some/file.py') == \
32 (np('some/file.py'), None, None)
33 assert split_test_name(':Baz') == \
35 assert split_test_name('foo:bar/baz.py') == \
36 (np('foo:bar/baz.py'), None, None)
38 def test_split_test_name_windows(self):
40 stn = util.split_test_name
41 self.assertEqual(stn(r'c:\some\path.py:a_test'),
42 (np(r'c:\some\path.py'), None, 'a_test'))
43 self.assertEqual(stn(r'c:\some\path.py'),
44 (np(r'c:\some\path.py'), None, None))
45 self.assertEqual(stn(r'c:/some/other/path.py'),
46 (np(r'c:/some/other/path.py'), None, None))
47 self.assertEqual(stn(r'c:/some/other/path.py:Class.test'),
48 (np(r'c:/some/other/path.py'), None, 'Class.test'))
50 stn('cat:dog:something')
54 self.fail("Nonsense test name should throw ValueError")
56 def test_test_address(self):
57 # test addresses are specified as
58 # package.module:class.method
59 # /path/to/file.py:class.method
60 # converted into 3-tuples (file, module, callable)
62 test_address = util.test_address
63 absfile = util.absfile
72 class FooTC(unittest.TestCase):
78 class CustomTestType(type):
80 class CustomTC(unittest.TestCase):
81 __metaclass__ = CustomTestType
87 foo_funct = case.FunctionTestCase(baz)
88 foo_functu = unittest.FunctionTestCase(baz)
90 foo_mtc = case.MethodTestCase(unbound_method(Foo, Foo.bar))
92 me = util.src(absfile(__file__))
93 self.assertEqual(test_address(baz),
94 (me, __name__, 'baz'))
95 assert test_address(Foo) == (me, __name__, 'Foo')
96 assert test_address(unbound_method(Foo, Foo.bar)) == (me, __name__,
98 assert test_address(f) == (me, __name__, 'Foo')
99 assert test_address(f.bar) == (me, __name__, 'Foo.bar')
100 assert test_address(nose) == (
101 util.src(absfile(nose.__file__)), 'nose', None)
103 # test passing the actual test callable, as the
104 # missed test plugin must do
105 self.assertEqual(test_address(FooTC('test_one')),
106 (me, __name__, 'FooTC.test_one'))
107 self.assertEqual(test_address(CustomTC('test_one')),
108 (me, __name__, 'CustomTC.test_one'))
109 self.assertEqual(test_address(foo_funct),
110 (me, __name__, 'baz'))
111 self.assertEqual(test_address(foo_functu),
112 (me, __name__, 'baz'))
113 self.assertEqual(test_address(foo_mtc),
114 (me, __name__, 'Foo.bar'))
116 def test_isclass_detects_classes(self):
117 class TC(unittest.TestCase):
121 class TC_object(object):
123 # issue153 -- was not detecting custom typed classes...
126 class TC_custom_type(object):
127 __metaclass__ = TCType
128 class TC_unittest_custom_type(unittest.TestCase):
129 __metaclass__ = TCType
131 assert util.isclass(TC), "failed to detect %s as class" % TC
132 assert util.isclass(TC_Classic), "failed to detect %s as class" % TC_Classic
133 assert util.isclass(TC_object), "failed to detect %s as class" % TC_object
134 assert util.isclass(TC_custom_type), "failed to detect %s as class" % TC_custom_type
135 assert util.isclass(TC_unittest_custom_type), "failed to detect %s as class" % TC_unittest_custom_type
137 def test_isclass_ignores_nonclass_things(self):
140 assert not util.isclass(anint), "should have ignored %s" % type(anint)
141 assert not util.isclass(adict), "should have ignored %s" % type(adict)
143 def test_tolist(self):
145 assert tolist('foo') == ['foo']
146 assert tolist(['foo', 'bar']) == ['foo', 'bar']
147 assert tolist('foo,bar') == ['foo', 'bar']
148 self.assertEqual(tolist('.*foo/.*,.1'), ['.*foo/.*', '.1'])
150 def test_try_run(self):
151 try_run = util.try_run
165 def __call__(self, mod):
168 foo = imp.new_module('foo')
172 foo.i_bar_m = Bar_m()
174 try_run(foo, ('bar',))
175 try_run(foo, ('bar_m',))
176 try_run(foo, ('i_bar',))
177 try_run(foo, ('i_bar_m',))
179 if __name__ == '__main__':