Discover how a few simple flags can save you hours of testing frustration!
Why Command-line options in PyTest? - Purpose & Use Cases
Imagine running your tests one by one manually, typing long commands each time to change settings or pick specific tests.
You have to remember all the options and type them perfectly every time.
This manual way is slow and tiring.
You might mistype commands or forget options, causing tests to run wrong or fail unexpectedly.
It's easy to get frustrated and waste time fixing simple mistakes.
Command-line options let you quickly customize test runs with simple flags and parameters.
You can easily select tests, set verbosity, or change behavior without rewriting code.
This makes running tests faster, clearer, and less error-prone.
pytest test_example.py
pytest test_example.py --maxfail=1
pytest test_example.py --verbosepytest test_example.py --maxfail=1 --verboseCommand-line options unlock fast, flexible control over test runs, making testing smooth and efficient.
A developer wants to stop tests after the first failure and see detailed output to fix bugs quickly.
Using command-line options, they run: pytest test_example.py --maxfail=1 --verbose and get exactly what they need instantly.
Manual test running is slow and error-prone.
Command-line options simplify and speed up test execution.
They help customize tests easily without changing code.
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
