Command-line options let you control how tests run without changing the code. They make testing flexible and easy.
Command-line options in PyTest
Introduction
Syntax
PyTest
pytest [options] [file_or_dir] [additional_args]
Options start with one or two dashes, like -v or --maxfail=2.
You can combine options to customize test runs easily.
Examples
PyTest
pytest -v
PyTest
pytest --maxfail=1PyTest
pytest tests/test_example.py
PyTest
pytest -k 'login and not slow'Sample Program
This script has three tests. Running with -v shows detailed results. Using --maxfail=1 stops after the first failure.
PyTest
import pytest def test_add(): assert 1 + 1 == 2 def test_subtract(): assert 2 - 1 == 1 def test_fail(): assert 1 == 0 # Run this in command line: # pytest -v --maxfail=1
Important Notes
Use pytest --help to see all available command-line options.
Combine options to fit your testing needs, like pytest -v -k 'test_name'.
Options make running tests faster and more focused without changing test code.
Summary
Command-line options customize how pytest runs tests.
They help run specific tests, control output, and stop on failures.
Using options saves time and makes testing easier.
Practice
1. What does the pytest command-line option
-v do when running tests?easy
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]
Hint: Verbose means more details shown during test run [OK]
Common Mistakes:
- Confusing verbose with quiet mode
- Thinking it runs tests only once
- Mixing it with coverage report option
2. Which of the following is the correct syntax to run pytest and stop after the first failure?
easy
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]
Hint: Use -x to stop pytest after first failure [OK]
Common Mistakes:
- Using long option names that don't exist
- Confusing -f with -x
- Assuming --failfast=false stops tests
3. Given the command
pytest -k 'test_add or test_subtract' -v, what will pytest do?medium
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]
Hint: -k filters tests by name, -v shows details [OK]
Common Mistakes:
- Thinking -k excludes tests instead of including
- Ignoring the verbose flag
- Assuming it stops on failure
4. You run
pytest --maxfail=2 -v but pytest does not stop after 2 failures. What is the likely cause?medium
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]
Hint: Add -x with --maxfail to stop tests early [OK]
Common Mistakes:
- Thinking --maxfail alone stops tests
- Adding space between --maxfail and number
- Assuming -v disables maxfail
5. You want to run pytest only on tests marked as
slow and generate a JUnit XML report saved as results.xml. Which command is correct?hard
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]
Hint: Use -m for markers and --junitxml for reports [OK]
Common Mistakes:
- Using incorrect flags like --mark or --xml
- Forgetting = between --junitxml and filename
- Adding -x which stops tests unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
