skip.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. This plugin installs a SKIP error class for the SkipTest exception.
  3. When SkipTest is raised, the exception will be logged in the skipped
  4. attribute of the result, 'S' or 'SKIP' (verbose) will be output, and
  5. the exception will not be counted as an error or failure. This plugin
  6. is enabled by default but may be disabled with the ``--no-skip`` option.
  7. """
  8. from nose.plugins.errorclass import ErrorClass, ErrorClassPlugin
  9. # on SkipTest:
  10. # - unittest SkipTest is first preference, but it's only available
  11. # for >= 2.7
  12. # - unittest2 SkipTest is second preference for older pythons. This
  13. # mirrors logic for choosing SkipTest exception in testtools
  14. # - if none of the above, provide custom class
  15. try:
  16. from unittest.case import SkipTest
  17. except ImportError:
  18. try:
  19. from unittest2.case import SkipTest
  20. except ImportError:
  21. class SkipTest(Exception):
  22. """Raise this exception to mark a test as skipped.
  23. """
  24. pass
  25. class Skip(ErrorClassPlugin):
  26. """
  27. Plugin that installs a SKIP error class for the SkipTest
  28. exception. When SkipTest is raised, the exception will be logged
  29. in the skipped attribute of the result, 'S' or 'SKIP' (verbose)
  30. will be output, and the exception will not be counted as an error
  31. or failure.
  32. """
  33. enabled = True
  34. skipped = ErrorClass(SkipTest,
  35. label='SKIP',
  36. isfailure=False)
  37. def options(self, parser, env):
  38. """
  39. Add my options to command line.
  40. """
  41. env_opt = 'NOSE_WITHOUT_SKIP'
  42. parser.add_option('--no-skip', action='store_true',
  43. dest='noSkip', default=env.get(env_opt, False),
  44. help="Disable special handling of SkipTest "
  45. "exceptions.")
  46. def configure(self, options, conf):
  47. """
  48. Configure plugin. Skip plugin is enabled by default.
  49. """
  50. if not self.can_configure:
  51. return
  52. self.conf = conf
  53. disable = getattr(options, 'noSkip', False)
  54. if disable:
  55. self.enabled = False