Junior — Middle
In which cases should a unit test be terminated with an error?
sobes.tech AI
Answer from AI
A modular test should be marked as failed when the functionality being tested does not meet the expected result. This means that the test has detected a bug or incorrect behavior in the code.
For example, if a function is supposed to return the sum of two numbers but returns a different value, the test should fail.
In Python, using unittest or pytest, this happens automatically if an assert fails:
def test_sum():
result = sum([1, 2, 3])
assert result == 6 # if result != 6, the test will fail
You can manually fail a test using exceptions or assert when conditions are not met. This helps identify and fix issues early in the development process.