PyTest is a tool to write and run tests for Python code. It helps check if your code works as expected.
Click to reveal answer
beginner
How do you name a test function in PyTest?
Test functions should start with the word test_. For example, def test_addition():.
Click to reveal answer
beginner
What does an assertion do in a PyTest test?
An assertion checks if a condition is true. If it is false, the test fails and shows an error.
Click to reveal answer
beginner
How do you run tests using PyTest?
You run tests by typing pytest in the command line inside your project folder.
Click to reveal answer
beginner
What is the output when a PyTest test passes?
PyTest shows a dot . for each passing test and a summary saying how many tests passed.
Click to reveal answer
Which prefix should a PyTest test function have?
Atest_
Bcheck_
Crun_
Dassert_
✗ Incorrect
PyTest recognizes test functions by the prefix test_.
What happens if an assertion in a PyTest test fails?
APyTest skips the test
BThe test passes anyway
CThe test fails and shows an error
DPyTest crashes
✗ Incorrect
An assertion failure means the test did not meet the expected condition, so it fails.
How do you run all tests in a folder using PyTest?
ARun <code>run_tests()</code> in Python
BRun <code>python test.py</code>
COpen tests in an editor
DRun <code>pytest</code> in the command line
✗ Incorrect
Typing pytest in the command line runs all tests it finds in the folder.
What symbol does PyTest show for a passing test?
AF
B.
CE
DS
✗ Incorrect
PyTest shows a dot . for each test that passes.
Which of these is a valid simple PyTest test function?
Adef test_sum(): assert 1 + 1 == 2
Bdef check_sum(): assert 1 + 1 == 2
Cdef sum_test(): assert 1 + 1 == 2
Ddef sum(): assert 1 + 1 == 2
✗ Incorrect
Only functions starting with test_ are recognized as tests by PyTest.
Explain how to write and run your first test using PyTest.
Think about the steps from writing code to seeing the test result.
You got /4 concepts.
Describe what happens when a PyTest test passes and when it fails.
Focus on the test output symbols and messages.
You got /3 concepts.
Practice
(1/5)
1. What does PyTest look for to identify a test function?
easy
A. Functions decorated with @pytest.test
B. Functions ending with _test
C. Functions starting with test_
D. Functions containing assert keyword
Solution
Step 1: Understand PyTest naming convention
PyTest automatically finds functions whose names start with test_.
Step 2: Compare options with PyTest rules
Only functions starting with test_ are recognized as tests by default.
Final Answer:
Functions starting with test_ -> Option C
Quick Check:
PyTest test function = starts with test_ [OK]
Hint: Look for functions starting with test_ to find tests [OK]
Common Mistakes:
Thinking functions ending with _test are tests
Believing assert keyword alone marks a test
Assuming decorators are needed for basic tests
2. Which of the following is the correct syntax for a simple PyTest test function?
easy
A. def add_test():\n assert 1 + 1 == 2
B. def test_add():\n assert 1 + 1 == 2
C. def test_add():\n assert 1 + 1 == 3
D. def testAdd():\n assert 1 + 1 == 2
Solution
Step 1: Check function name format
PyTest requires test functions to start with test_, so test_add is correct.
Step 2: Verify assertion correctness
The assertion 1 + 1 == 2 is true, so the test will pass.
Final Answer:
def test_add():\n assert 1 + 1 == 2 -> Option B
Quick Check:
Correct test name and true assert = def test_add():\n assert 1 + 1 == 2 [OK]
Hint: Use def test_...() with assert for valid PyTest tests [OK]
Common Mistakes:
Naming function without test_ prefix
Using incorrect assertion values
Using camelCase instead of snake_case for test names
3. What will be the result when running this PyTest test?
def test_example():
x = 5
y = 2
assert x - y == 3
medium
A. Test passes successfully
B. SyntaxError occurs
C. Test fails with AssertionError
D. Test is skipped automatically
Solution
Step 1: Evaluate the assertion expression
Calculate x - y which is 5 - 2 = 3.
Step 2: Compare with asserted value
The assertion checks if 3 == 3, which is true, so no error is raised.
Final Answer:
Test passes successfully -> Option A
Quick Check:
5 - 2 equals 3 means assert passes [OK]
Hint: Calculate assert expression; true means test passes [OK]
Common Mistakes:
Assuming test fails without checking assertion
Confusing syntax errors with assertion failures
Thinking tests skip without skip decorators
4. Identify the error in this PyTest test function:
def test_sum():
total = 2 + 2
assert total = 4
medium
A. Using single equals (=) instead of double equals (==) in assert
B. Function name does not start with test_
C. Missing return statement in test function
D. Indentation error in function body
Solution
Step 1: Check the assert statement syntax
The assert uses = which is assignment, not comparison. It should be ==.
Step 2: Confirm function name and indentation
The function name starts with test_ and indentation is correct, so no error there.
Final Answer:
Using single equals (=) instead of double equals (==) in assert -> Option A
Quick Check:
Assert needs ==, not = [OK]
Hint: Use == in assert, not = for comparison [OK]
Common Mistakes:
Using = instead of == in assert
Thinking return is needed in test functions
Confusing indentation errors with syntax errors
5. You want to test a function multiply(a, b) that returns the product of two numbers. Which PyTest test function correctly tests that multiply(3, 4) returns 12?
hard
A. def test_multiply():\n assert multiply(3, 4) == 7
B. def test_multiply():\n assert multiply(3, 4) = 12
C. def multiply_test():\n assert multiply(3, 4) == 12
D. def test_multiply():\n assert multiply(3, 4) == 12
Solution
Step 1: Verify test function name
The function name must start with test_, so test_multiply is correct.
Step 2: Check assertion correctness
The assertion must compare with == and the expected result is 12, so assert multiply(3, 4) == 12 is correct.
Final Answer:
def test_multiply():\n assert multiply(3, 4) == 12 -> Option D
Quick Check:
Correct name and assert with == 12 = def test_multiply():\n assert multiply(3, 4) == 12 [OK]
Hint: Test functions start with test_ and assert expected == actual [OK]
Common Mistakes:
Wrong expected value in assert
Wrong function name prefix
Using = instead of == in assert
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.