Bird
Raised Fist0
PyTesttesting~10 mins

Test functions in PyTest - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a test function using pytest.

PyTest
def [1]():
    assert 1 + 1 == 2
Drag options to blanks, or click blank then click option'
Acheck_add
Btest_addition
Caddition_test
Dverify_sum
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the function without the 'test_' prefix.
Using names that pytest won't recognize as tests.
2fill in blank
medium

Complete the code to assert that a function returns the expected value.

PyTest
def test_double():
    result = double(3)
    assert result [1] 6
Drag options to blanks, or click blank then click option'
A==
B!=
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of ==.
Using comparison operators like < or > which do not check equality.
3fill in blank
hard

Fix the error in the test function name so pytest can discover it.

PyTest
def [1]():
    assert sum([1, 2, 3]) == 6
Drag options to blanks, or click blank then click option'
Atest_sum
Bverify_sum
Csum_test
Dcheck_sum
Attempts:
3 left
💡 Hint
Common Mistakes
Using function names without the 'test_' prefix.
Using names that look like tests but don't start with 'test_'.
4fill in blank
hard

Fill both blanks to complete a test function that checks if a list contains a value.

PyTest
def test_contains():
    items = [1, 2, 3]
    assert [1] in [2]
Drag options to blanks, or click blank then click option'
A2
Bitems
C3
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value not in the list.
Using an undefined variable for the list.
5fill in blank
hard

Fill all three blanks to create a test function that checks if a string starts with a prefix.

PyTest
def test_startswith():
    text = "hello world"
    prefix = "he"
    assert [1].[2]([3])
Drag options to blanks, or click blank then click option'
Atext
Bstartswith
Cprefix
Dendswith
Attempts:
3 left
💡 Hint
Common Mistakes
Using endswith instead of startswith.
Swapping the variable names.

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

  1. Step 1: Understand the role of test functions

    Test functions are written to verify that the code behaves correctly.
  2. Step 2: Identify the main goal

    The main goal is to check if the code works as expected, catching bugs early.
  3. Final Answer:

    To check if the code works as expected -> Option C
  4. 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

  1. Step 1: Recall pytest naming rules

    Pytest requires test functions to start with 'test_' to be recognized automatically.
  2. Step 2: Identify the correct naming

    Only 'test_function()' starts with 'test_', so it is correct.
  3. Final Answer:

    test_function() -> Option D
  4. 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

  1. Step 1: Analyze the test code

    The function calculates 2 + 3 and stores in 'result'.
  2. Step 2: Check the assertion

    The assertion checks if 'result' equals 5, which is true.
  3. Final Answer:

    Test passes successfully -> Option A
  4. 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

  1. Step 1: Check the assert statement syntax

    The assert statement uses '=' which is assignment, not comparison.
  2. Step 2: Identify correct syntax

    Assert requires '==' to compare values, so '=' causes a syntax error.
  3. Final Answer:

    Incorrect assert syntax using '=' instead of '==' -> Option A
  4. 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:
hard
A. def test_max_number(): assert max_number([1, 3, 2]) == 3 assert max_number([]) == None
B. def test_max_number(): assert max_number([1, 3, 2]) == 3 import pytest with pytest.raises(ValueError): max_number([])
C. def test_max_number(): assert max_number([1, 3, 2]) == 2 assert max_number([0]) == 0
D. def test_max_number(): assert max_number([1, 3, 2]) == 3 assert max_number([1, 3, 2]) == 1

Solution

  1. Step 1: Understand max() behavior on empty list

    Calling max([]) raises ValueError, so test must expect this error.
  2. Step 2: Check which test handles empty list correctly

    def test_max_number(): assert max_number([1, 3, 2]) == 3 import pytest with pytest.raises(ValueError): max_number([]) uses pytest.raises to check for ValueError, correctly testing the edge case.
  3. Final Answer:

    Test function that asserts max and expects ValueError on empty list -> Option B
  4. Quick Check:

    Use pytest.raises for exceptions in tests [OK]
Hint: Use pytest.raises to test expected errors [OK]
Common Mistakes:
  • Assuming max([]) returns None instead of error
  • Wrong expected max value in assertions
  • Testing same input with conflicting expected outputs
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