noseplugin.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. """
  8. Enhance nose with extra options and behaviors for running SQLAlchemy tests.
  9. NOTE: copied/adapted from SQLAlchemy master for backwards compatibility;
  10. this should be removable when Alembic targets SQLAlchemy 1.0.0.
  11. """
  12. try:
  13. # installed by bootstrap.py
  14. import alembic_plugin_base as plugin_base
  15. except ImportError:
  16. # assume we're a package, use traditional import
  17. from . import plugin_base
  18. import os
  19. import sys
  20. from nose.plugins import Plugin
  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. def begin(self):
  44. global fixtures
  45. from alembic.testing import fixtures # noqa
  46. plugin_base.post_begin()
  47. def describeTest(self, test):
  48. return ""
  49. def wantFunction(self, fn):
  50. return False
  51. def wantMethod(self, fn):
  52. if py3k:
  53. if not hasattr(fn.__self__, 'cls'):
  54. return False
  55. cls = fn.__self__.cls
  56. else:
  57. cls = fn.im_class
  58. return plugin_base.want_method(cls, fn)
  59. def wantClass(self, cls):
  60. return plugin_base.want_class(cls)
  61. def beforeTest(self, test):
  62. plugin_base.before_test(
  63. test,
  64. test.test.cls.__module__,
  65. test.test.cls, test.test.method.__name__)
  66. def afterTest(self, test):
  67. plugin_base.after_test(test)
  68. def startContext(self, ctx):
  69. if not isinstance(ctx, type) \
  70. or not issubclass(ctx, fixtures.TestBase):
  71. return
  72. plugin_base.start_test_class(ctx)
  73. def stopContext(self, ctx):
  74. if not isinstance(ctx, type) \
  75. or not issubclass(ctx, fixtures.TestBase):
  76. return
  77. plugin_base.stop_test_class(ctx)