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
Step 1: Analyze the Arrange step
List numbers is [1, 2, 3] and expected sum is 6, which is correct.
Step 2: Check the Act and Assert steps
sum(numbers) calculates 6, matching expected. The assertion will pass.
Final Answer:
Test will pass because sum(numbers) equals expected -> Option A
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
Step 1: Review the Act step
text.upper is a method reference, missing parentheses to call it.
Step 2: Understand the impact on Assert
Without calling upper(), result is a method, not a string, so assertion fails.
Final Answer:
Missing parentheses in Act step calling upper() -> Option A
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
Step 1: Understand the test goal
The test must verify that dividing by zero raises a ZeroDivisionError exception.
Step 2: Identify correct pytest usage
Using with pytest.raises(ZeroDivisionError): correctly checks for the exception during Act step.
Final Answer:
Using pytest.raises to check for ZeroDivisionError -> Option D
Quick Check:
Use pytest.raises to test exceptions [OK]
Hint: Use pytest.raises to assert exceptions in tests [OK]