Bird
Raised Fist0
PyTesttesting~10 mins

Basic assert statement 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 assert that the sum of 2 and 3 equals 5.

PyTest
def test_sum():
    result = 2 + 3
    assert result [1] 5
Drag options to blanks, or click blank then click option'
A=
B<
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' in the assert statement.
Using '!=' which checks for inequality.
2fill in blank
medium

Complete the code to assert that the length of the list is 4.

PyTest
def test_list_length():
    items = [1, 2, 3, 4]
    assert len(items) [1] 4
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '>' which check less or greater than, not equality.
Using '!=' which checks for inequality.
3fill in blank
hard

Fix the error in the assert statement to correctly check if the variable is True.

PyTest
def test_is_active():
    is_active = True
    assert is_active [1] True
Drag options to blanks, or click blank then click option'
A==
Bis
C=
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' in assert.
Using 'is' which is for identity, not equality.
4fill in blank
hard

Fill both blanks to assert that the variable score is greater than 50.

PyTest
def test_score():
    score = 75
    assert score [1] [2]
Drag options to blanks, or click blank then click option'
A>
B50
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' for greater than check.
Using '<' which checks less than.
5fill in blank
hard

Fill all three blanks to assert that the length of names is less than 5 and the first name is 'Alice'.

PyTest
def test_names():
    names = ['Alice', 'Bob', 'Charlie']
    assert len(names) [1] [2] and names[0] [3] 'Alice'
Drag options to blanks, or click blank then click option'
A<
B5
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' for length check.
Using '=' instead of '==' for string comparison.

Practice

(1/5)
1. What does the assert statement do in a pytest test?
easy
A. Skips the test without running it
B. Runs the test multiple times automatically
C. Prints a message to the console
D. Checks if a condition is true and stops the test if false

Solution

  1. Step 1: Understand the purpose of assert

    The assert statement checks if a condition is true during a test.
  2. Step 2: Know the effect of assert failing

    If the condition is false, the test stops and reports failure.
  3. Final Answer:

    Checks if a condition is true and stops the test if false -> Option D
  4. Quick Check:

    Assert = condition check [OK]
Hint: Assert stops test on false condition [OK]
Common Mistakes:
  • Thinking assert runs tests multiple times
  • Confusing assert with print statements
  • Believing assert skips tests
2. Which of the following is the correct syntax for an assert statement in pytest?
easy
A. assert(x == 5; "x should be 5")
B. assert x == 5, "x should be 5"
C. assert x = 5, "x should be 5"
D. assert x == 5 then "x should be 5"

Solution

  1. Step 1: Review correct assert syntax

    In Python, assert is followed by a condition, optionally a comma and a message string.
  2. Step 2: Identify syntax errors in options

    assert x == 5, "x should be 5" uses correct syntax: assert condition, message. Others have wrong punctuation or assignment.
  3. Final Answer:

    assert x == 5, "x should be 5" -> Option B
  4. Quick Check:

    assert condition, message [OK]
Hint: Use comma after condition for message [OK]
Common Mistakes:
  • Using assignment (=) instead of comparison (==)
  • Adding parentheses incorrectly
  • Using wrong keywords like 'then'
3. What will be the result of this pytest assertion?
def test_value():
    x = 10
    assert x < 5, "x is not less than 5"
medium
A. Test passes silently
B. Test is skipped
C. Test fails with message 'x is not less than 5'
D. SyntaxError occurs

Solution

  1. Step 1: Evaluate the condition in assert

    The condition is x < 5, but x is 10, so 10 < 5 is false.
  2. Step 2: Understand assert failure behavior

    Since condition is false, the test fails and shows the message after the comma.
  3. Final Answer:

    Test fails with message 'x is not less than 5' -> Option C
  4. Quick Check:

    False condition triggers failure [OK]
Hint: False assert condition fails test with message [OK]
Common Mistakes:
  • Assuming test passes when condition is false
  • Expecting syntax error for valid assert
  • Thinking test skips on failure
4. Identify the error in this pytest assertion:
def test_sum():
    total = 3 + 4
    assert total = 7, "Sum should be 7"
medium
A. Using assignment (=) instead of comparison (==) in assert
B. Missing parentheses around assert condition
C. Incorrect indentation of assert statement
D. Wrong message format after assert

Solution

  1. Step 1: Check assert condition syntax

    The assert condition must use comparison (==), not assignment (=).
  2. Step 2: Identify the error in the code

    Here, 'assert total = 7' uses assignment, which is invalid syntax.
  3. Final Answer:

    Using assignment (=) instead of comparison (==) in assert -> Option A
  4. Quick Check:

    Use '==' for comparison in assert [OK]
Hint: Use '==' not '=' in assert condition [OK]
Common Mistakes:
  • Confusing assignment with comparison
  • Adding unnecessary parentheses
  • Misplacing the error in indentation
5. You want to check if a list items contains exactly 3 elements using pytest assert. Which is the best assert statement?
hard
A. assert len(items) == 3, "List does not have 3 elements"
B. assert items == 3, "List length check failed"
C. assert items.length == 3, "Wrong length"
D. assert len(items) = 3, "Length must be 3"

Solution

  1. Step 1: Understand how to get list length in Python

    Use the built-in function len() to get the number of elements in a list.
  2. Step 2: Check correct assert syntax for length comparison

    Compare len(items) with 3 using '==', and provide a clear message after a comma.
  3. Final Answer:

    assert len(items) == 3, "List does not have 3 elements" -> Option A
  4. Quick Check:

    len(list) == number [OK]
Hint: Use len() and '==' to check list size [OK]
Common Mistakes:
  • Comparing list directly to number
  • Using JavaScript style .length property
  • Using assignment '=' instead of '=='
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