Pytest Configuration
Relevant source files
The following files were used as context for generating this wiki page:
The testing suite for cron-ext is designed to validate the integration between Meltano schedules and the system crontab. Given that the extension manipulates system-level files (crontab), the configuration emphasizes isolation through containerization and temporary project environments.
Tests are primarily managed through pytest and orchestrated via Docker Compose to ensure that crontab modifications do not affect the host developer environment.
Test Environment Isolation
To prevent accidental modification of the host system's crontab, the project enforces a containerized testing environment. The pytest_sessionstart hook checks for the CONTAINERIZED environment variable and raises an exception if the tests are executed directly on a host machine.
Sources: tests/conftest.py:12-17, tests/compose.yml:9
Container Orchestration
The environment is defined in tests/compose.yml, which mounts the project root and sets necessary environment variables for Python.
Pytest Initialization and Fixtures
The project utilizes pytest fixtures to manage the lifecycle of a Meltano project during testing. This includes initializing a temporary Meltano project, installing the cron utility, and configuring environment variables like MELTANO_PROJECT_ROOT.
Key Fixtures
| Fixture | Scope | Description |
|---|---|---|
meltano_project | Session | Creates a temporary directory, initializes a Meltano project, installs the cron extension, and sets project environment variables. |
invoke | Session | Provides a CliRunner wrapper to invoke the cron-ext CLI and assert expected exit codes. |
cron_entries | Function (Context) | Manages system crontab entries, allowing tests to inject specific lines and ensuring the crontab is restored after the test. |
Sources: tests/test_cron_ext.py:34-75, tests/test_cron_ext.py:82-88
Meltano Project Setup Flow
The meltano_project fixture automates the setup required for every test case.
Sources: tests/test_cron_ext.py:36-67
Global CLI and INI Configuration
The behavior of pytest is configured globally in pyproject.toml. The addopts setting ensures that tests are run with extra verbosity and that summary reports for all except passed tests are displayed.
| Configuration Item | Value | Description |
|---|---|---|
addopts | -ra -vvv | Show extra test summary info; set verbosity to level 3. |
testpaths (implied) | tests | Directory containing test files. |
python_files | test_*.py | Pattern for identifying test modules. |
Sources: pyproject.toml:50-51
Mocking and Context Management
For tests involving crontab manipulation, the suite uses context managers to simulate crontab state. The meltano_cron_entries context manager specifically wraps entries in the "MELTANO CRONTAB SECTION" headers, simulating how the extension marks its managed blocks.
@contextmanager
def meltano_cron_entries(entries: Sequence[str], append: bool = False) -> None:
section_template = "# --- {} MELTANO CRONTAB SECTION ({}) ---"
with cron_entries(
(
section_template.format("BEGIN", os.environ["MELTANO_PROJECT_ID"]),
*entries,
section_template.format("END", os.environ["MELTANO_PROJECT_ID"]),
),
append=append,
):
yield
Sources: tests/test_cron_ext.py:112-123
Summary of Test Dependencies
The test suite requires several development dependencies to function correctly, including specific versions of Meltano and testing utilities.
- pytest: The core testing framework (version ^8.3).
- coverage: Used for measuring code coverage (version ^7.10).
- meltano: Required to run the
meltanocommands during fixture setup (version >=3.5). - typer: Used by
CliRunnerfor testing the command-line interface.
Sources: pyproject.toml:34-40