conftest.py 2.0 KB

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