What is the purpose of command-line options in pytest?
Command-line options in pytest allow you to customize test runs, such as selecting tests, setting verbosity, or enabling features without changing the code.
Click to reveal answer
beginner
How do you run pytest with increased verbosity using command-line options?
Use the -v or --verbose option to increase verbosity, showing more details about each test run.
Click to reveal answer
intermediate
What does the -k option do in pytest?
The -k option lets you run tests that match a specific expression or substring in their names, helping to select tests easily.
Click to reveal answer
intermediate
Explain the use of the --maxfail option in pytest.
The --maxfail=N option stops the test run after N failures, which helps save time by not running all tests if many fail.
Click to reveal answer
beginner
How can you run tests from a specific file using pytest command-line options?
You can specify the test file path directly after the pytest command, for example: pytest tests/test_example.py to run tests only from that file.
Click to reveal answer
Which pytest option runs tests with detailed output?
A--maxfail
B-v
C-q
D-k
✗ Incorrect
The -v option increases verbosity, showing detailed output for each test.
What does the -k option do in pytest?
ARuns tests matching a name pattern
BStops after first failure
CRuns tests quietly
DSets maximum failures allowed
✗ Incorrect
The -k option filters tests by name or expression.
How do you stop pytest after 3 test failures?
A-v 3
B--failmax=3
C-k 3
D--maxfail=3
✗ Incorrect
Use --maxfail=3 to stop after 3 failures.
Which command runs tests only from a specific file?
Apytest tests/test_file.py
Bpytest -k test_file
Cpytest --maxfail=1
Dpytest -v
✗ Incorrect
Specifying the file path runs tests only from that file.
What does the -q option do in pytest?
AFilters tests by name
BRuns tests with detailed output
CRuns tests quietly with less output
DStops after first failure
✗ Incorrect
The -q option reduces output for a quieter test run.
Describe how you can use pytest command-line options to select and control test execution.
Think about filtering tests and controlling output.
You got /4 concepts.
Explain why command-line options are useful when running tests with pytest.
Consider flexibility and efficiency.
You got /4 concepts.
Practice
(1/5)
1. What does the pytest command-line option -v do when running tests?
easy
A. Runs tests only once without reruns
B. Runs tests in quiet mode, suppressing output
C. Generates a test coverage report
D. Runs tests in verbose mode, showing each test name
Solution
Step 1: Understand the -v option in pytest
The -v option 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 -v specifically increases output detail.
Final Answer:
Runs tests in verbose mode, showing each test name -> Option D
Quick 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
A. pytest -x
B. pytest --stop-on-failure
C. pytest --failfast=false
D. pytest -f
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-failure and --failfast=false are invalid, and -f is not a recognized pytest option.
Final Answer:
pytest -x -> Option A
Quick 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
A. Run all tests except those named test_add or test_subtract
B. Run only tests named test_add or test_subtract with verbose output
C. Run tests with names containing 'test' only
D. Run tests and stop after first failure
Solution
Step 1: Understand the -k option
The -k option filters tests by substring expressions. Here, it selects tests named test_add or test_subtract.
Step 2: Understand the -v option
The -v option 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 B
Quick 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
A. You need to add -x to stop after max failures
B. The option --maxfail requires a space before the number
C. The --maxfail option is deprecated and ignored
D. You must use --maxfail=2 without -v
Solution
Step 1: Understand --maxfail behavior
The --maxfail option sets how many failures to allow before stopping, but it requires -x to 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 A
Quick 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
A. pytest --only-mark slow --junitxml results.xml
B. pytest --mark slow --xml=results.xml
C. pytest -m slow --junitxml=results.xml
D. pytest -m slow -x --junit=results.xml
Solution
Step 1: Use -m to select tests by marker
The -m option filters tests by marker name, so -m slow runs only tests marked as slow.
Step 2: Use --junitxml=filename to generate report
The --junitxml=results.xml option saves test results in JUnit XML format to the specified file.
Step 3: Verify other options are invalid
Options like --mark, --xml, and --only-mark are not valid pytest flags.
Final Answer:
pytest -m slow --junitxml=results.xml -> Option C
Quick 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 unknownstrongwhile earning +1 Grip toward your certificate.