1
0

conftest.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Shared conftest for all integration tests."""
  2. import os
  3. import re
  4. from pathlib import Path
  5. import pytest
  6. from reflex.config import environment
  7. from reflex.testing import AppHarness, AppHarnessProd
  8. DISPLAY = None
  9. XVFB_DIMENSIONS = (800, 600)
  10. @pytest.fixture(scope="session", autouse=True)
  11. def xvfb():
  12. """Create virtual X display.
  13. This function is a no-op unless GITHUB_ACTIONS is set in the environment.
  14. Yields:
  15. the pyvirtualdisplay object that the browser will be open on
  16. """
  17. if os.environ.get("GITHUB_ACTIONS") and not environment.APP_HARNESS_HEADLESS.get():
  18. from pyvirtualdisplay.smartdisplay import ( # pyright: ignore [reportMissingImports]
  19. SmartDisplay,
  20. )
  21. global DISPLAY
  22. with SmartDisplay(visible=False, size=XVFB_DIMENSIONS) as DISPLAY:
  23. yield DISPLAY
  24. DISPLAY = None
  25. else:
  26. yield None
  27. def pytest_exception_interact(node, call, report):
  28. """Take and upload screenshot when tests fail.
  29. Args:
  30. node: The pytest item that failed.
  31. call: The pytest call describing when/where the test was invoked.
  32. report: The pytest log report object.
  33. """
  34. screenshot_dir = environment.SCREENSHOT_DIR.get()
  35. if DISPLAY is None or screenshot_dir is None:
  36. return
  37. screenshot_dir = Path(screenshot_dir)
  38. screenshot_dir.mkdir(parents=True, exist_ok=True)
  39. safe_filename = re.sub(
  40. r"(?u)[^-\w.]",
  41. "_",
  42. str(node.nodeid).strip().replace(" ", "_").replace(":", "_").replace(".py", ""),
  43. )
  44. try:
  45. DISPLAY.waitgrab().save(
  46. (Path(screenshot_dir) / safe_filename).with_suffix(".png"),
  47. )
  48. except Exception as e:
  49. print(f"Failed to take screenshot for {node}: {e}")
  50. @pytest.fixture(
  51. scope="session", params=[AppHarness, AppHarnessProd], ids=["dev", "prod"]
  52. )
  53. def app_harness_env(request):
  54. """Parametrize the AppHarness class to use for the test, either dev or prod.
  55. Args:
  56. request: The pytest fixture request object.
  57. Returns:
  58. The AppHarness class to use for the test.
  59. """
  60. return request.param