conftest.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from __future__ import absolute_import, unicode_literals
  2. import os
  3. import shutil
  4. import warnings
  5. import django
  6. def pytest_addoption(parser):
  7. parser.addoption('--deprecation', choices=['all', 'pending', 'imminent', 'none'], default='pending')
  8. parser.addoption('--postgres', action='store_true')
  9. parser.addoption('--elasticsearch', action='store_true')
  10. def pytest_configure(config):
  11. deprecation = config.getoption('deprecation')
  12. only_wagtail = r'^wagtail(\.|$)'
  13. if deprecation == 'all':
  14. # Show all deprecation warnings from all packages
  15. warnings.simplefilter('default', DeprecationWarning)
  16. warnings.simplefilter('default', PendingDeprecationWarning)
  17. elif deprecation == 'pending':
  18. # Show all deprecation warnings from wagtail
  19. warnings.filterwarnings('default', category=DeprecationWarning, module=only_wagtail)
  20. warnings.filterwarnings('default', category=PendingDeprecationWarning, module=only_wagtail)
  21. elif deprecation == 'imminent':
  22. # Show only imminent deprecation warnings from wagtail
  23. warnings.filterwarnings('default', category=DeprecationWarning, module=only_wagtail)
  24. elif deprecation == 'none':
  25. # Deprecation warnings are ignored by default
  26. pass
  27. if config.getoption('postgres'):
  28. os.environ['DATABASE_ENGINE'] = 'django.db.backends.postgresql_psycopg2'
  29. # Setup django after processing the pytest arguments so that the env
  30. # variables are available in the settings
  31. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wagtail.tests.settings')
  32. django.setup()
  33. from wagtail.tests.settings import MEDIA_ROOT, STATIC_ROOT
  34. shutil.rmtree(STATIC_ROOT, ignore_errors=True)
  35. shutil.rmtree(MEDIA_ROOT, ignore_errors=True)
  36. def pytest_unconfigure(config):
  37. from wagtail.tests.settings import MEDIA_ROOT, STATIC_ROOT
  38. shutil.rmtree(STATIC_ROOT, ignore_errors=True)
  39. shutil.rmtree(MEDIA_ROOT, ignore_errors=True)