6 from nose.config import Config
7 from nose.selector import log, Selector
8 from nose.util import absdir
11 class TestSelector(unittest.TestCase):
14 logging.getLogger('nose.selector').setLevel(logging.WARN)
16 def test_ignore_files_default(self):
17 """A default configuration should always skip some 'hidden' files."""
18 s = Selector(Config())
20 assert not s.wantFile('_test_underscore.py')
21 assert not s.wantFile('.test_hidden.py')
22 assert not s.wantFile('setup.py')
24 def test_ignore_files_override(self):
25 """Override the configuration to skip only specified files."""
27 c.ignoreFiles = [re.compile(r'^test_favourite_colour\.py$')]
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')
35 def test_exclude(self):
36 s = Selector(Config())
38 c.exclude = [re.compile(r'me')]
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')
46 def test_include(self):
47 s = Selector(Config())
49 c.include = [re.compile(r'me')]
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')
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')
67 def test_want_class(self):
70 class Bar(unittest.TestCase):
75 def __new__(cls, name, bases, dct):
76 return type.__new__(cls, name, bases, dct)
77 class TestClass(object):
78 __metaclass__ = TestType
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)
86 TestMe.__test__ = False
87 assert not s.wantClass(TestMe), "Failed to respect __test__ = False"
89 assert not s.wantClass(Bar), "Failed to respect __test__ = False"
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')
98 # default src directory
99 assert s.wantDirectory('lib')
100 assert s.wantDirectory('src')
102 # FIXME move to functional tests
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)
112 def test_want_file(self):
114 #logging.getLogger('nose.selector').setLevel(logging.DEBUG)
115 #logging.basicConfig()
118 c.where = [absdir(os.path.join(os.path.dirname(__file__), 'support'))]
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')
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')
137 def test_want_function(self):
145 s = Selector(Config())
146 assert s.wantFunction(test_bar)
147 assert s.wantFunction(test_foo)
148 assert not s.wantFunction(foo)
150 test_foo.__test__ = False
151 assert not s.wantFunction(test_foo), \
152 "Failed to respect __test__ = False"
154 def test_want_method(self):
162 def test_not_test(self):
164 test_not_test.__test__ = False
166 s = Selector(Config())
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"
174 def test_want_module(self):
176 m2 = mod('this.that')
177 m3 = mod('this.that.another')
178 m4 = mod('this.that.another.one')
179 m5 = mod('test.something')
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)
195 assert not s.wantModule(m6), "Failed to respect __test__ = False"
198 if __name__ == '__main__':
199 # log.setLevel(logging.DEBUG)