pytest_project/ ├── tests/ │ ├── test_login.py │ ├── test_checkout.py │ └── test_profile.py ├── config/ │ ├── dev.yaml │ ├── staging.yaml │ └── prod.yaml ├── conftest.py ├── utils/ │ ├── helpers.py │ └── fixtures.py └── pytest.ini
Why configuration standardizes test behavior in PyTest - Framework Benefits
- Config Layer: Holds environment-specific settings in YAML files and pytest.ini for global options.
- Fixtures Layer: Defined in conftest.py and utils/fixtures.py to provide reusable setup and teardown.
- Test Layer: Actual test scripts inside tests/ folder using pytest functions and fixtures.
- Utilities Layer: Helper functions and common utilities in utils/helpers.py to support tests.
Configuration files (YAML) store environment details like URLs, credentials, and timeouts. pytest.ini sets global pytest options like markers and logging. conftest.py reads config files based on environment variables (e.g., ENV=dev) to load correct settings. This standardizes test behavior by ensuring tests run with consistent data and environment settings without code changes.
# conftest.py example snippet
import os
import yaml
import pytest
@pytest.fixture(scope="session")
def config():
env = os.getenv("ENV", "dev")
with open(f"config/{env}.yaml") as f:
data = yaml.safe_load(f)
return data
pytest supports plugins like pytest-html and pytest-junitxml to generate readable reports. Reports show which tests passed or failed with details. CI/CD pipelines (GitHub Actions, Jenkins) run tests automatically on code changes using the same config environment variables to keep behavior consistent. Failures trigger alerts for quick fixes.
- Use environment-specific config files: Separate dev, staging, prod settings to avoid mixing data.
- Load config dynamically: Use environment variables to select config at runtime for flexibility.
- Keep secrets secure: Do not hardcode passwords; use secure vaults or environment variables.
- Centralize config access: Use fixtures or helper functions to read config once and share across tests.
- Document config usage: Make it clear how to add or change config for new environments or tests.
Where in this folder structure would you add a new configuration file for a "testing" environment?
Answer: Add a new YAML file named testing.yaml inside the config/ folder.
Practice
pytest.ini configuration file important for test behavior?Solution
Step 1: Understand the role of configuration files
Configuration files likepytest.inidefine settings that control how tests run, such as markers, test paths, or warnings.Step 2: Recognize the benefit of standardization
By setting options in one place, all tests behave consistently across different runs and environments.Final Answer:
It sets consistent options so tests run the same way every time. -> Option AQuick Check:
Configuration standardizes test behavior = A [OK]
- Thinking config files fix test code errors automatically
- Believing config files replace writing tests
- Assuming config files skip tests to speed up runs
pytest.ini to standardize test selection?Solution
Step 1: Recall the correct syntax for markers in pytest.ini
The correct section header is[pytest]and the key ismarkerswith an equals sign and description.Step 2: Identify the properly formatted option
[pytest] markers = slow: marks tests as slow usesmarkers = slow: marks tests as slow, which matches pytest documentation syntax.Final Answer:
[pytest] markers = slow: marks tests as slow -> Option CQuick Check:
Correct marker syntax = B [OK]
- Using 'mark' instead of 'markers'
- Missing equals sign after 'markers'
- Not enclosing section header in brackets
pytest.ini content:[pytest] addopts = -v --maxfail=2
What will happen when you run
pytest on a test suite with 5 failing tests?Solution
Step 1: Interpret the
The optionaddoptsoptions-vmeans verbose output. The option--maxfail=2means pytest stops after 2 failures.Step 2: Predict test run behavior
When running tests with 5 failures, pytest will stop after the second failure and show detailed output because of-v.Final Answer:
Pytest will stop after 2 failures and show verbose output. -> Option DQuick Check:
addopts '-v --maxfail=2' = stop at 2 fails + verbose [OK]
- Thinking pytest runs all tests despite maxfail
- Confusing verbose with minimal output
- Assuming maxfail=2 means stop after 5 failures
filterwarnings = ignore::DeprecationWarning in pytest.ini but still see deprecation warnings during test runs. What is the likely cause?Solution
Step 1: Verify correct spelling and placement of
The option must be spelled exactly asfilterwarningsfilterwarningsunder the[pytest]section inpytest.ini.Step 2: Understand pytest's support for warning filters
Pytest supports filtering warnings via config. If warnings still appear, likely the option is misspelled or in wrong section.Final Answer:
Thefilterwarningsoption is misspelled or misplaced in the config file. -> Option BQuick Check:
Correct spelling and placement = D [OK]
- Assuming pytest ignores config filters
- Using wrong option names
- Not placing options under [pytest] section
@pytest.mark.slow are skipped by default unless explicitly included. Which pytest.ini configuration achieves this standardized behavior?Solution
Step 1: Understand marker usage and addopts filtering
To skip tests marked slow by default, use-m 'not slow'to run tests not marked slow.Step 2: Identify correct addopts syntax
[pytest] markers = slow: marks slow tests addopts = -m 'not slow' usesaddopts = -m 'not slow', which tells pytest to exclude slow tests unless overridden.Final Answer:
[pytest] markers = slow: marks slow tests addopts = -m 'not slow' -> Option AQuick Check:
Exclude slow tests by default = C [OK]
- Using '-m slow' runs only slow tests, not skips them
- Trying unsupported options like --skip or --disable
- Not defining the marker in pytest.ini
Ready to prove it?
Pass the test and this pattern moves unknown
