Created
April 4, 2024 12:47
-
-
Save OmarIthawi/f92a452d3d193d6bc7c68a3eb7532491 to your computer and use it in GitHub Desktop.
Python testing tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Run with either: | |
- $ python testing_example.py | |
OR | |
- $ pytest -v testing_example.py | |
""" | |
def sum_numbers(a, b): | |
result = a + b | |
print(f'DEBUG: {a} + {b} = {result}') | |
return result | |
def test_sum_numbers_positive(): | |
""" | |
Something goes here. | |
""" | |
assert sum_numbers(1, 2) == 3 | |
def test_sum_numbers_zero(): | |
assert sum_numbers(0, 0) == 0 | |
def test_sum_numbers_one_with_zero(): | |
assert sum_numbers(1, 0) == 1 | |
def test_sum_numbers_flipped_one_with_zero(): | |
assert sum_numbers(0, 1) == 1 | |
if __name__ == '__main__': | |
# Runs if `pytest` isn't used | |
test_sum_numbers_positive() | |
test_sum_numbers_zero() | |
test_sum_numbers_one_with_zero() | |
test_sum_numbers_flipped_one_with_zero() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment