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?
-v do when running tests?-v option in pytest-v option stands for verbose mode, which means pytest will show detailed information about each test it runs.-v specifically increases output detail.-x, which tells pytest to stop after the first test failure.--stop-on-failure and --failfast=false are invalid, and -f is not a recognized pytest option.pytest -k 'test_add or test_subtract' -v, what will pytest do?-k option-k option filters tests by substring expressions. Here, it selects tests named test_add or test_subtract.-v option-v option runs tests in verbose mode, showing detailed test names and results.pytest --maxfail=2 -v but pytest does not stop after 2 failures. What is the likely cause?--maxfail behavior--maxfail option sets how many failures to allow before stopping, but it requires -x to actually stop the test run.-x, pytest will report max failures but continue running all tests.slow and generate a JUnit XML report saved as results.xml. Which command is correct?-m to select tests by marker-m option filters tests by marker name, so -m slow runs only tests marked as slow.--junitxml=filename to generate report--junitxml=results.xml option saves test results in JUnit XML format to the specified file.--mark, --xml, and --only-mark are not valid pytest flags.Pass the test and this pattern moves unknown