CLI-interface

Introductie tot testen in Python

Alexander Levin

Data Scientist

Voorbeeld: code

Test van de machtsoperator:

# 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)

CLI-commando:

python3 -m unittest test_sqneg.py

Voer het Python-script test_sqneg.py uit met de module unittest.

Introductie tot testen in Python

Voorbeeld: output

Het commando: python3 -m unittest test_sqneg.py

De testuitvoer: uitvoer van unittest test_sqneg.py

Introductie tot testen in Python

Keyword-argument -k

unittest -k — voer testmethoden en -klassen uit die overeenkomen met het patroon of een substring

Commando: python3 -m unittest -k "SomeStringOrPattern" test_script.py

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

Uitvoer: output met een keyword

1 https://docs.python.org/3/library/unittest.html
Introductie tot testen in Python

Fail-fast vlag -f

unittest -f — stop de testrun bij de eerste fout of failure.

Commando: python3 -m unittest -f test_script.py

Use case: wanneer alle tests cruciaal zijn, zoals het testen van een vliegtuig vóór de vlucht.

voorbeeld van mislukte test

1 https://docs.python.org/3/library/unittest.html
Introductie tot testen in Python

Catch-vlag -c

Catch-vlag unittest -c — hiermee kun je tests onderbreken met "Ctrl-C".

  • Als je "Ctrl-C" eenmaal indrukt, wacht unittest tot de huidige test klaar is en rapporteert de resultaten tot nu toe.
  • Bij twee keer drukken gooit unittest de uitzondering KeyboardInterrupt. Commando: python3 -m unittest -c test_script.py

Use case: debuggen van een grote test suite

1 https://docs.python.org/3/library/unittest.html
Introductie tot testen in Python

Verbose-vlag -v

unittest -v — voer tests uit met meer detail

Commando: python3 -m unittest -v test_script.py.

Use case: voor debuggen

Voorbeeldoutput: output unittest met verbose-vlag

1 https://docs.python.org/3/library/unittest.html
Introductie tot testen in Python

Samenvatting

  • Basiscommando zonder argumenten: python3 -m unittest test_script.py

  • Uitvoer in unittest

  • Keyword-argument: python3 -m unittest -k "SomeStringOrPattern" test_script.py

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

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

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

Introductie tot testen in Python

Laten we oefenen!

Introductie tot testen in Python

Preparing Video For Download...