diff --git a/README.md b/README.md index b0e8a90..2bdffc7 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,7 @@ You can use environment variable to control certain features of testomat.io | TESTOMATIO_STACK_PASSED | Enables logs for passed tests. Disabled by default. | TESTOMATIO_STACK_PASSED=true pytest --testomatio report | | TESTOMATIO_SHARED_RUN | Report parallel execution to the same run matching it by title. If the run was created more than 20 minutes ago, a new run will be created instead. | TESTOMATIO_TITLE="Run1" TESTOMATIO_SHARED_RUN=1 pytest --testomatio report | | TESTOMATIO_SHARED_RUN_TIMEOUT | Changes timeout of shared run. After timeout, shared run won`t accept other runs with same name, and new runs will be created. Timeout is set in minutes, default is 20 minutes. | TESTOMATIO_TITLE="Run1" TESTOMATIO_SHARED_RUN=1 TESTOMATIO_SHARED_RUN_TIMEOUT=10 pytest --testomatio report | +| TESTOMATIO_EXCLUDE_FILES_FROM_REPORT_GLOB_PATTERN | Excludes tests from report using glob patterns. You can specify multiple patterns using **;** as separator | TESTOMATIO_EXCLUDE_FILES_FROM_REPORT_GLOB_PATTERN="**/*_auth.py;directory" pytest --testomatio report | | TESTOMATIO_CREATE | Create test which are not yet exist in a project | TESTOMATIO_CREATE=1 pytest --testomatio report | | TESTOMATIO_WORKDIR | Specify a custom working directory for relative file paths in test reports. When tests are created with **TESTOMATIO_CREATE=1**, file paths will be relative to this directory. | TESTOMATIO_WORKDIR=new_dir pytest --testomatio report | | TESTOMATIO_DISABLE_BATCH_UPLOAD | Disables batch uploading and uploads each test result one by one. | TESTOMATIO_DISABLE_BATCH_UPLOAD=True pytest --testomatio report | diff --git a/pytestomatio/main.py b/pytestomatio/main.py index 06173e3..da1960c 100644 --- a/pytestomatio/main.py +++ b/pytestomatio/main.py @@ -109,6 +109,22 @@ def pytest_configure(config: Config): # This ensures we only apply our OR logic after other filters have done their job. config.pluginmanager.register(TestomatioFilterPlugin(), "testomatio_filter_plugin") + +def pytest_ignore_collect(collection_path, config): + if config.getoption(testomatio) is None or config.getoption(testomatio) != 'report': + return + + exclude_patterns = os.environ.get('TESTOMATIO_EXCLUDE_FILES_FROM_REPORT_GLOB_PATTERN', None) + if not exclude_patterns: + return + + exclude_patterns = exclude_patterns.split(';') + relative_path = collection_path.relative_to(config.rootpath) + for pattern in exclude_patterns: + if pattern and relative_path.match(pattern): + return True + + @pytest.hookimpl(tryfirst=True) def pytest_collection_modifyitems(session: Session, config: Config, items: list[Item]) -> None: if config.getoption(testomatio) is None: diff --git a/tests/test_main.py b/tests/test_main.py index d1cf6d6..6af9c43 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,3 +1,5 @@ +from pathlib import Path + import pytest import os @@ -40,6 +42,35 @@ def test_pytest_collection_stores_original_items(self): assert mock_session._pytestomatio_original_collected_items == [] +@pytest.mark.smoke +class TestPytestIgnoreCollect: + """Tests for pytest_ignore_collect hook""" + + @pytest.fixture + def mock_config(self): + config = Mock() + config.getoption.return_value = 'report' + config.rootpath = 'temp/' + return config + + def test_ignores_by_extension(self, mock_config): + pattern = '**/*.py' + paths = [('directory/test_file.py', True), ('directory/file.py', True), ('new_dir/file.js', None)] + with patch.dict(os.environ, {'TESTOMATIO_EXCLUDE_FILES_FROM_REPORT_GLOB_PATTERN': pattern}, clear=True): + for path, expected_result in paths: + collect_path = Path(mock_config.rootpath + path) + result = main.pytest_ignore_collect(collect_path, mock_config) + assert result is expected_result + + def test_ignores_by_name(self, mock_config): + pattern = '**/test_*.py' + paths = [('directory/test_file.py', True), ('directory/file.py', None), ('directory/file.js', None)] + with patch.dict(os.environ, {'TESTOMATIO_EXCLUDE_FILES_FROM_REPORT_GLOB_PATTERN': pattern}, clear=True): + for path, expected_result in paths: + collect_path = Path(mock_config.rootpath + path) + result = main.pytest_ignore_collect(collect_path, mock_config) + assert result is expected_result + @pytest.mark.smoke class TestPytestConfigure: """Tests for pytest_configure hook"""