-
Notifications
You must be signed in to change notification settings - Fork 3
[ENG-177] Auto-detect test runs #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
safaricd
wants to merge
6
commits into
main
Choose a base branch
from
detect-test-runs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f75fa4a
Disable all telemetry from tests
safaricd 2a25d7d
Disable all telemetry from tests
safaricd ac38e9e
Fix failing unit tests
safaricd 37e06fd
Update src/tabpfn_common_utils/telemetry/core/service.py
safaricd 5b0ae4f
Fix unit tests issues
safaricd d5b31c7
Merge branch 'detect-test-runs' of github.com:PriorLabs/tabpfn_common…
safaricd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import sys | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from tabpfn_common_utils.telemetry.core.service import ProductTelemetry | ||
|
||
|
||
class TestRunsInTest: | ||
"""Test the _runs_in_test method for detecting test environments.""" | ||
|
||
@pytest.fixture(autouse=True) | ||
def setup(self) -> None: | ||
"""Set up test fixtures and clear cache before each test.""" | ||
self.original_class = ProductTelemetry() # type: ignore | ||
self.original_class._runs_in_test.cache_clear() | ||
|
||
def test_detects_pytest_current_test_env_var(self) -> None: | ||
"""Test detection via PYTEST_CURRENT_TEST environment variable.""" | ||
with patch.dict( | ||
os.environ, {"PYTEST_CURRENT_TEST": "tests/test_service.py::test_method"} | ||
): | ||
assert self.original_class._runs_in_test() is True | ||
|
||
def test_detects_pytest_xdist_worker_env_var(self) -> None: | ||
"""Test detection via PYTEST_XDIST_WORKER environment variable.""" | ||
with patch.dict(os.environ, {"PYTEST_XDIST_WORKER": "gw0"}): | ||
assert self.original_class._runs_in_test() is True | ||
|
||
def test_detects_pytest_in_sys_modules(self) -> None: | ||
"""Test detection via pytest in sys.modules.""" | ||
# pytest should already be in sys.modules when running tests | ||
assert "pytest" in sys.modules | ||
assert self.original_class._runs_in_test() is True | ||
|
||
def test_detects_unittest_in_sys_modules(self) -> None: | ||
"""Test detection via unittest in sys.modules.""" | ||
with patch.dict(sys.modules, {"unittest": object()}): | ||
assert self.original_class._runs_in_test() is True | ||
|
||
def test_detects_nose_in_sys_modules(self) -> None: | ||
"""Test detection via nose in sys.modules.""" | ||
with patch.dict(sys.modules, {"nose": object()}): | ||
assert self.original_class._runs_in_test() is True | ||
|
||
def test_detects_pytest_in_argv(self) -> None: | ||
"""Test detection via pytest in sys.argv[0].""" | ||
# Clear env vars and modules to ensure only sys.argv detection is tested | ||
with ( | ||
patch.dict(os.environ, {}, clear=True), | ||
patch("tabpfn_common_utils.telemetry.core.service.sys.modules", {}), | ||
patch.object(sys, "argv", ["/usr/bin/pytest", "tests/"]), | ||
): | ||
self.original_class._runs_in_test.cache_clear() | ||
assert self.original_class._runs_in_test() is True | ||
|
||
def test_detects_py_test_in_argv(self) -> None: | ||
"""Test detection via py.test in sys.argv[0].""" | ||
# Clear env vars and modules to ensure only sys.argv detection is tested | ||
with ( | ||
patch.dict(os.environ, {}, clear=True), | ||
patch("tabpfn_common_utils.telemetry.core.service.sys.modules", {}), | ||
patch.object(sys, "argv", ["/usr/local/bin/py.test", "tests/"]), | ||
): | ||
self.original_class._runs_in_test.cache_clear() | ||
assert self.original_class._runs_in_test() is True | ||
|
||
def test_detects_pytest_uppercase_in_argv(self) -> None: | ||
"""Test detection is case-insensitive for sys.argv[0].""" | ||
# Clear env vars and modules to ensure only sys.argv detection is tested | ||
with ( | ||
patch.dict(os.environ, {}, clear=True), | ||
patch("tabpfn_common_utils.telemetry.core.service.sys.modules", {}), | ||
patch.object(sys, "argv", ["/path/to/PYTEST", "tests/"]), | ||
): | ||
self.original_class._runs_in_test.cache_clear() | ||
assert self.original_class._runs_in_test() is True | ||
|
||
def test_caching_behavior(self) -> None: | ||
"""Test that the function caches its result.""" | ||
# First call should compute the result | ||
result1 = self.original_class._runs_in_test() | ||
|
||
# Check cache info | ||
cache_info = self.original_class._runs_in_test.cache_info() # type: ignore | ||
assert cache_info.hits == 0 | ||
assert cache_info.misses == 1 | ||
|
||
# Second call should use cache | ||
result2 = self.original_class._runs_in_test() | ||
assert result1 == result2 | ||
|
||
# Check cache was used | ||
cache_info = self.original_class._runs_in_test.cache_info() # type: ignore | ||
assert cache_info.hits == 1 | ||
assert cache_info.misses == 1 | ||
|
||
def test_returns_bool(self) -> None: | ||
"""Test that the function always returns a boolean.""" | ||
result = self.original_class._runs_in_test() | ||
assert isinstance(result, bool) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: typo, overwrite -> override