Recall & Review
beginner
What is the purpose of the
assert statement in pytest?The
assert statement checks if a condition is true. If it is false, the test fails and pytest shows an error.Click to reveal answer
beginner
How does pytest report a failed
assert statement?Pytest shows the exact expression that failed and the values involved, helping you understand why the test failed.
Click to reveal answer
beginner
Write a simple pytest
assert statement to check if 5 is greater than 3.def test_greater():
assert 5 > 3 This test will pass because 5 is indeed greater than 3.Click to reveal answer
intermediate
Can
assert statements include messages? How?Yes, you can add a message after a comma to explain the failure.<br>
assert x == 10, "x should be 10"
Click to reveal answer
beginner
Why is using
assert better than printing values for testing?Because
assert automatically fails the test if the condition is false, making tests clear and automated without manual checks.Click to reveal answer
What happens if an
assert condition is false in pytest?✗ Incorrect
If the
assert condition is false, pytest marks the test as failed and shows the error details.Which of these is a valid assert statement in pytest?
✗ Incorrect
Option A uses correct syntax and logic. Option B has wrong syntax, A is not valid Python, D compares int to string.
How can you add a custom message to an assert statement?
✗ Incorrect
You add a message after a comma in the assert statement to explain failure.
What does pytest show when an assert fails?
✗ Incorrect
Pytest shows the exact expression that failed and the values involved to help debugging.
Why is using assert statements important in automated testing?
✗ Incorrect
Assert statements make tests automatic by checking conditions and failing tests when conditions are false.
Explain how the assert statement works in pytest and why it is useful.
Think about how assert helps catch errors automatically.
You got /4 concepts.
Write a simple pytest test function using assert to check if a number is positive.
Use assert with a condition like x > 0 inside a function starting with test_
You got /3 concepts.