Introducere în testarea în Python
Alexander Levin
Data Scientist
Probleme frecvente:
Aceste probleme pot genera costuri semnificative de remediere.
Testarea ajută la:
Este necesară pentru a verifica îndeplinirea cerințelor specificate
Testul - o procedură de verificare a corectitudinii unei aplicații sau sistem software
assertGândiți-vă la avioane:
Toate cele de mai sus sunt teste! Și avem nevoie de ele pentru siguranță.

assert condition - testează dacă condition este True.condition este False, Python generează un AssertionError.pytest - un framework popular de testare în Python, care oferă o modalitate simplă de a scrie teste.
Exemplu de test "assert" scris cu pytest în Python:
import pytest
# A function to test
def squared(number):
return number * number
# A test function always starts with "test"
def test_squared():
assert squared(-2) == squared(2)
with# Writing to file example
with open("hello_world.txt", 'w') as hello_file:
hello_file.write("Hello world \n")
pytest.raises - utilizat când testul ar trebui să genereze o Exception
import pytest
# A function to test
def division(a, b):
return a / b
# A test function
def test_raises():
with pytest.raises(ZeroDivisionError):
division(a=25, b=0)
Testarea este:
Implementarea testelor:
pytest - framework Python puternic, care simplifică procesul de testareassert - cuvânt cheie Python utilizat în pytest pentru teste de bazăpytest.raises - manager de context pentru teste ce generează ExceptionIntroducere în testarea în Python