Diferència entre revisions de la pàgina «Introducció al testing»

De binefa.com
Salta a la navegació Salta a la cerca
Línia 91: Línia 91:
 
   
 
   
 
  ============================== 4 passed in 0.03s ===============================
 
  ============================== 4 passed in 0.03s ===============================
 +
Forçant un error. Per exemple:
 +
assert multiplica(5, -2) == -11
 +
Sortiria pel terminal:
 +
  pytest 01_testing_pytest.py -v
 +
 +
============================= test session starts ==============================
 +
platform linux -- Python 3.9.2, pytest-8.3.5, pluggy-1.5.0 -- /usr/bin/python3
 +
cachedir: .pytest_cache
 +
rootdir: /media/emmagatzematge/curs2024-2025/dam/testing/codis
 +
plugins: anyio-3.3.2
 +
collected 4 items                                                             
 +
 +
01_testing_pytest.py::test_multiplica_positius PASSED                    [ 25%]
 +
01_testing_pytest.py::test_multiplica_negatius PASSED                    [ 50%]
 +
01_testing_pytest.py::test_multiplica_mixta FAILED                      [ 75%]
 +
01_testing_pytest.py::test_multiplica_zero PASSED                        [100%]
 +
 +
=================================== FAILURES ===================================
 +
____________________________ test_multiplica_mixta _____________________________
 +
 +
    def test_multiplica_mixta():
 +
>      assert multiplica(5, -2) == -11
 +
E      assert -10 == -11
 +
E        +  where -10 = multiplica(5, -2)
 +
 +
01_testing_pytest.py:15: AssertionError
 +
=========================== short test summary info ============================
 +
FAILED 01_testing_pytest.py::test_multiplica_mixta - assert -10 == -11
 +
========================= 1 failed, 3 passed in 0.10s ==========================

Revisió del 16:58, 31 març 2025

Primera presa de contacte

unittest

Codi:

# 00_testing_unittest.py
import unittest

# Funció que volem provar
def suma(a, b):
   return a + b

# Classe de tests
class TestSuma(unittest.TestCase):
   def test_suma_positius(self):
       self.assertEqual(suma(2, 3), 5)
   
   def test_suma_negatius(self):
       self.assertEqual(suma(-1, -1), -2)
   
   def test_suma_mixta(self):
       self.assertEqual(suma(5, -3), 2)
   
   def test_suma_zero(self):
       self.assertEqual(suma(0, 0), 0)

# Executar els tests si s'executa directament aquest fitxer
if __name__ == '__main__':
   unittest.main()

Execució de la prova:

python 00_testing_unittest.py 

Sortida pel terminal:

....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK

Forçant un error. Per exemple:

self.assertEqual(suma(-1, -1), -3)

La sortida al terminal seria:

.F..
======================================================================
FAIL: test_suma_negatius (__main__.TestSuma)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "/media/emmagatzematge/curs2024-2025/dam/testing/codis/00_testing_unittest.py", line 14, in test_suma_negatius
   self.assertEqual(suma(-1, -1), -3)
AssertionError: -2 != -3

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED (failures=1)

pytest

Heu d'instal·lar pytest

pip install pytest

Si sou a Linux cal afegir la ruta de pytest al PATH. Per exemple, afegint al final de l'arxiu .basrc de la carpeta d'usuari (canvieu jordi pel vostre usuari al sistema):

export PATH=$PATH:/home/jordi/.local/bin

Codi d'exemple:

# 01_testing_pytest.py

# Funció que volem provar
def multiplica(a, b):
   return a * b

# Tests amb pytest (només cal definir funcions que comencin per "test_")
def test_multiplica_positius():
   assert multiplica(3, 4) == 12

def test_multiplica_negatius():
   assert multiplica(-2, -3) == 6

def test_multiplica_mixta():
   assert multiplica(5, -2) == -10 

def test_multiplica_zero():
   assert multiplica(0, 100) == 0

Execució:

pytest 01_testing_pytest.py -v

Sortida pel terminal:

============================= test session starts ==============================
platform linux -- Python 3.9.2, pytest-8.3.5, pluggy-1.5.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /media/emmagatzematge/curs2024-2025/dam/testing/codis
plugins: anyio-3.3.2
collected 4 items                                                              

01_testing_pytest.py::test_multiplica_positius PASSED                    [ 25%]
01_testing_pytest.py::test_multiplica_negatius PASSED                    [ 50%]
01_testing_pytest.py::test_multiplica_mixta PASSED                       [ 75%]
01_testing_pytest.py::test_multiplica_zero PASSED                        [100%]

============================== 4 passed in 0.03s ===============================

Forçant un error. Per exemple:

assert multiplica(5, -2) == -11 

Sortiria pel terminal:

 pytest 01_testing_pytest.py -v
============================= test session starts ==============================
platform linux -- Python 3.9.2, pytest-8.3.5, pluggy-1.5.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /media/emmagatzematge/curs2024-2025/dam/testing/codis
plugins: anyio-3.3.2
collected 4 items                                                               

01_testing_pytest.py::test_multiplica_positius PASSED                    [ 25%]
01_testing_pytest.py::test_multiplica_negatius PASSED                    [ 50%]
01_testing_pytest.py::test_multiplica_mixta FAILED                       [ 75%]
01_testing_pytest.py::test_multiplica_zero PASSED                        [100%]

=================================== FAILURES ===================================
____________________________ test_multiplica_mixta _____________________________

   def test_multiplica_mixta():
>       assert multiplica(5, -2) == -11
E       assert -10 == -11
E        +  where -10 = multiplica(5, -2)

01_testing_pytest.py:15: AssertionError
=========================== short test summary info ============================
FAILED 01_testing_pytest.py::test_multiplica_mixta - assert -10 == -11
========================= 1 failed, 3 passed in 0.10s ==========================