5
6
from nose.config import Config
6
7
from nose.selector import log, Selector
7
8
from nose.util import absdir
10
def __init__(self, name):
13
11
class TestSelector(unittest.TestCase):
15
def test_anytest(self):
17
def exact_file_a(filename, modname, funcname):
20
return filename == '/a/file.py'
22
def exact_file_b(filename, modname, funcname):
25
return filename == '/a/nother/file.py'
27
def exact_module_a(filename, modname, funcname):
30
return modname == 'some.module'
32
def exact_module_b(filename, modname, funcname):
35
return modname == 'some.other.module'
39
s.tests = [ '/a/file.py', 'some.module' ]
41
# in these cases, there is a test that doesn't care
43
assert s.anytest(exact_file_a)
44
assert s.anytest(exact_file_b)
45
assert s.anytest(exact_module_a)
46
assert s.anytest(exact_module_b)
48
# no test matches file b
49
s.tests = [ '/a/file.py' ]
50
assert s.anytest(exact_file_a)
51
assert not s.anytest(exact_file_b)
52
assert s.anytest(exact_module_a)
53
assert s.anytest(exact_module_b)
55
s.tests = [ '/a/file.py', '/that/file.py' ]
56
assert s.anytest(exact_file_a)
57
assert not s.anytest(exact_file_b)
58
assert s.anytest(exact_module_a)
59
assert s.anytest(exact_module_b)
61
# no test matches module b
62
s.tests = [ 'some.module' ]
63
assert s.anytest(exact_file_a)
64
assert s.anytest(exact_file_b)
65
assert s.anytest(exact_module_a)
66
assert not s.anytest(exact_module_b)
68
# no test matches module b
69
s.tests = [ 'some.module', 'blah.blah' ]
70
assert s.anytest(exact_file_a)
71
assert s.anytest(exact_file_b)
72
assert s.anytest(exact_module_a)
73
assert not s.anytest(exact_module_b)
14
logging.getLogger('nose.selector').setLevel(logging.WARN)
75
16
def test_exclude(self):
76
17
s = Selector(Config())
78
c.exclude = re.compile(r'me')
19
c.exclude = [re.compile(r'me')]
81
22
assert s.matches('test_foo')
86
27
def test_include(self):
87
28
s = Selector(Config())
89
c.include = re.compile(r'me')
30
c.include = [re.compile(r'me')]
92
33
assert s.matches('test')
93
34
assert s2.matches('test')
94
35
assert not s.matches('meatball')
95
36
assert s2.matches('meatball')
37
assert not s.matches('toyota')
38
assert not s2.matches('toyota')
40
c.include.append(re.compile('toy'))
41
assert s.matches('test')
42
assert s2.matches('test')
43
assert not s.matches('meatball')
44
assert s2.matches('meatball')
45
assert not s.matches('toyota')
46
assert s2.matches('toyota')
97
48
def test_want_class(self):
56
def __new__(cls, name, bases, dct):
57
return type.__new__(cls, name, bases, dct)
58
class TestClass(object):
59
__metaclass__ = TestType
105
61
s = Selector(Config())
106
62
assert not s.wantClass(Foo)
107
63
assert s.wantClass(Bar)
108
64
assert s.wantClass(TestMe)
111
assert s.wantClass(Bar)
112
assert not s.wantClass(Foo)
113
assert not s.wantClass(TestMe)
115
s.tests = [ 'something:Bar' ]
116
assert not s.wantClass(Bar)
117
assert not s.wantClass(Foo)
118
assert not s.wantClass(TestMe)
120
s.tests = [ '%s:Bar' % __name__ ]
121
assert s.wantClass(Bar)
122
assert not s.wantClass(Foo)
123
assert not s.wantClass(TestMe)
125
s.tests = [ __name__ ]
126
assert s.wantClass(Bar)
127
assert not s.wantClass(Foo)
128
assert s.wantClass(TestMe)
130
s.tests = [ __file__ ]
131
assert s.wantClass(Bar)
132
assert not s.wantClass(Foo)
133
assert s.wantClass(TestMe)
65
assert s.wantClass(TestClass)
67
TestMe.__test__ = False
68
assert not s.wantClass(TestMe), "Failed to respect __test__ = False"
70
assert not s.wantClass(Bar), "Failed to respect __test__ = False"
135
72
def test_want_directory(self):
136
73
s = Selector(Config())
166
110
assert s.wantFile('test.py')
167
111
assert s.wantFile('foo/test_foo.py')
168
assert s.wantFile('bar/baz/test.py', package='baz')
169
assert not s.wantFile('foo.py', package='bar.baz')
112
assert s.wantFile('bar/baz/test.py')
113
assert not s.wantFile('foo.py')
170
114
assert not s.wantFile('test_data.txt')
171
assert not s.wantFile('data.text', package='bar.bz')
172
assert not s.wantFile('bar/baz/__init__.py', package='baz')
174
s.tests = [ 'test.py', 'other/file.txt' ]
175
assert s.wantFile('test.py')
176
assert not s.wantFile('foo/test_foo.py')
177
assert not s.wantFile('bar/baz/test.py', package='baz')
178
# still not a python module... some plugin might want it,
179
# but the default selector doesn't
180
assert not s.wantFile('other/file.txt')
182
s.tests = [ 'a.module' ]
183
assert not s.wantFile('test.py')
184
assert not s.wantFile('foo/test_foo.py')
185
assert not s.wantFile('test-dir/test.py', package='baz')
186
assert not s.wantFile('other/file.txt')
187
assert s.wantFile('/path/to/a/module.py')
188
assert s.wantFile('/another/path/to/a/module/file.py')
189
assert not s.wantFile('/path/to/a/module/data/file.txt')
115
assert not s.wantFile('data.text')
116
assert not s.wantFile('bar/baz/__init__.py')
191
118
def test_want_function(self):
143
def test_not_test(self):
145
test_not_test.__test__ = False
218
147
s = Selector(Config())
220
149
assert s.wantMethod(Baz.test_me)
221
150
assert s.wantMethod(Baz.test_too)
222
151
assert not s.wantMethod(Baz.other)
224
s.tests = [ ':Baz.test_too' ]
225
assert s.wantMethod(Baz.test_too)
226
assert not s.wantMethod(Baz.test_me)
227
assert not s.wantMethod(Baz.other)
152
assert not s.wantMethod(Baz.test_not_test), \
153
"Failed to respect __test__ = False"
229
155
def test_want_module(self):
231
m2 = Mod('this.that')
232
m3 = Mod('this.that.another')
233
m4 = Mod('this.that.another.one')
234
m5 = Mod('test.something')
157
m2 = mod('this.that')
158
m3 = mod('this.that.another')
159
m4 = mod('this.that.another.one')
160
m5 = mod('test.something')
239
165
s = Selector(Config())
240
166
assert not s.wantModule(m)
244
170
assert not s.wantModule(m5)
245
171
assert s.wantModule(m6)
246
172
assert s.wantModule(m7)
247
assert not s.wantModule(m8)
249
s.tests = [ 'this.that.another' ]
250
assert not s.wantModule(m)
251
assert s.wantModule(m2)
252
assert s.wantModule(m3)
253
assert s.wantModule(m4)
254
assert not s.wantModule(m5)
255
assert not s.wantModule(m6)
256
assert not s.wantModule(m7)
257
assert not s.wantModule(m8)
259
def test_want_module_tests(self):
261
m2 = Mod('this.that')
262
m3 = Mod('this.that.another')
263
m4 = Mod('this.that.another.one')
264
m5 = Mod('test.something')
269
s = Selector(Config())
270
assert not s.wantModuleTests(m)
271
assert not s.wantModuleTests(m2)
272
assert not s.wantModuleTests(m3)
273
assert not s.wantModuleTests(m4)
274
assert not s.wantModuleTests(m5)
275
assert s.wantModuleTests(m6)
276
assert s.wantModuleTests(m7)
277
assert s.wantModuleTests(m8)
279
s.tests = [ 'this.that.another' ]
280
assert not s.wantModuleTests(m)
281
assert not s.wantModuleTests(m2)
282
assert s.wantModuleTests(m3)
283
assert s.wantModuleTests(m4)
284
assert not s.wantModuleTests(m5)
285
assert not s.wantModuleTests(m6)
286
assert not s.wantModuleTests(m7)
287
assert not s.wantModuleTests(m8)
289
def test_module_in_tests(self):
290
s = Selector(Config())
291
# s.tests = [ 'ever', 'what', 'what.ever' ]
295
w_e = Mod('what.ever')
296
w_n = Mod('what.not')
297
f_e = Mod('for.ever')
300
assert s.moduleInTests(w)
301
assert s.moduleInTests(w, True)
302
assert s.moduleInTests(w_e)
303
assert s.moduleInTests(w_e, True)
304
assert s.moduleInTests(w_n)
305
assert s.moduleInTests(w_n, True)
306
assert not s.moduleInTests(we)
307
assert not s.moduleInTests(we, True)
308
assert not s.moduleInTests(f_e)
309
assert not s.moduleInTests(f_e, True)
311
s.tests = [ 'what.ever' ]
312
assert not s.moduleInTests(w)
313
assert s.moduleInTests(w, True)
314
assert s.moduleInTests(w_e)
315
assert s.moduleInTests(w_e, True)
316
assert not s.moduleInTests(w_n)
317
assert not s.moduleInTests(w_n, True)
318
assert not s.moduleInTests(we)
319
assert not s.moduleInTests(we, True)
320
assert not s.moduleInTests(f_e)
321
assert not s.moduleInTests(f_e, True)
323
s.tests = [ 'what.ever', 'what.not' ]
324
assert not s.moduleInTests(w)
325
assert s.moduleInTests(w, True)
326
assert s.moduleInTests(w_e)
327
assert s.moduleInTests(w_e, True)
328
assert s.moduleInTests(w_n)
329
assert s.moduleInTests(w_n, True)
330
assert not s.moduleInTests(we)
331
assert not s.moduleInTests(we, True)
332
assert not s.moduleInTests(f_e)
333
assert not s.moduleInTests(f_e, True)
173
assert s.wantModule(m8)
176
assert not s.wantModule(m6), "Failed to respect __test__ = False"
335
179
if __name__ == '__main__':
337
logging.getLogger('nose.selector').setLevel(logging.DEBUG)
338
logging.basicConfig()
339
180
# log.setLevel(logging.DEBUG)