runtests.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import absolute_import, print_function
  4. import sys
  5. import os
  6. import nose
  7. from nose.plugins.manager import PluginManager
  8. from nose.plugins.doctests import Doctest
  9. from nose.plugins import builtin
  10. NLTK_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
  11. sys.path.insert(0, NLTK_ROOT)
  12. NLTK_TEST_DIR = os.path.join(NLTK_ROOT, 'nltk')
  13. if __name__ == '__main__':
  14. # there shouldn't be import from NLTK for coverage to work properly
  15. from doctest_nose_plugin import DoctestFix
  16. try:
  17. # Import RedNose plugin for colored test output
  18. from rednose import RedNose
  19. rednose_available = True
  20. except ImportError:
  21. rednose_available = False
  22. class NltkPluginManager(PluginManager):
  23. """
  24. Nose plugin manager that replaces standard doctest plugin
  25. with a patched version and adds RedNose plugin for colored test output.
  26. """
  27. def loadPlugins(self):
  28. for plug in builtin.plugins:
  29. if plug != Doctest:
  30. self.addPlugin(plug())
  31. self.addPlugin(DoctestFix())
  32. if rednose_available:
  33. self.addPlugin(RedNose())
  34. super(NltkPluginManager, self).loadPlugins()
  35. manager = NltkPluginManager()
  36. manager.loadPlugins()
  37. # allow passing extra options and running individual tests
  38. # Examples:
  39. #
  40. # python runtests.py semantics.doctest
  41. # python runtests.py --with-id -v
  42. # python runtests.py --with-id -v nltk.featstruct
  43. args = sys.argv[1:]
  44. if not args:
  45. args = [NLTK_TEST_DIR]
  46. if all(arg.startswith('-') for arg in args):
  47. # only extra options were passed
  48. args += [NLTK_TEST_DIR]
  49. # Activate RedNose and hide skipped test messages from output
  50. if rednose_available:
  51. args += ['--rednose', '--hide-skips']
  52. arguments = [
  53. '--exclude=', # why is this needed?
  54. # '--with-xunit',
  55. # '--xunit-file=$WORKSPACE/nosetests.xml',
  56. # '--nocapture',
  57. '--with-doctest',
  58. # '--doctest-tests',
  59. # '--debug=nose,nose.importer,nose.inspector,nose.plugins,nose.result,nose.selector',
  60. '--doctest-extension=.doctest',
  61. '--doctest-fixtures=_fixt',
  62. '--doctest-options=+ELLIPSIS,+NORMALIZE_WHITESPACE,+IGNORE_EXCEPTION_DETAIL,+ALLOW_UNICODE,'
  63. 'doctestencoding=utf-8',
  64. # '--verbosity=3',
  65. ] + args
  66. nose.main(argv=arguments, plugins=manager.plugins)