conftest.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. from wagtail.tests.settings import MEDIA_ROOT, STATIC_ROOT
  33. shutil.rmtree(STATIC_ROOT, ignore_errors=True)
  34. shutil.rmtree(MEDIA_ROOT, ignore_errors=True)
  35. def pytest_unconfigure(config):
  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)