We run tests to check if our code works correctly. The pytest command helps us run all tests easily.
Running tests (pytest command)
Introduction
Syntax
PyTest
pytest [options] [file_or_dir]
You can run pytest without arguments to run all tests in the current folder.
Use options like -v for more details or -k to run specific tests.
Examples
PyTest
pytest
PyTest
pytest -v
test_example.py.PyTest
pytest test_example.py
test_function_name.PyTest
pytest -k test_function_name
Sample Program
This simple test checks if the add function returns correct sums. Running pytest -v will show each test and if it passed.
PyTest
def add(a, b): return a + b def test_add(): assert add(2, 3) == 5 assert add(-1, 1) == 0 # Run this in terminal: # pytest -v
Important Notes
Make sure your test files and functions start with test_ so pytest finds them.
You can stop tests early with Ctrl+C if needed.
Summary
The pytest command runs your tests automatically.
Use options like -v for detailed results or specify files to run specific tests.
Running tests often helps catch mistakes early and keeps your code working well.
Practice
1. What does the basic
pytest command do when run in a project folder?easy
Solution
Step 1: Understand the default behavior of pytest command
Runningpytestwithout arguments searches for test files and runs all tests found.Step 2: Eliminate incorrect options
Options A, C, and D describe actions pytest does not perform by default.Final Answer:
It automatically finds and runs all test files and test functions. -> Option BQuick Check:
pytest runs tests automatically = B [OK]
Hint: pytest runs all tests by default when no file is specified [OK]
Common Mistakes:
- Thinking pytest only checks syntax
- Assuming pytest deletes files
- Confusing pytest with package installer
2. Which of the following is the correct command to run pytest with detailed output?
easy
Solution
Step 1: Recall pytest option for verbose output
The-voption stands for verbose and shows detailed test results.Step 2: Check other options for correctness
Options-d,--detail, and--verbose-outputare not valid pytest flags.Final Answer:
pytest -v -> Option AQuick Check:
Verbose flag is -v = C [OK]
Hint: Use -v for verbose pytest output [OK]
Common Mistakes:
- Using -d instead of -v
- Typing long incorrect flags
- Confusing verbose with debug flags
3. Given the command
pytest test_math.py, what will happen?medium
Solution
Step 1: Understand pytest file argument behavior
When a filename is given, pytest runs tests only from that file.Step 2: Eliminate incorrect options
A: Runs all tests in all files (wrong when file specified). B: Ignores test_math.py (wrong). C: Misunderstands filename as test name search (wrong).Final Answer:
pytest runs only the tests inside the file named test_math.py. -> Option CQuick Check:
pytest filename runs tests in that file only = D [OK]
Hint: Specify filename to run tests only from that file [OK]
Common Mistakes:
- Assuming pytest runs all tests ignoring filename
- Confusing filename with test function name
- Thinking pytest ignores specified files
4. You run
pytest -v test_example.py but get an error: pytest: error: unrecognized arguments: -v. What is the likely cause?medium
Solution
Step 1: Understand the error message
The error says-vis unrecognized, which is unusual because-vis a valid pytest option.Step 2: Identify environment or command issues
This usually means pytest is not properly installed or a different command is running (like python or another tool).Final Answer:
You are running pytest with a wrong command or from a wrong environment. -> Option DQuick Check:
Unrecognized option means wrong environment = A [OK]
Hint: Check pytest installation and environment if options fail [OK]
Common Mistakes:
- Assuming -v is invalid option
- Ignoring environment issues
- Blaming test file existence for option errors
5. You want to run only tests marked as
slow in your project using pytest. Which command correctly runs only those tests?hard
Solution
Step 1: Recall pytest marker option
The-moption runs tests matching a marker expression, such asslow.Step 2: Verify other options
Options--only,--mark, and-sdo not filter tests by marker.Final Answer:
pytest -m slow -> Option AQuick Check:
Use -m to run tests by marker = A [OK]
Hint: Use -m marker to run tests with that marker [OK]
Common Mistakes:
- Using incorrect flags like --only or --mark
- Confusing -s (capture output) with marker filtering
- Not marking tests before filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
