Imported Upstream version 1.1.2
[platform/upstream/python-nose.git] / unit_tests / test_selector.py
1 import logging
2 import os
3 import re
4 import unittest
5 import nose.selector
6 from nose.config import Config
7 from nose.selector import log, Selector
8 from nose.util import absdir
9 from mock import mod
10
11 class TestSelector(unittest.TestCase):
12
13     def tearDown(self):
14         logging.getLogger('nose.selector').setLevel(logging.WARN)
15     
16     def test_ignore_files_default(self):
17         """A default configuration should always skip some 'hidden' files."""
18         s = Selector(Config())
19         
20         assert not s.wantFile('_test_underscore.py')
21         assert not s.wantFile('.test_hidden.py')
22         assert not s.wantFile('setup.py')
23         
24     def test_ignore_files_override(self):
25         """Override the configuration to skip only specified files."""
26         c = Config()
27         c.ignoreFiles = [re.compile(r'^test_favourite_colour\.py$')]
28         s = Selector(c)
29         
30         assert s.wantFile('_test_underscore.py')
31         assert s.wantFile('.test_hidden.py')
32         assert not s.wantFile('setup.py') # Actually excluded because of testMatch
33         assert not s.wantFile('test_favourite_colour.py')
34     
35     def test_exclude(self):
36         s = Selector(Config())
37         c = Config()
38         c.exclude = [re.compile(r'me')]
39         s2 = Selector(c)
40         
41         assert s.matches('test_foo')
42         assert s2.matches('test_foo')
43         assert s.matches('test_me')
44         assert not s2.matches('test_me')
45         
46     def test_include(self):
47         s = Selector(Config())
48         c = Config()
49         c.include = [re.compile(r'me')]
50         s2 = Selector(c)
51
52         assert s.matches('test')
53         assert s2.matches('test')
54         assert not s.matches('meatball')
55         assert s2.matches('meatball')
56         assert not s.matches('toyota')
57         assert not s2.matches('toyota')
58         
59         c.include.append(re.compile('toy'))
60         assert s.matches('test')
61         assert s2.matches('test')
62         assert not s.matches('meatball')
63         assert s2.matches('meatball')
64         assert not s.matches('toyota')
65         assert s2.matches('toyota')
66         
67     def test_want_class(self):
68         class Foo:
69             pass
70         class Bar(unittest.TestCase):
71             pass
72         class TestMe:
73             pass
74         class TestType(type):
75             def __new__(cls, name, bases, dct):
76                 return type.__new__(cls, name, bases, dct)
77         class TestClass(object):
78             __metaclass__ = TestType
79         
80         s = Selector(Config())
81         assert not s.wantClass(Foo)
82         assert s.wantClass(Bar)
83         assert s.wantClass(TestMe)
84         assert s.wantClass(TestClass)
85
86         TestMe.__test__ = False
87         assert not s.wantClass(TestMe), "Failed to respect __test__ = False"
88         Bar.__test__ = False
89         assert not s.wantClass(Bar), "Failed to respect __test__ = False"
90         
91     def test_want_directory(self):
92         s = Selector(Config())
93         assert s.wantDirectory('test')
94         assert not s.wantDirectory('test/whatever')
95         assert s.wantDirectory('whatever/test')
96         assert not s.wantDirectory('/some/path/to/unit_tests/support')
97
98         # default src directory
99         assert s.wantDirectory('lib')
100         assert s.wantDirectory('src')
101
102         # FIXME move to functional tests
103         
104         # this looks on disk for support/foo, which is a package
105         here = os.path.abspath(os.path.dirname(__file__))
106         support = os.path.join(here, 'support')
107         tp = os.path.normpath(os.path.join(support, 'foo'))
108         assert s.wantDirectory(tp)
109         # this looks for support, which is not a package
110         assert not s.wantDirectory(support)        
111         
112     def test_want_file(self):
113
114         #logging.getLogger('nose.selector').setLevel(logging.DEBUG)
115         #logging.basicConfig()
116         
117         c = Config()
118         c.where = [absdir(os.path.join(os.path.dirname(__file__), 'support'))]
119         base = c.where[0]
120         s = Selector(c)
121
122         assert not s.wantFile('setup.py')
123         assert not s.wantFile('/some/path/to/setup.py')
124         assert not s.wantFile('ez_setup.py')
125         assert not s.wantFile('.test.py')
126         assert not s.wantFile('_test.py')
127         assert not s.wantFile('setup_something.py')
128         
129         assert s.wantFile('test.py')
130         assert s.wantFile('foo/test_foo.py')
131         assert s.wantFile('bar/baz/test.py')
132         assert not s.wantFile('foo.py')
133         assert not s.wantFile('test_data.txt')
134         assert not s.wantFile('data.text')
135         assert not s.wantFile('bar/baz/__init__.py')
136         
137     def test_want_function(self):
138         def foo():
139             pass
140         def test_foo():
141             pass
142         def test_bar():
143             pass
144         
145         s = Selector(Config())
146         assert s.wantFunction(test_bar)
147         assert s.wantFunction(test_foo)
148         assert not s.wantFunction(foo)
149
150         test_foo.__test__ = False
151         assert not s.wantFunction(test_foo), \
152                "Failed to respect __test__ = False"
153
154     def test_want_method(self):
155         class Baz:
156             def test_me(self):
157                 pass
158             def test_too(self):
159                 pass
160             def other(self):
161                 pass
162             def test_not_test(self):
163                 pass
164             test_not_test.__test__ = False
165             
166         s = Selector(Config())
167         
168         assert s.wantMethod(Baz.test_me)
169         assert s.wantMethod(Baz.test_too)
170         assert not s.wantMethod(Baz.other)
171         assert not s.wantMethod(Baz.test_not_test), \
172                "Failed to respect __test__ = False"
173         
174     def test_want_module(self):
175         m = mod('whatever')
176         m2 = mod('this.that')
177         m3 = mod('this.that.another')
178         m4 = mod('this.that.another.one')
179         m5 = mod('test.something')
180         m6 = mod('a.test')
181         m7 = mod('my_tests')
182         m8 = mod('__main__')
183         
184         s = Selector(Config())
185         assert not s.wantModule(m)
186         assert not s.wantModule(m2)
187         assert not s.wantModule(m3)
188         assert not s.wantModule(m4)
189         assert not s.wantModule(m5)
190         assert s.wantModule(m6)
191         assert s.wantModule(m7)
192         assert s.wantModule(m8)
193
194         m6.__test__ = False
195         assert not s.wantModule(m6), "Failed to respect __test__ = False"
196
197         
198 if __name__ == '__main__':
199     # log.setLevel(logging.DEBUG)
200     unittest.main()