Verify PyTest can run a simple test and report results
Preconditions (2)
✅ Expected Result: PyTest runs the test, shows one test passed, and exits with success status
import pytest def test_addition(): assert 1 + 1 == 2 if __name__ == '__main__': pytest.main(['-v', 'test_sample.py'])
This code defines a simple test function test_addition that checks if 1 + 1 equals 2 using a plain assert. PyTest automatically detects functions starting with test_ as tests.
The pytest.main() call runs the test with verbose output, showing the test name and result. This simulates running pytest test_sample.py from the command line.
Using simple asserts and naming conventions makes tests easy to write and understand, which is why PyTest is popular. It also provides clear reports and requires minimal setup.
Now add data-driven testing with 3 different input pairs to verify addition
pytest considered popular among Python testing frameworks?pytest?test_ in lowercase.pytest test code?def test_example():
x = 5
y = 2
assert x - y == 35 - 2 == 3 is true, so the assertion passes.pytest test code:def test_sum():
total = sum([1, 2, 3])
assert total = 6pytest helps in extending its functionality easily and is a key reason for its popularity?Pass the test and this pattern moves unknown