Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / Python

Modulo unittest

Organizza metodi di test in classi TestCase e li esegue in suite.

Versione: Python 3.14; repository image, production runtime not attested

Sintassi

unittest.TestSuite([case]).run(result)

Organizza metodi di test in classi TestCase e li esegue in suite.

TestResult distingue asserzioni fallite, errori inattesi e test saltati. Ogni metodo di test standard ha la propria istanza.

L’esempio esegue tre test con un’asserzione fallita e un’eccezione.

Esempi

import unittest
class Checks(unittest.TestCase):
    def test_ok(self):
        self.assertEqual(2 + 2, 4)
    def test_fail(self):
        self.assertEqual(1, 2)
    def test_error(self):
        raise ValueError("input")
suite = unittest.defaultTestLoader.loadTestsFromTestCase(Checks)
result = unittest.TestResult()
suite.run(result)
print(result.testsRun, len(result.failures), len(result.errors), result.wasSuccessful())

Risultato

3 1 1 False

L’esempio esegue tre test con un’asserzione fallita e un’eccezione.

Fonti