diff --git a/libs/standard-tests/langchain_tests/_langsmith_plugin.py b/libs/standard-tests/langchain_tests/_langsmith_plugin.py index f1c4147723f..0f6cdebfeef 100644 --- a/libs/standard-tests/langchain_tests/_langsmith_plugin.py +++ b/libs/standard-tests/langchain_tests/_langsmith_plugin.py @@ -23,7 +23,6 @@ import sys import warnings from typing import TYPE_CHECKING, Any -import pytest from langsmith.run_helpers import tracing_context if TYPE_CHECKING: @@ -84,8 +83,22 @@ def _langsmith_ci_cm() -> Iterator[None]: yield -@pytest.fixture(scope="session", autouse=True) -def _langsmith_ci_context() -> Iterator[None]: - """Apply `LANGSMITH_TAGS`/`LANGSMITH_METADATA` to traces in this session.""" - with _langsmith_ci_cm(): - yield +_langsmith_ci_context: contextlib.ExitStack | None = None + + +def pytest_configure() -> None: + """Apply `LANGSMITH_TAGS`/`LANGSMITH_METADATA` to traces.""" + global _langsmith_ci_context # noqa: PLW0603 + + if _langsmith_ci_context is None: + _langsmith_ci_context = contextlib.ExitStack() + _langsmith_ci_context.enter_context(_langsmith_ci_cm()) + + +def pytest_unconfigure() -> None: + """Restore the previous LangSmith tracing context.""" + global _langsmith_ci_context # noqa: PLW0603 + + if _langsmith_ci_context is not None: + _langsmith_ci_context.close() + _langsmith_ci_context = None diff --git a/libs/standard-tests/tests/unit_tests/test_langsmith_plugin.py b/libs/standard-tests/tests/unit_tests/test_langsmith_plugin.py index b17c508a1fc..4c7e2639399 100644 --- a/libs/standard-tests/tests/unit_tests/test_langsmith_plugin.py +++ b/libs/standard-tests/tests/unit_tests/test_langsmith_plugin.py @@ -129,22 +129,23 @@ class TestContextManager: class TestPluginDiscovery: - """End-to-end: the `pytest11` entry point wires up the autouse fixture.""" + """End-to-end: the `pytest11` entry point wires up the plugin hooks.""" - def test_autouse_fixture_applies_env_in_subprocess( + def test_plugin_hooks_apply_env_in_subprocess( self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.setenv("GITHUB_ACTIONS", "true") monkeypatch.setenv("LANGSMITH_TAGS", "discovered,from-entrypoint") monkeypatch.delenv("LANGSMITH_METADATA", raising=False) - pytester.makepyfile( + pytester.makeconftest( dedent(""" from langsmith.run_helpers import get_tracing_context - def test_tags_visible_via_autouse_fixture(): + def pytest_runtest_setup(item): ctx = get_tracing_context() assert ctx["tags"] == ["discovered", "from-entrypoint"] """), ) + pytester.makepyfile("def test_tags_visible_before_test_setup(): pass") result = pytester.runpytest_subprocess("-q") result.assert_outcomes(passed=1)