This shows you the differences between two versions of the page.
tsc:laboratoare:laborator-05 [2024/03/26 20:52] dragos.sandulescu97 [Exerciții ] |
tsc:laboratoare:laborator-05 [2025/04/07 13:20] (current) giorgiana.vlasceanu [Exerciții ] |
||
---|---|---|---|
Line 75: | Line 75: | ||
=== Creare primului test === | === Creare primului test === | ||
- | Pentru ca pytest să recunoască un fișier de test, acesta trebuie să conțină prefixul **test_** (e.g. test_factorial.py). De asemenea, și metoda trebuie să conțină prefixul **test_** (e.g. def test_i_love_ic()). | + | Pentru ca pytest să recunoască un fișier de test, acesta trebuie să conțină prefixul **test_** (e.g. test_factorial.py). De asemenea, și metoda trebuie să conțină prefixul **test_** (e.g. def test_i_love_tsc()). |
<code python> | <code python> | ||
- | # content of test_IC.py | + | # content of test_TSC.py |
def increment(x): | def increment(x): | ||
Line 89: | Line 89: | ||
Pentru a rula pytest pentru fișierul anterior: | Pentru a rula pytest pentru fișierul anterior: | ||
<code teraterm> | <code teraterm> | ||
- | $ pytest test_IC.py | + | $ pytest test_TSC.py |
+ | $ pytest -v test_TSC.py -- Pentru o rulare detaliata a testelor - de preferat aceasta optiune | ||
</code> | </code> | ||
Line 102: | Line 103: | ||
$ pytest $NUME_FISIER.py::$NUME_TEST | $ pytest $NUME_FISIER.py::$NUME_TEST | ||
- | $ pytest test_IC.py::test_increment | + | $ pytest test_TESC.py::test_increment |
</code> | </code> | ||
Line 121: | Line 122: | ||
collected 1 item | collected 1 item | ||
- | test_IC.py . [100%] | + | test_TSC.py . [100%] |
================================================== 1 passed in 0.09s =================================================== | ================================================== 1 passed in 0.09s =================================================== | ||
Line 134: | Line 135: | ||
collected 1 item | collected 1 item | ||
- | test_IC.py F [100%] | + | test_TSC.py F [100%] |
======================================================= FAILURES ======================================================= | ======================================================= FAILURES ======================================================= | ||
Line 143: | Line 144: | ||
E + where 4 = increment(3) | E + where 4 = increment(3) | ||
- | test_IC.py:5: AssertionError | + | test_TSC.py:5: AssertionError |
=============================================== short test summary info ================================================ | =============================================== short test summary info ================================================ | ||
- | FAILED test_IC.py::test_increment - assert 4 == 5 | + | FAILED test_TSC.py::test_increment - assert 4 == 5 |
================================================== 1 failed in 0.15s =================================================== | ================================================== 1 failed in 0.15s =================================================== | ||
</code> | </code> | ||
Line 178: | Line 179: | ||
**5. in and not in ** | **5. in and not in ** | ||
<code python> | <code python> | ||
- | note_IC = [10, 4, 9, 9] | + | note_TSC = [10, 4, 9, 9] |
- | assert 10 in note_IC || assert 5 not in note_IC # Success Examples | + | assert 10 in note_TSC || assert 5 not in note_TSC # Success Examples |
- | assert 10 not in note_IC || assert 7 in note_IC # Fail Examples | + | assert 10 not in note_TSC || assert 742342342342 in note_TSC # Fail Examples |
</code> | </code> | ||
Line 191: | Line 192: | ||
**7. any() | all() ** | **7. any() | all() ** | ||
<code python> | <code python> | ||
- | note_IC = [10, 4, 9, 8, 7] | + | note_TSC = [10, 4, 9, 8, 7] |
- | trecut_IC = [True, False, True, True, True] | + | trecut_TSC = [True, False, True, True, True] |
- | assert any(note_IC) == True # Success Example => cel putin un element din lista este diferit de 0. | + | assert any(note_TSC) == True # Success Example => cel putin un element din lista este diferit de 0. |
- | assert any(trecut_IC) == True # Success Example => cel putin un element din lista este True. | + | assert any(trecut_TSC) == True # Success Example => cel putin un element din lista este True. |
- | assert all(note_IC) # Success Example => toate elementele din lista sunt diferite de 0. | + | assert all(note_TSC) # Success Example => toate elementele din lista sunt diferite de 0. |
- | assert all(trecut_IC) # Fail Example => nu toate elementele din lista sunt True. | + | assert all(trecut_TSC) # Fail Example => nu toate elementele din lista sunt True. |
</code> | </code> | ||
Line 298: | Line 299: | ||
@pytest.mark.parametrize("a, b, expected", [(1, 2, 4), (3, 4, 9)]) | @pytest.mark.parametrize("a, b, expected", [(1, 2, 4), (3, 4, 9)]) | ||
- | def test_i_wish_i_was_rich(a, b, expected): | + | def test_i_know_how_to_count(a, b, expected): |
assert 2 * a + b == expected | assert 2 * a + b == expected | ||
</code> | </code> | ||
Line 351: | Line 352: | ||
@pytest.fixture | @pytest.fixture | ||
def fixture_function(): | def fixture_function(): | ||
- | return "IC is love, IC is life" | + | return "TSC is love, TSC is life" |
def test_fixture(fixture_function): | def test_fixture(fixture_function): | ||
- | assert fixture_function == "IC is love, IC is life" | + | assert fixture_function == "TSC is love, TSC is life" |
</code> | </code> | ||
Line 422: | Line 423: | ||
- | ==== Exerciții - WIP - termin pana pe 28. Scuze :( ==== | + | ==== Exerciții ==== |
- | 0. Clonați repo-ul https://github.com/Ingineria-Calculatoarelor-ACS-UPB/unit-testing-1-lab | + | Pentru a clona [[https://github.com/cs-pub-ro/systems-testing | repo-ul]] și a accesa resursele aferente laboratorului: |
+ | |||
+ | <code bash> | ||
+ | student@tsc:~$ git clone git@github.com:cs-pub-ro/systems-testing.git | ||
+ | student@tsc:~$ cd systems-testing/laboratories | ||
+ | student@tsc:~/laboratories$ cd unit-testing-1 | ||
+ | </code> | ||
+ | |||
+ | Dacă aveți local [[https://github.com/cs-pub-ro/systems-testing | repo-ul]], asigurați-vă că aveți ultima versiune. | ||
+ | |||
+ | <code bash> | ||
+ | student@tsc:~$ cd systems-testing | ||
+ | student@tsc:~$ git pull | ||
+ | </code> | ||
- | 1. Factorial [**4.5p**]. | ||
- | * [**2p**] Creați un fisier de test pentru factorial.py. Creați un test care să verifice că funcția factorial întoarce output-ul corect pentru un input introdus de voi. | ||
- | * [**2.5p**] În fisierul creat anterior: | ||
- | * Scrieți un test care să primească mai multe input-uri consecutive pentru funcția factorial folosind pytest.mark.parametrize. | ||
- | * Scrieți un test care să valideze aruncarea unei excepții în cazul în care se introduce ca input o valoare incorectă. | ||
- | 2. Stores and Products [**5.5p**]. | + | Realizați o serie de teste care să acopere funcționalitatea codului și a edge-cases descoperite. Punctearea este influențată de acoperirea testelor realizate. |
- | * [**1p**] Scrieți un test care să verifice crearea corectă de instanțe pentru Product și Store. | + | |
- | * [**1p**] Scrieți o suită de teste(cel puțin 2) care să poată să fie rulată separat de restul testelor existente care să verifice add_product și remove_product din clasa Store folosind Markers. | + | |
- | * [**1.5p**] Din cauza unui bug, scrieți un test care să dea skip atunci când versiunea de python este mai mare de 3.7. | + | |
- | * [**1p**] Scrieți un test care se asteptă să eșueze atunci când un Product este creat fara expiration_date. Creați încă un test care se așteaptă să eșueze când se încearcă adăugare unui element invalid în Store (e.g. în loc să fie adăugat un Product, să fie adăugta un string). | + | |
- | * [**1p**] Creați un fisier separat în care să testați atât pe success, cât și pe fail, metodele de determinare a celui mai scump produs și a produsului în cea mai mare cantitate. | + | |