Interfața CLI

Introducere în testarea în Python

Alexander Levin

Data Scientist

Exemplu: cod

Test al operatorului de exponențiere:

# test_sqneg.py
import unittest
# Declaring the TestCase class
class TestSquared(unittest.TestCase):
    # Defining the test
    def test_negative(self):
        self.assertEqual((-3) ** 2, 9)

Comandă CLI:

python3 -m unittest test_sqneg.py

Rulează scriptul Python test_sqneg.py folosind modulul unittest.

Introducere în testarea în Python

Exemplu: ieșire

Comanda: python3 -m unittest test_sqneg.py

Ieșirea testului: ieșirea testului unittest test_sqneg.py

Introducere în testarea în Python

Argumentul cu cuvânt-cheie -k

unittest -k - rulează metodele și clasele de test care corespund unui șablon sau subșir

Comandă: python3 -m unittest -k "SomeStringOrPattern" test_script.py

Exemplu: python3 -m unittest -k "Squared" test_sqneg.py

Ieșire: ieșire cu un cuvânt-cheie

1 https://docs.python.org/3/library/unittest.html
Introducere în testarea în Python

Indicatorul Fail fast -f

unittest -f - oprește rularea la prima eroare sau eșec.

Comandă: python3 -m unittest -f test_script.py

Exemplu de utilizare: când toate testele sunt critice, precum verificarea unui avion înainte de zbor.

exemplu de test eșuat

1 https://docs.python.org/3/library/unittest.html
Introducere în testarea în Python

Indicatorul Catch -c

Indicatorul Catch unittest -c - permite întreruperea testului prin apăsarea "Ctrl - C".

  • Dacă "Ctrl - C"
    • este apăsat o dată, unittest așteaptă finalizarea testului curent și raportează rezultatele de până atunci.
    • este apăsat de două ori, unittest generează excepția KeyboardInterrupt.

Comandă: python3 -m unittest -c test_script.py

Exemplu de utilizare: la depanarea unei suite mari de teste

1 https://docs.python.org/3/library/unittest.html
Introducere în testarea în Python

Indicatorul Verbose -v

unittest -v - rulează testele cu mai multe detalii

Comandă: python3 -m unittest -v test_script.py.

Exemplu de utilizare: pentru depanare

Exemplu de ieșire: ieșirea unittest cu indicatorul verbose

1 https://docs.python.org/3/library/unittest.html
Introducere în testarea în Python

Rezumat

  • Comandă de bază fără argumente: python3 -m unittest test_script.py

  • Ieșire în unittest

  • Argument cu cuvânt-cheie: python3 -m unittest -k "SomeStringOrPattern" test_script.py

  • Indicatorul Fail fast: python3 -m unittest -f test_script.py

  • Indicatorul Catch: python3 -m unittest -c test_script.py

  • Indicatorul Verbose: python3 -m unittest -v test_script.py

Introducere în testarea în Python

Să exersăm!

Introducere în testarea în Python

Preparing Video For Download...