Command-line options in PyTest - Build an Automation Script
import subprocess import sys from pathlib import Path import pytest def test_pytest_command_line_options(tmp_path): # Create a sample test file with two tests, one fails test_file = tmp_path / "test_sample.py" test_file.write_text( """ import pytest def test_pass(): assert True def test_fail(): assert False """ ) # Run pytest with --maxfail=1 --disable-warnings -q result = subprocess.run( [sys.executable, "-m", "pytest", str(test_file), "--maxfail=1", "--disable-warnings", "-q"], capture_output=True, text=True ) # Check that return code is 1 (because of failure) assert result.returncode == 1, f"Expected return code 1 but got {result.returncode}" # Check output contains only one failure reported # The output should mention '1 failed' and not '2 failed' assert "1 failed" in result.stdout or "1 failed" in result.stderr, "Expected exactly one failure" assert "2 failed" not in result.stdout and "2 failed" not in result.stderr, "More than one failure reported" # Check warnings are not shown assert "WARNING" not in result.stdout and "WARNING" not in result.stderr, "Warnings should be suppressed" # Check output is quiet (no verbose info) # The -q option reduces output, so no 'collected' line assert "collected" not in result.stdout, "Output should be quiet without 'collected' line"
This test creates a temporary test file with two tests: one passes and one fails.
It runs pytest as a subprocess with the command-line options --maxfail=1 to stop after the first failure, --disable-warnings to suppress warnings, and -q for quiet output.
The test then asserts the return code is 1, indicating a failure occurred.
It checks the output to confirm only one failure is reported, no warnings appear, and the output is quiet without extra lines like 'collected'.
This approach uses subprocess to simulate real command-line usage and captures output for assertions, following best practices.
Now add data-driven testing to run the pytest command with different maxfail values: 1, 2, and 3, and verify the behavior accordingly
Practice
-v do when running tests?Solution
Step 1: Understand the
The-voption in pytest-voption stands for verbose mode, which means pytest will show detailed information about each test it runs.Step 2: Compare with other options
Options like quiet mode or coverage report are different flags, so-vspecifically increases output detail.Final Answer:
Runs tests in verbose mode, showing each test name -> Option DQuick Check:
Verbose = detailed output [OK]
- Confusing verbose with quiet mode
- Thinking it runs tests only once
- Mixing it with coverage report option
Solution
Step 1: Recall pytest option to stop on first failure
The correct option is-x, which tells pytest to stop after the first test failure.Step 2: Check other options for correctness
--stop-on-failureand--failfast=falseare invalid, and-fis not a recognized pytest option.Final Answer:
pytest -x -> Option AQuick Check:
Stop on first failure = -x [OK]
- Using long option names that don't exist
- Confusing -f with -x
- Assuming --failfast=false stops tests
pytest -k 'test_add or test_subtract' -v, what will pytest do?Solution
Step 1: Understand the
The-koption-koption filters tests by substring expressions. Here, it selects tests namedtest_addortest_subtract.Step 2: Understand the
The-voption-voption runs tests in verbose mode, showing detailed test names and results.Final Answer:
Run only tests named test_add or test_subtract with verbose output -> Option BQuick Check:
-k filters tests, -v shows details [OK]
- Thinking -k excludes tests instead of including
- Ignoring the verbose flag
- Assuming it stops on failure
pytest --maxfail=2 -v but pytest does not stop after 2 failures. What is the likely cause?Solution
Step 1: Understand
The--maxfailbehavior--maxfailoption sets how many failures to allow before stopping, but it requires-xto actually stop the test run.Step 2: Check command usage
Without-x, pytest will report max failures but continue running all tests.Final Answer:
You need to add -x to stop after max failures -> Option AQuick Check:
--maxfail needs -x to stop tests [OK]
- Thinking --maxfail alone stops tests
- Adding space between --maxfail and number
- Assuming -v disables maxfail
slow and generate a JUnit XML report saved as results.xml. Which command is correct?Solution
Step 1: Use
The-mto select tests by marker-moption filters tests by marker name, so-m slowruns only tests marked asslow.Step 2: Use
The--junitxml=filenameto generate report--junitxml=results.xmloption saves test results in JUnit XML format to the specified file.Step 3: Verify other options are invalid
Options like--mark,--xml, and--only-markare not valid pytest flags.Final Answer:
pytest -m slow --junitxml=results.xml -> Option CQuick Check:
-m for marker, --junitxml for report [OK]
- Using incorrect flags like --mark or --xml
- Forgetting = between --junitxml and filename
- Adding -x which stops tests unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
