Verify PyTest assertion mechanism using assert statement
Preconditions (2)
✅ Expected Result: Test passes successfully without errors, confirming assert works as PyTest's core mechanism
def test_sum(): result = 2 + 3 assert result == 5
This test function test_sum calculates the sum of 2 and 3, then uses the assert statement to check if the result is 5. PyTest automatically detects functions starting with test_ and runs them. The assert statement is the core mechanism because PyTest interprets it to determine if the test passes or fails. If the assertion is true, the test passes silently; if false, PyTest reports a failure with details.
This simple example shows how assert is easy to write and read, making PyTest tests clear and effective.
Now add data-driven testing with 3 different pairs of numbers and their expected sums
assert as its core mechanism for testing?assert in PyTestassert checks if a condition is true and stops the test if not, showing an error.assertassert because it is easy to write and gives clear, helpful error messages when tests fail.assert is simple and shows clear error messages -> Option Bx equals 10 using PyTest's core mechanism?assert x == 10. Single equals = is assignment, not comparison.def test_example():
x = 5
y = 10
assert x + y == 15
assert y - x == 4x + y == 15 means 5 + 10 == 15, which is true, so the first assert passes.y - x == 4 means 10 - 5 == 4, which is false (10 - 5 = 5), so the second assert fails and stops the test.def test_check():
value = 7
assert value = 7= which is assignment, not comparison. This causes a syntax error.nums contains only positive numbers. Which assert statement best uses PyTest's core mechanism to do this?nums is greater than zero (positive).all() with a generator to check all numbers are positive, which is correct. assert nums > 0 compares list to 0, invalid. assert len(nums) > 0 checks list length, not positivity. assert nums == [n for n in nums if n > 0] compares list to filtered list, which passes if all positive but less clear.Pass the test and this pattern moves unknown