Bird
Raised Fist0
PyTesttesting~20 mins

Arrange-Act-Assert pattern in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Arrange-Act-Assert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Identify the output of a pytest test using Arrange-Act-Assert
What will be the result of running this pytest test function?
PyTest
def add(a, b):
    return a + b

def test_add():
    # Arrange
    x = 5
    y = 3
    # Act
    result = add(x, y)
    # Assert
    assert result == 8
ATest passes successfully
BTypeError during test execution
CSyntaxError during test execution
DTest fails with AssertionError
Attempts:
2 left
💡 Hint
Check if the assertion matches the expected sum of x and y.
assertion
intermediate
2:00remaining
Choose the correct assertion for Arrange-Act-Assert pattern
Given the following test code snippet, which assertion correctly verifies the output?
PyTest
def multiply(a, b):
    return a * b

def test_multiply():
    # Arrange
    a = 4
    b = 6
    # Act
    output = multiply(a, b)
    # Assert
    # Which assertion is correct here?
Aassert output < 20
Bassert output != 24
Cassert output > 30
Dassert output == 24
Attempts:
2 left
💡 Hint
Multiply 4 by 6 and check the expected result.
🔧 Debug
advanced
2:00remaining
Find the bug in this Arrange-Act-Assert pytest test
This test is supposed to check if the function returns the square of a number. What is the bug causing the test to fail?
PyTest
def square(n):
    return n * n

def test_square():
    # Arrange
    num = 7
    # Act
    result = square(num)
    # Assert
    assert result == 42
AThe function square is not defined correctly
BThe assertion expects 42 but 7 squared is 49
CThe variable num is not assigned properly
DThe test is missing the Act step
Attempts:
2 left
💡 Hint
Calculate 7 squared and compare with the assertion value.
framework
advanced
2:00remaining
Identify the correct Arrange-Act-Assert structure in pytest
Which of the following pytest test functions correctly follows the Arrange-Act-Assert pattern?
A
def test_func():
    x = 10
    y = 20
    result = x + y
    assert result == 30
B
def test_func():
    assert 10 + 20 == 30
    x = 10
    y = 20
    result = x + y
C
def test_func():
    result = 10 + 20
    x = 10
    y = 20
    assert result == 30
D
def test_func():
    x = 10
    assert x == 10
    y = 20
    result = x + y
Attempts:
2 left
💡 Hint
Arrange means setting up variables first, then Act, then Assert last.
🧠 Conceptual
expert
2:00remaining
Why is the Arrange-Act-Assert pattern important in testing?
Select the best explanation for the importance of the Arrange-Act-Assert pattern in writing tests.
AIt allows skipping the assertion step if the test runs without errors.
BIt enforces using only one assertion per test function.
CIt organizes tests clearly into setup, execution, and verification steps, improving readability and maintenance.
DIt requires tests to run faster by combining all steps into one line.
Attempts:
2 left
💡 Hint
Think about how clear structure helps when reading or fixing tests later.

Practice

(1/5)
1. What is the main purpose of the Arrange-Act-Assert pattern in pytest?
easy
A. To speed up test execution time
B. To organize tests into clear steps for better readability
C. To automatically generate test data
D. To replace the need for assertions in tests

Solution

  1. Step 1: Understand the pattern's goal

    The Arrange-Act-Assert pattern structures tests into three parts: setup, action, and verification.
  2. Step 2: Identify the main benefit

    This structure makes tests easier to read and maintain by clearly separating steps.
  3. Final Answer:

    To organize tests into clear steps for better readability -> Option B
  4. Quick Check:

    Arrange-Act-Assert = Organize tests clearly [OK]
Hint: Remember: Arrange sets up, Act performs, Assert checks [OK]
Common Mistakes:
  • Thinking it speeds up test execution
  • Confusing it with test data generation
  • Believing it removes the need for assertions
2. Which of the following is the correct order of steps in the Arrange-Act-Assert pattern?
easy
A. Act, Arrange, Assert
B. Assert, Arrange, Act
C. Arrange, Act, Assert
D. Act, Assert, Arrange

Solution

  1. Step 1: Recall the pattern sequence

    The pattern always starts with Arrange (setup), then Act (perform action), and finally Assert (check results).
  2. Step 2: Match the correct order

    Only Arrange, Act, Assert follows this exact sequence.
  3. Final Answer:

    Arrange, Act, Assert -> Option C
  4. Quick Check:

    Order is Arrange -> Act -> Assert [OK]
Hint: Think: Prepare, Do, Check in that order [OK]
Common Mistakes:
  • Mixing up the order of steps
  • Starting with Assert before Act
  • Confusing Act and Arrange steps
3. Given the following pytest test code, what will be the test result?
def test_sum():
    # Arrange
    numbers = [1, 2, 3]
    expected = 6

    # Act
    result = sum(numbers)

    # Assert
    assert result == expected
medium
A. Test will pass because sum(numbers) equals expected
B. Test will fail because sum is not defined
C. Test will fail because expected is incorrect
D. Test will error due to syntax mistake

Solution

  1. Step 1: Analyze the Arrange step

    List numbers is [1, 2, 3] and expected sum is 6, which is correct.
  2. Step 2: Check the Act and Assert steps

    sum(numbers) calculates 6, matching expected. The assertion will pass.
  3. Final Answer:

    Test will pass because sum(numbers) equals expected -> Option A
  4. Quick Check:

    sum([1,2,3]) = 6, assertion true [OK]
Hint: Check if actual equals expected in Assert step [OK]
Common Mistakes:
  • Assuming sum is undefined
  • Thinking expected value is wrong
  • Looking for syntax errors where none exist
4. Identify the error in this pytest test using Arrange-Act-Assert pattern:
def test_uppercase():
    # Arrange
    text = "hello"

    # Act
    result = text.upper

    # Assert
    assert result == "HELLO"
medium
A. Missing parentheses in Act step calling upper()
B. Incorrect expected value in Assert step
C. Variable text is not defined in Arrange step
D. Assert statement syntax is invalid

Solution

  1. Step 1: Review the Act step

    text.upper is a method reference, missing parentheses to call it.
  2. Step 2: Understand the impact on Assert

    Without calling upper(), result is a method, not a string, so assertion fails.
  3. Final Answer:

    Missing parentheses in Act step calling upper() -> Option A
  4. Quick Check:

    Call methods with () to get results [OK]
Hint: Remember to call methods with parentheses () [OK]
Common Mistakes:
  • Thinking expected value is wrong
  • Assuming variable is undefined
  • Believing assert syntax is incorrect
5. You want to test a function divide(a, b) that returns the division of two numbers. Using Arrange-Act-Assert, which test correctly checks that dividing by zero raises a ZeroDivisionError?
hard
A. def test_divide_zero(): # Arrange a, b = 10, 0 # Act result = divide(a, b) # Assert assert result == 0
B. def test_divide_zero(): # Arrange a, b = 10, 0 # Act result = divide(a, b) # Assert assert result is None
C. def test_divide_zero(): # Arrange a, b = 10, 0 # Act divide(a, b) # Assert assert True
D. def test_divide_zero(): # Arrange a, b = 10, 0 # Act & Assert with pytest.raises(ZeroDivisionError): divide(a, b)

Solution

  1. Step 1: Understand the test goal

    The test must verify that dividing by zero raises a ZeroDivisionError exception.
  2. Step 2: Identify correct pytest usage

    Using with pytest.raises(ZeroDivisionError): correctly checks for the exception during Act step.
  3. Final Answer:

    Using pytest.raises to check for ZeroDivisionError -> Option D
  4. Quick Check:

    Use pytest.raises to test exceptions [OK]
Hint: Use pytest.raises to assert exceptions in tests [OK]
Common Mistakes:
  • Ignoring exception and asserting wrong result
  • Not using pytest.raises for exception testing
  • Asserting True without checking error