conftest.py 2.1 KB

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