A test function in pytest is a regular Python function whose name starts with test_. It contains code to check if a part of your program works correctly.
Click to reveal answer
beginner
How do you write a simple test function to check if 2 + 2 equals 4?
You write a function starting with test_ and use an assert statement like this:
def test_addition():
assert 2 + 2 == 4
Click to reveal answer
intermediate
Why should test functions be independent?
Test functions should be independent so that one test's result does not affect another. This makes tests reliable and easier to debug.
Click to reveal answer
beginner
What happens if an assertion in a test function fails?
If an assertion fails, pytest marks the test as failed and shows an error message explaining what went wrong.
Click to reveal answer
beginner
How does pytest find test functions to run?
Pytest automatically finds functions that start with test_ in files named test_*.py or *_test.py.
Click to reveal answer
What prefix should a function name have to be recognized as a test by pytest?
Acheck_
Btest_
Cverify_
Drun_
✗ Incorrect
Pytest recognizes functions starting with test_ as test functions.
What keyword is used inside a test function to check if a condition is true?
Aassert
Bcheck
Cverify
Dtest
✗ Incorrect
The assert keyword is used to verify conditions in test functions.
If a test function's assertion fails, what will pytest report?
ATest passed
BTest skipped
CTest failed
DTest ignored
✗ Incorrect
Pytest reports the test as failed if an assertion inside it fails.
Which file name pattern does pytest use to find test files?
Atest_*.py
Brun_*.py
Ccheck_*.py
Dverify_*.py
✗ Incorrect
Pytest looks for files named test_*.py or *_test.py to find tests.
Why is it important that test functions do not depend on each other?
ATo make tests run faster
BTo reduce code size
CTo use less memory
DTo avoid test failures caused by other tests
✗ Incorrect
Independent tests avoid failures caused by side effects from other tests.
Explain how to write a basic test function in pytest and what it should contain.
Think about a simple function that checks if something is true.
You got /4 concepts.
Describe why test functions should be independent and how pytest finds them.
Consider reliability and automatic test discovery.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a test function in pytest?
easy
A. To create user interfaces
B. To run the program faster
C. To check if the code works as expected
D. To store data permanently
Solution
Step 1: Understand the role of test functions
Test functions are written to verify that the code behaves correctly.
Step 2: Identify the main goal
The main goal is to check if the code works as expected, catching bugs early.
Final Answer:
To check if the code works as expected -> Option C
Quick Check:
Test functions = Verify code correctness [OK]
Hint: Test functions verify code behavior, not speed or UI [OK]
Common Mistakes:
Thinking test functions improve program speed
Confusing test functions with UI code
Assuming test functions store data
2. Which of the following is the correct way to name a test function in pytest?
easy
A. check_function()
B. TestFunction()
C. function_test()
D. test_function()
Solution
Step 1: Recall pytest naming rules
Pytest requires test functions to start with 'test_' to be recognized automatically.
Step 2: Identify the correct naming
Only 'test_function()' starts with 'test_', so it is correct.
Final Answer:
test_function() -> Option D
Quick Check:
Test function names start with 'test_' [OK]
Hint: Test functions must start with 'test_' prefix [OK]
Common Mistakes:
Using names without 'test_' prefix
Capitalizing function names incorrectly
Placing 'test' at the end of the name
3. What will be the result of running this pytest test function?
def test_sum():
result = 2 + 3
assert result == 5
medium
A. Test passes successfully
B. Test fails with AssertionError
C. SyntaxError occurs
D. Test is skipped automatically
Solution
Step 1: Analyze the test code
The function calculates 2 + 3 and stores in 'result'.
Step 2: Check the assertion
The assertion checks if 'result' equals 5, which is true.
Final Answer:
Test passes successfully -> Option A
Quick Check:
2 + 3 == 5 means test passes [OK]
Hint: If assert condition is true, test passes [OK]
Common Mistakes:
Assuming assertion fails when it is true
Confusing syntax errors with assertion errors
Thinking tests skip without skip decorators
4. Identify the error in this pytest test function:
def test_multiply():
result = 4 * 5
assert result = 20
medium
A. Incorrect assert syntax using '=' instead of '=='
B. Function name does not start with 'test_'
C. Multiplication operator '*' is invalid
D. Missing return statement in test function
Solution
Step 1: Check the assert statement syntax
The assert statement uses '=' which is assignment, not comparison.
Step 2: Identify correct syntax
Assert requires '==' to compare values, so '=' causes a syntax error.
Final Answer:
Incorrect assert syntax using '=' instead of '==' -> Option A
Quick Check:
Use '==' in assert, not '=' [OK]
Hint: Assert needs '==' for comparison, '=' causes error [OK]
Common Mistakes:
Using '=' instead of '==' in assert
Ignoring function naming rules
Thinking return is needed in test functions
5. You want to test a function that returns the maximum number from a list. Which pytest test function below correctly tests this behavior including an edge case for an empty list?
def max_number(numbers):
return max(numbers)
# Choose the correct test function: