Hack Bulgaria
github.com/HackBulgaria
&&
raise statement
def divide(x, y):
try:
result = x / y
except ZeroDivisionError:
print("division by zero!")
else:
print("result is", result)
finally:
print("executing finally clause")
def divide(x, y):
try:
result = x / y
return result
except ZeroDivisionError:
print("division by zero!")
finally:
print("executing finally clause")
def divide():
while True:
try:
x = int(input("Please enter x: "))
y = int(input("Please enter y: "))
result = x / y
return result
except ZeroDivisionError:
print("division by zero!")
except ValueError:
print("Please enter a valid number")
finally:
print("executing finally clause")
def divide(x, y):
assert y is not 0
assert type(y) is int
assert type(x) is int
return x / y
да знаем във всеки момент кое от кода работи и кое не
по-удобен начин за тестване на програмата
по-лесна поддръжка(промяна) на кода
по-надежден софтуер
... и още много причини
import unittest
from solution import gas_stations
class GasStationTests(unittest.TestCase):
def test_works_correctly(self):
self.assertEqual(gas_stations(320,
90,
[50, 80, 140, 180, 220, 290]),
[80, 140, 220, 290])
self.assertEqual(gas_stations(390,
80,
[70, 90, 140, 210, 240, 280, 350]),
[70, 140, 210, 280, 350])
def test_raise_value_error(self):
with self.assertRaises(ValueError):
gas_stations(320, -10, [50, 80, 140, 180, 220, 290])
if __name__ == '__main__':
unittest.main()
if __name__ == '__main__':
unittest.main()
slavyana@slavyana-Inspiron-3537:~/A/week1$ python3 tests_week01_dive_into_python.py
..........
----------------------------------------------------------------------
Ran 10 tests in 0.002s
OK
slavyana@slavyana-Inspiron-3537:~/A/week1$ watch python3 tests_week01_dive_into_python.py
Every 2,0s: python3 tests_week01_dive_into_python.py
-------------------------------------------------------------------
Ran 10 tests in 0.002s
OK
By Hack Bulgaria