Bird
Raised Fist0
PyTesttesting~20 mins

Command-line options in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Command-line Options Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of pytest with verbose option
What is the output behavior when running pytest -v on a test file with 3 passing tests?
PyTest
def test_one():
    assert 1 == 1

def test_two():
    assert 2 == 2

def test_three():
    assert 3 == 3
Apytest shows only a summary line with dots for each test
Bpytest runs tests silently without showing any test names
Cpytest stops after the first test and shows only one test result
Dpytest shows detailed test names and statuses for all 3 tests
Attempts:
2 left
💡 Hint
The -v option stands for verbose output.
❓ assertion
intermediate
2:00remaining
Correct assertion for exit code with pytest --maxfail=1
You run pytest --maxfail=1 on a test suite with multiple failing tests. Which assertion correctly checks that pytest stopped after the first failure?
PyTest
import subprocess
result = subprocess.run(['pytest', '--maxfail=1'], capture_output=True, text=True)
exit_code = result.returncode
Aassert exit_code == 1 # pytest exits with 1 on failure
Bassert exit_code == None # pytest does not return exit code
Cassert exit_code > 1 # pytest exits with code >1 on maxfail
Dassert exit_code == 0 # pytest exits with 0 even if tests fail
Attempts:
2 left
💡 Hint
pytest returns 0 if all tests pass, 1 if any test fails.
❓ locator
advanced
2:00remaining
Best locator for pytest to run tests in a specific directory
You want to run pytest only on tests inside the tests/integration folder. Which command correctly locates and runs those tests?
Apytest --dir=tests/integration
Bpytest -k tests/integration
Cpytest tests/integration
Dpytest -m tests/integration
Attempts:
2 left
💡 Hint
pytest accepts folder paths as positional arguments to run tests there.
🔧 Debug
advanced
2:00remaining
Identify the error in pytest command usage
Which pytest command will cause an error when trying to run tests with keyword expression login and not slow?
Apytest -k login and not slow
Bpytest -k 'login and not slow'
Cpytest -k "login and not slow"
Dpytest -k "login and not slow" --maxfail=2
Attempts:
2 left
💡 Hint
Shell interprets unquoted words separately.
❓ framework
expert
2:00remaining
Effect of pytest --tb=short option on test failure output
What is the effect of running pytest with the --tb=short option on the traceback shown for failed tests?
AShows the full detailed traceback with all calls
BShows a shortened traceback with only the last call and error message
CSuppresses all traceback output completely
DShows traceback only for tests marked as slow
Attempts:
2 left
💡 Hint
The --tb option controls traceback verbosity.

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

  1. 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.
  2. Step 2: Compare with other options

    Options like quiet mode or coverage report are different flags, so -v specifically increases output detail.
  3. Final Answer:

    Runs tests in verbose mode, showing each test name -> Option D
  4. 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

  1. 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.
  2. Step 2: Check other options for correctness

    --stop-on-failure and --failfast=false are invalid, and -f is not a recognized pytest option.
  3. Final Answer:

    pytest -x -> Option A
  4. 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

  1. Step 1: Understand the -k option

    The -k option filters tests by substring expressions. Here, it selects tests named test_add or test_subtract.
  2. Step 2: Understand the -v option

    The -v option runs tests in verbose mode, showing detailed test names and results.
  3. Final Answer:

    Run only tests named test_add or test_subtract with verbose output -> Option B
  4. 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

  1. 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.
  2. Step 2: Check command usage

    Without -x, pytest will report max failures but continue running all tests.
  3. Final Answer:

    You need to add -x to stop after max failures -> Option A
  4. 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

  1. 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.
  2. Step 2: Use --junitxml=filename to generate report

    The --junitxml=results.xml option saves test results in JUnit XML format to the specified file.
  3. Step 3: Verify other options are invalid

    Options like --mark, --xml, and --only-mark are not valid pytest flags.
  4. Final Answer:

    pytest -m slow --junitxml=results.xml -> Option C
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard