/packages/nose/unit_tests/test_selector.py

https://github.com/mozilla/kuma-lib · Python · 200 lines · 197 code · 3 blank · 0 comment · 0 complexity · de511a01f344b8b3fd77edb6022fe7fd MD5 · raw file

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