collect.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. This plugin bypasses the actual execution of tests, and instead just collects
  3. test names. Fixtures are also bypassed, so running nosetests with the
  4. collection plugin enabled should be very quick.
  5. This plugin is useful in combination with the testid plugin (``--with-id``).
  6. Run both together to get an indexed list of all tests, which will enable you to
  7. run individual tests by index number.
  8. This plugin is also useful for counting tests in a test suite, and making
  9. people watching your demo think all of your tests pass.
  10. """
  11. from nose.plugins.base import Plugin
  12. from nose.case import Test
  13. import logging
  14. import unittest
  15. log = logging.getLogger(__name__)
  16. class CollectOnly(Plugin):
  17. """
  18. Collect and output test names only, don't run any tests.
  19. """
  20. name = "collect-only"
  21. enableOpt = 'collect_only'
  22. def options(self, parser, env):
  23. """Register commandline options.
  24. """
  25. parser.add_option('--collect-only',
  26. action='store_true',
  27. dest=self.enableOpt,
  28. default=env.get('NOSE_COLLECT_ONLY'),
  29. help="Enable collect-only: %s [COLLECT_ONLY]" %
  30. (self.help()))
  31. def prepareTestLoader(self, loader):
  32. """Install collect-only suite class in TestLoader.
  33. """
  34. # Disable context awareness
  35. log.debug("Preparing test loader")
  36. loader.suiteClass = TestSuiteFactory(self.conf)
  37. def prepareTestCase(self, test):
  38. """Replace actual test with dummy that always passes.
  39. """
  40. # Return something that always passes
  41. log.debug("Preparing test case %s", test)
  42. if not isinstance(test, Test):
  43. return
  44. def run(result):
  45. # We need to make these plugin calls because there won't be
  46. # a result proxy, due to using a stripped-down test suite
  47. self.conf.plugins.startTest(test)
  48. result.startTest(test)
  49. self.conf.plugins.addSuccess(test)
  50. result.addSuccess(test)
  51. self.conf.plugins.stopTest(test)
  52. result.stopTest(test)
  53. return run
  54. class TestSuiteFactory:
  55. """
  56. Factory for producing configured test suites.
  57. """
  58. def __init__(self, conf):
  59. self.conf = conf
  60. def __call__(self, tests=(), **kw):
  61. return TestSuite(tests, conf=self.conf)
  62. class TestSuite(unittest.TestSuite):
  63. """
  64. Basic test suite that bypasses most proxy and plugin calls, but does
  65. wrap tests in a nose.case.Test so prepareTestCase will be called.
  66. """
  67. def __init__(self, tests=(), conf=None):
  68. self.conf = conf
  69. # Exec lazy suites: makes discovery depth-first
  70. if callable(tests):
  71. tests = tests()
  72. log.debug("TestSuite(%r)", tests)
  73. unittest.TestSuite.__init__(self, tests)
  74. def addTest(self, test):
  75. log.debug("Add test %s", test)
  76. if isinstance(test, unittest.TestSuite):
  77. self._tests.append(test)
  78. else:
  79. self._tests.append(Test(test, config=self.conf))