Framework Mode - Command-line options
Folder Structure
project-root/ ├── tests/ │ ├── test_example.py │ └── test_login.py ├── conftest.py ├── pytest.ini ├── requirements.txt └── README.md
project-root/ ├── tests/ │ ├── test_example.py │ └── test_login.py ├── conftest.py ├── pytest.ini ├── requirements.txt └── README.md
tests/ folder, contains test files like test_example.py.conftest.py for setup and teardown, shared resources.pytest.ini file to set default command-line options and test discovery rules.Use pytest.ini to define default command-line options and customize test runs:
[pytest]
addopts = -v --maxfail=2 --tb=short
markers =
smoke: Quick smoke tests
regression: Full regression tests
Command-line options examples:
pytest -v: verbose outputpytest -k "login and not slow": run tests matching expressionpytest --maxfail=1: stop after first failurepytest --tb=short: shorter traceback on failureUse conftest.py to add custom command-line options with pytest_addoption hook.
# conftest.py
import pytest
def pytest_addoption(parser):
parser.addoption(
"--env",
action="store",
default="dev",
help="Environment to run tests against: dev, staging, prod"
)
@pytest.fixture
def env(request):
return request.config.getoption("--env")
--junitxml=report.xml to generate XML reports for CI tools.
- name: Run tests
run: |
pytest -v --junitxml=results.xml
This allows CI to parse test results and show pass/fail status.
pytest.ini to keep test runs consistent.conftest.py to add custom options for flexibility (e.g., environment selection).-k expressions to select specific tests easily.Where in this framework structure would you add a new custom command-line option to select a browser type for tests?