runtests.py 2.3 KB

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