noseplugin.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # plugin/noseplugin.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """Enhance nose with extra options and behaviors for running SQLAlchemy tests.
  8. Must be run via ./sqla_nose.py so that it is imported in the expected
  9. way (e.g. as a package-less import).
  10. """
  11. try:
  12. # installed by bootstrap.py
  13. import sqla_plugin_base as plugin_base
  14. except ImportError:
  15. # assume we're a package, use traditional import
  16. from . import plugin_base
  17. import os
  18. import sys
  19. from nose.plugins import Plugin
  20. import nose
  21. fixtures = None
  22. py3k = sys.version_info >= (3, 0)
  23. class NoseSQLAlchemy(Plugin):
  24. enabled = True
  25. name = 'sqla_testing'
  26. score = 100
  27. def options(self, parser, env=os.environ):
  28. Plugin.options(self, parser, env)
  29. opt = parser.add_option
  30. def make_option(name, **kw):
  31. callback_ = kw.pop("callback", None)
  32. if callback_:
  33. def wrap_(option, opt_str, value, parser):
  34. callback_(opt_str, value, parser)
  35. kw["callback"] = wrap_
  36. opt(name, **kw)
  37. plugin_base.setup_options(make_option)
  38. plugin_base.read_config()
  39. def configure(self, options, conf):
  40. super(NoseSQLAlchemy, self).configure(options, conf)
  41. plugin_base.pre_begin(options)
  42. plugin_base.set_coverage_flag(options.enable_plugin_coverage)
  43. plugin_base.set_skip_test(nose.SkipTest)
  44. def begin(self):
  45. global fixtures
  46. from sqlalchemy.testing import fixtures # noqa
  47. plugin_base.post_begin()
  48. def describeTest(self, test):
  49. return ""
  50. def wantFunction(self, fn):
  51. return False
  52. def wantMethod(self, fn):
  53. if py3k:
  54. if not hasattr(fn.__self__, 'cls'):
  55. return False
  56. cls = fn.__self__.cls
  57. else:
  58. cls = fn.im_class
  59. return plugin_base.want_method(cls, fn)
  60. def wantClass(self, cls):
  61. return plugin_base.want_class(cls)
  62. def beforeTest(self, test):
  63. if not hasattr(test.test, 'cls'):
  64. return
  65. plugin_base.before_test(
  66. test,
  67. test.test.cls.__module__,
  68. test.test.cls, test.test.method.__name__)
  69. def afterTest(self, test):
  70. plugin_base.after_test(test)
  71. def startContext(self, ctx):
  72. if not isinstance(ctx, type) \
  73. or not issubclass(ctx, fixtures.TestBase):
  74. return
  75. plugin_base.start_test_class(ctx)
  76. def stopContext(self, ctx):
  77. if not isinstance(ctx, type) \
  78. or not issubclass(ctx, fixtures.TestBase):
  79. return
  80. plugin_base.stop_test_class(ctx)