Testing the URL subset of the sitemap tests
Introduction
The sitemap tests request every URL of the site: the link checker, the
content checker and the engine marker test all walk
src/sitemap.xml. While one folder is being rewritten
that is too much, because a run takes minutes and every unrelated page that
is temporarily broken fails it.
The suite therefore accepts a page subset. The environment variable
PATHS_FILE names a file with one URL path per line,
and only those paths are checked. The runner
container/tests/run_sitemap_tests.sh passes the file
with --paths-file, and its
docker
backend mounts it into the test container as
/selection/paths.txt.
The Tests tab of the backoffice writes the same file for its Directories
and Latest commits page scopes.
The module container/tests/url_selection.py reads the
file once, at import, and answers whether a URL belongs to the subset. Its
test, container/tests/test_url_selection.py, is the
shortest file of the suite: two tests, three imports, no web server, no
browser, no PHP.
Tools used here: Python , pytest .
What the module does
The module has five functions, two module-level assignments, and one property that decides how the test must be written: SELECTED is computed while the module is imported, so the file is read exactly once per process.
The module container/tests/url_selection.py:
import os from urllib.parse import unquote, urlparse import pytest PATHS_FILE = os.environ.get("PATHS_FILE", "").strip() def _key(url_or_path): """Compare decoded paths; /dir/index.php and /dir/ are the same page.""" path = unquote(urlparse(url_or_path).path or "/") return path.removesuffix("index.php") if path.endswith("/index.php") else path def _load(path): if not path: return None with open(path, encoding="utf-8") as handle: return frozenset(_key(line.strip()) for line in handle if line.startswith("/")) SELECTED = _load(PATHS_FILE) def in_selection(url_or_path): return SELECTED is None or _key(url_or_path) in SELECTED def skip_if_nothing_selected(locs): """A page subset that leaves this test nothing to check skips it, not fails.""" if SELECTED is not None and not locs: pytest.skip(f"none of the {len(SELECTED)} paths in PATHS_FILE apply to this test", allow_module_level=True) def selection_label(): return "(all)" if SELECTED is None else f"{len(SELECTED)} paths from {PATHS_FILE}"
_key() reduces a URL or a path to the string that is
actually compared: the decoded path, without scheme, host and query string.
It also drops a trailing index.php, because
/about/ and /about/index.php
are one page. A URL with no path at all becomes
/.
_load() answers None when
the variable is empty, and that None is the
"no subset" state: every URL passes. When a file is named, it
keeps the lines that start with a slash and drops every other line, so a
comment or a stray word in the same file cannot become a path. The result
is a frozenset, since membership is the only
operation the module ever needs.
skip_if_nothing_selected(locs) is called by the
sitemap tests with their own list of locations. When a subset exists and
that list is empty, the test is skipped instead of failed: a page outside
the subset is not a broken page.
selection_label() is the string the same tests print
in their report, (all) or
2 paths from /path/to/paths.txt.
The fixture that puts the module back
The test file starts with the import and one autouse fixture. A reload changes the module object for the whole pytest session, so a test that leaves it reloaded would hand its subset to every later test. The fixture reloads the module once more after the test, when monkeypatch has already restored the original environment.
The test file container/tests/test_url_selection.py, first half:
import importlib import pytest import url_selection @pytest.fixture(autouse=True) def _restore_module(): yield importlib.reload(url_selection) # runs after monkeypatch restored PATHS_FILE def _reload(monkeypatch, value): monkeypatch.setenv("PATHS_FILE", value) return importlib.reload(url_selection)
import url_selection works without a path because
the module sits next to the test file and pytest puts the folder of the
test module on sys.path when it imports it.
_reload(monkeypatch, value) is three lines used by
both tests: set the variable, reload the module, return the reloaded one.
The reload is the point. The module read
PATHS_FILE at import, before the test existed, so
changing os.environ alone changes nothing; only a
second run of the module body recomputes
SELECTED. That behaviour is the subject of the
import-time settings
page.
The fixture is autouse, so it runs for both tests without being named, and
its code after yield is the teardown. An autouse
fixture is set up before the monkeypatch a test
asks for, and pytest tears fixtures down in the reverse order, so the final
reload sees the environment as it was before the test.
Test 1: no file, every path passes
def test_unset_paths_file_selects_everything(monkeypatch): selection = _reload(monkeypatch, "") assert selection.in_selection("https://www.aredel.com/any/page.php") assert selection.selection_label() == "(all)" selection.skip_if_nothing_selected([]) # no subset -> no skip
The test sets the variable to an empty string, which is the same state as an unset variable after .strip(). Three consequences are checked: any URL is selected, the report label says (all), and a test with an empty location list is not skipped. The last line has no assertion and does not need one: skip_if_nothing_selected([]) raises when it skips, so reaching the next line is the result.
Test 2: a file with two paths
The second test writes a real file with tmp_path and puts four lines in it: two paths, one comment, one word that is not a path.
def test_listed_paths_match_exactly_and_index_php_equals_directory(monkeypatch, tmp_path): listed = tmp_path / "paths.txt" listed.write_text("/about/\n/ru/qa/page.php\n# comment\nnot-a-path\n", encoding="utf-8") selection = _reload(monkeypatch, str(listed)) assert selection.in_selection("https://www.aredel.com/about/") assert selection.in_selection("/about/index.php") assert selection.in_selection("http://php-apache/ru/qa/page.php") assert not selection.in_selection("/ru/qa/") assert not selection.in_selection("/about/team/") assert selection.selection_label() == f"2 paths from {listed}" with pytest.raises(pytest.skip.Exception): selection.skip_if_nothing_selected([])
Each assertion has its own reason:
- https://www.aredel.com/about/ passes although the file holds a path and not a URL: the host is thrown away before the comparison;
- /about/index.php passes for the same entry, /about/: one page, one key;
- http://php-apache/ru/qa/page.php passes, and this host name is what the docker backend of the runner uses, so the file works unchanged inside the container;
- /ru/qa/ does not pass: the match is exact, a directory is not the page that was listed;
- /about/team/ does not pass either: a subset of the site is not a prefix filter;
- the label is 2 paths from ... although the file has four lines, which proves that the comment and not-a-path were dropped;
- the last statement expects pytest.skip.Exception: with a subset and nothing to check, the sitemap test skips. The with form is the same pytest.raises tool that checks exceptions anywhere else.
Run the file
Run the file from the container directory:
./tools/run-tests-venv.sh -- tests/test_url_selection.py -q
Checked 5 packages in 234ms BASE_URL=http://127.0.0.1:8080 SITEMAP_FILE=src/sitemap.xml .. [100%] 2 passed in 1.19s
The first line is uv checking the packages of the venv, the second the two variables the script exports for the tests. Then one dot per test, and the summary line. To run a single test, add -k with a part of its name, for example -k listed for the second one; pytest then collects one item and prints 1 passed.
Article author: Arthur Isaev