~ubuntu-branches/debian/squeeze/nose/squeeze

« back to all changes in this revision

Viewing changes to unit_tests/test_selector.py

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Marek, Torsten Marek, Gustavo Noronha Silva
  • Date: 2008-06-12 13:39:43 UTC
  • mfrom: (1.2.1 upstream) (2.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080612133943-2q7syp67fwl4on13
Tags: 0.10.3-1

[Torsten Marek]
* New upstream release (Closes: #461994)
* debian/control
  - bump standards version to 3.8.0, no changes necessary
  - add suggestions for python-coverage (Closes: #457053)
  - change dependency on python-setuptools into 
    python-pkg-resources (Closes: #468719)
  - added myself to uploaders

[Gustavo Noronha Silva]
* debian/control:
  - remove -1 from build-dep on setuptools

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import logging
1
2
import os
2
3
import re
3
4
import unittest
5
6
from nose.config import Config
6
7
from nose.selector import log, Selector
7
8
from nose.util import absdir
8
 
 
9
 
class Mod:
10
 
    def __init__(self, name):
11
 
        self.__name__ = name
 
9
from mock import mod
12
10
 
13
11
class TestSelector(unittest.TestCase):
14
12
 
15
 
    def test_anytest(self):
16
 
 
17
 
        def exact_file_a(filename, modname, funcname):
18
 
            if filename is None:
19
 
                return None
20
 
            return filename == '/a/file.py'
21
 
        
22
 
        def exact_file_b(filename, modname, funcname):
23
 
            if filename is None:
24
 
                return None
25
 
            return filename == '/a/nother/file.py'
26
 
 
27
 
        def exact_module_a(filename, modname, funcname):
28
 
            if modname is None:
29
 
                return None
30
 
            return modname == 'some.module'
31
 
 
32
 
        def exact_module_b(filename, modname, funcname):
33
 
            if modname is None:
34
 
                return None
35
 
            return modname == 'some.other.module'
36
 
 
37
 
        c = Config()
38
 
        s = Selector(c)
39
 
        s.tests = [ '/a/file.py', 'some.module' ]
40
 
 
41
 
        # in these cases, there is a test that doesn't care
42
 
        # so they all pass
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)
47
 
 
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)
54
 
 
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)
60
 
        
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)
67
 
 
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)
74
 
        
 
13
    def tearDown(self):
 
14
        logging.getLogger('nose.selector').setLevel(logging.WARN)
 
15
    
75
16
    def test_exclude(self):
76
17
        s = Selector(Config())
77
18
        c = Config()
78
 
        c.exclude = re.compile(r'me')        
 
19
        c.exclude = [re.compile(r'me')]
79
20
        s2 = Selector(c)
80
21
        
81
22
        assert s.matches('test_foo')
86
27
    def test_include(self):
87
28
        s = Selector(Config())
88
29
        c = Config()
89
 
        c.include = re.compile(r'me')
 
30
        c.include = [re.compile(r'me')]
90
31
        s2 = Selector(c)
91
32
 
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')
 
39
        
 
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')
96
47
        
97
48
    def test_want_class(self):
98
49
        class Foo:
101
52
            pass
102
53
        class TestMe:
103
54
            pass
 
55
        class TestType(type):
 
56
            def __new__(cls, name, bases, dct):
 
57
                return type.__new__(cls, name, bases, dct)
 
58
        class TestClass(object):
 
59
            __metaclass__ = TestType
104
60
        
105
61
        s = Selector(Config())
106
62
        assert not s.wantClass(Foo)
107
63
        assert s.wantClass(Bar)
108
64
        assert s.wantClass(TestMe)
109
 
 
110
 
        s.tests = [ ':Bar' ]
111
 
        assert s.wantClass(Bar)
112
 
        assert not s.wantClass(Foo)
113
 
        assert not s.wantClass(TestMe)
114
 
 
115
 
        s.tests = [ 'something:Bar' ]
116
 
        assert not s.wantClass(Bar)
117
 
        assert not s.wantClass(Foo)
118
 
        assert not s.wantClass(TestMe)
119
 
 
120
 
        s.tests = [ '%s:Bar' % __name__ ]
121
 
        assert s.wantClass(Bar)
122
 
        assert not s.wantClass(Foo)
123
 
        assert not s.wantClass(TestMe)
124
 
 
125
 
        s.tests = [ __name__ ]
126
 
        assert s.wantClass(Bar)
127
 
        assert not s.wantClass(Foo)
128
 
        assert s.wantClass(TestMe)
129
 
 
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)
 
66
 
 
67
        TestMe.__test__ = False
 
68
        assert not s.wantClass(TestMe), "Failed to respect __test__ = False"
 
69
        Bar.__test__ = False
 
70
        assert not s.wantClass(Bar), "Failed to respect __test__ = False"
134
71
        
135
72
    def test_want_directory(self):
136
73
        s = Selector(Config())
142
79
        # default src directory
143
80
        assert s.wantDirectory('lib')
144
81
        assert s.wantDirectory('src')
 
82
 
 
83
        # FIXME move to functional tests
145
84
        
146
85
        # this looks on disk for support/foo, which is a package
147
86
        here = os.path.abspath(os.path.dirname(__file__))
152
91
        assert not s.wantDirectory(support)        
153
92
        
154
93
    def test_want_file(self):
 
94
 
 
95
        #logging.getLogger('nose.selector').setLevel(logging.DEBUG)
 
96
        #logging.basicConfig()
 
97
        
155
98
        c = Config()
156
 
        c.where = absdir(os.path.join(os.path.dirname(__file__), 'support'))
 
99
        c.where = [absdir(os.path.join(os.path.dirname(__file__), 'support'))]
 
100
        base = c.where[0]
157
101
        s = Selector(c)
158
102
 
159
103
        assert not s.wantFile('setup.py')
165
109
        
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')
173
 
 
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')
181
 
 
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')
190
117
        
191
118
    def test_want_function(self):
192
119
        def foo():
201
128
        assert s.wantFunction(test_foo)
202
129
        assert not s.wantFunction(foo)
203
130
 
204
 
        s.tests = [ ':test_bar' ]
205
 
        assert s.wantFunction(test_bar)
206
 
        assert not s.wantFunction(test_foo)
207
 
        assert not s.wantFunction(foo)
 
131
        test_foo.__test__ = False
 
132
        assert not s.wantFunction(test_foo), \
 
133
               "Failed to respect __test__ = False"
208
134
 
209
135
    def test_want_method(self):
210
136
        class Baz:
214
140
                pass
215
141
            def other(self):
216
142
                pass
 
143
            def test_not_test(self):
 
144
                pass
 
145
            test_not_test.__test__ = False
217
146
            
218
147
        s = Selector(Config())
219
148
        
220
149
        assert s.wantMethod(Baz.test_me)
221
150
        assert s.wantMethod(Baz.test_too)
222
151
        assert not s.wantMethod(Baz.other)
223
 
 
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"
228
154
        
229
155
    def test_want_module(self):
230
 
        m = Mod('whatever')
231
 
        m2 = Mod('this.that')
232
 
        m3 = Mod('this.that.another')
233
 
        m4 = Mod('this.that.another.one')
234
 
        m5 = Mod('test.something')
235
 
        m6 = Mod('a.test')
236
 
        m7 = Mod('my_tests')
237
 
        m8 = Mod('__main__')
 
156
        m = mod('whatever')
 
157
        m2 = mod('this.that')
 
158
        m3 = mod('this.that.another')
 
159
        m4 = mod('this.that.another.one')
 
160
        m5 = mod('test.something')
 
161
        m6 = mod('a.test')
 
162
        m7 = mod('my_tests')
 
163
        m8 = mod('__main__')
238
164
        
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)
248
 
        
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)
258
 
        
259
 
    def test_want_module_tests(self):
260
 
        m = Mod('whatever')
261
 
        m2 = Mod('this.that')
262
 
        m3 = Mod('this.that.another')
263
 
        m4 = Mod('this.that.another.one')
264
 
        m5 = Mod('test.something')
265
 
        m6 = Mod('a.test')
266
 
        m7 = Mod('my_tests')
267
 
        m8 = Mod('__main__')
268
 
        
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)
278
 
        
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)
288
 
 
289
 
    def test_module_in_tests(self):
290
 
        s = Selector(Config())
291
 
        # s.tests = [ 'ever', 'what', 'what.ever' ]
292
 
 
293
 
        w = Mod('what')
294
 
        we = Mod('whatever')
295
 
        w_e = Mod('what.ever')
296
 
        w_n = Mod('what.not')
297
 
        f_e = Mod('for.ever')
298
 
 
299
 
        s.tests = [ 'what' ]
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)
310
 
 
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)
322
 
 
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)
 
174
 
 
175
        m6.__test__ = False
 
176
        assert not s.wantModule(m6), "Failed to respect __test__ = False"
 
177
 
334
178
        
335
179
if __name__ == '__main__':
336
 
    import logging
337
 
    logging.getLogger('nose.selector').setLevel(logging.DEBUG)
338
 
    logging.basicConfig()
339
180
    # log.setLevel(logging.DEBUG)
340
181
    unittest.main()