conftest.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. """
  2. Pytest configuration for LightRAG tests.
  3. This file provides command-line options and fixtures for test configuration.
  4. """
  5. import pytest
  6. @pytest.fixture(autouse=True)
  7. def _hermetic_mineru_env(monkeypatch):
  8. """Make every test start with parser-routing env vars in their unset state.
  9. ``lightrag/api/{auth,config}.py`` call ``load_dotenv(override=False)``
  10. at import time, leaking the developer's local ``.env`` into the test
  11. process. The MinerU test fixtures assume ``MINERU_API_MODE`` is unset
  12. (so it defaults to ``"local"`` per ``MinerURawClient.__init__`` /
  13. ``parser_engine_endpoint_requirement``):
  14. - A leaked ``MINERU_API_MODE=offical`` typo (or any invalid value)
  15. makes ``MinerURawClient()`` raise at construction.
  16. - A leaked ``MINERU_API_MODE=official`` flips
  17. ``parser_engine_endpoint_requirement`` to return
  18. ``"MINERU_API_TOKEN"`` instead of ``"MINERU_LOCAL_ENDPOINT"``,
  19. breaking the validation-error string match.
  20. ``LIGHTRAG_PARSER`` is cleared for the same reason: a routing rule
  21. like ``docx:mineru-iet`` in the developer's ``.env`` forces
  22. ``parser_routing.validate_parser_routing_config`` to require the
  23. corresponding endpoint (``MINERU_LOCAL_ENDPOINT`` /
  24. ``DOCLING_ENDPOINT``) at every ``create_app`` call, which then trips
  25. unrelated API/FastAPI tests (``test_bedrock_llm.py``,
  26. ``test_path_prefixes.py``).
  27. Strip these variables globally; tests that need a specific mode can
  28. still ``monkeypatch.setenv(...)`` themselves and monkeypatch will
  29. restore the inherited value at teardown.
  30. """
  31. monkeypatch.delenv("MINERU_API_MODE", raising=False)
  32. monkeypatch.delenv("MINERU_API_TOKEN", raising=False)
  33. monkeypatch.delenv("MINERU_LOCAL_ENDPOINT", raising=False)
  34. monkeypatch.delenv("MINERU_OFFICIAL_ENDPOINT", raising=False)
  35. monkeypatch.delenv("LIGHTRAG_PARSER", raising=False)
  36. monkeypatch.delenv("DOCLING_ENDPOINT", raising=False)
  37. def pytest_configure(config):
  38. """Register custom markers for LightRAG tests."""
  39. config.addinivalue_line(
  40. "markers", "offline: marks tests as offline (no external dependencies)"
  41. )
  42. config.addinivalue_line(
  43. "markers",
  44. "integration: marks tests requiring external services (skipped by default)",
  45. )
  46. config.addinivalue_line("markers", "requires_db: marks tests requiring database")
  47. config.addinivalue_line(
  48. "markers", "requires_api: marks tests requiring LightRAG API server"
  49. )
  50. def pytest_addoption(parser):
  51. """Add custom command-line options for LightRAG tests."""
  52. parser.addoption(
  53. "--keep-artifacts",
  54. action="store_true",
  55. default=False,
  56. help="Keep test artifacts (temporary directories and files) after test completion for inspection",
  57. )
  58. parser.addoption(
  59. "--stress-test",
  60. action="store_true",
  61. default=False,
  62. help="Enable stress test mode with more intensive workloads",
  63. )
  64. parser.addoption(
  65. "--test-workers",
  66. action="store",
  67. default=3,
  68. type=int,
  69. help="Number of parallel workers for stress tests (default: 3)",
  70. )
  71. parser.addoption(
  72. "--run-integration",
  73. action="store_true",
  74. default=False,
  75. help="Run integration tests that require external services (database, API server, etc.)",
  76. )
  77. def pytest_collection_modifyitems(config, items):
  78. """Modify test collection to skip integration tests by default.
  79. Integration tests are skipped unless --run-integration flag is provided.
  80. This allows running offline tests quickly without needing external services.
  81. """
  82. if config.getoption("--run-integration"):
  83. # If --run-integration is specified, run all tests
  84. return
  85. skip_integration = pytest.mark.skip(
  86. reason="Requires external services(DB/API), use --run-integration to run"
  87. )
  88. for item in items:
  89. if "integration" in item.keywords:
  90. item.add_marker(skip_integration)
  91. @pytest.fixture(scope="session")
  92. def keep_test_artifacts(request):
  93. """
  94. Fixture to determine whether to keep test artifacts.
  95. Priority: CLI option > Environment variable > Default (False)
  96. """
  97. import os
  98. # Check CLI option first
  99. if request.config.getoption("--keep-artifacts"):
  100. return True
  101. # Fall back to environment variable
  102. return os.getenv("LIGHTRAG_KEEP_ARTIFACTS", "false").lower() == "true"
  103. @pytest.fixture(scope="session")
  104. def stress_test_mode(request):
  105. """
  106. Fixture to determine whether stress test mode is enabled.
  107. Priority: CLI option > Environment variable > Default (False)
  108. """
  109. import os
  110. # Check CLI option first
  111. if request.config.getoption("--stress-test"):
  112. return True
  113. # Fall back to environment variable
  114. return os.getenv("LIGHTRAG_STRESS_TEST", "false").lower() == "true"
  115. @pytest.fixture(scope="session")
  116. def parallel_workers(request):
  117. """
  118. Fixture to determine the number of parallel workers for stress tests.
  119. Priority: CLI option > Environment variable > Default (3)
  120. """
  121. import os
  122. # Check CLI option first
  123. cli_workers = request.config.getoption("--test-workers")
  124. if cli_workers != 3: # Non-default value provided
  125. return cli_workers
  126. # Fall back to environment variable
  127. return int(os.getenv("LIGHTRAG_TEST_WORKERS", "3"))
  128. @pytest.fixture(scope="session")
  129. def run_integration_tests(request):
  130. """
  131. Fixture to determine whether to run integration tests.
  132. Priority: CLI option > Environment variable > Default (False)
  133. """
  134. import os
  135. # Check CLI option first
  136. if request.config.getoption("--run-integration"):
  137. return True
  138. # Fall back to environment variable
  139. return os.getenv("LIGHTRAG_RUN_INTEGRATION", "false").lower() == "true"