Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| modul:m450:learningunits:lu04:loesungen:xfail [2026/11/17 08:24] – Lösung zu LU04.A03 erstellt admin | modul:m450:learningunits:lu04:loesungen:xfail [2026/11/17 18:10] (aktuell) – Kompetenz-Tags ergänzt admin | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| ====== LU04.L03 - Erwartete Fehler ====== | ====== LU04.L03 - Erwartete Fehler ====== | ||
| - | Lösung zu [[..: | + | Lösung zu [[..: |
| - | ===== Teil 1 und 2: Tests mit xfail-Markierungen | + | ===== test_cart.py |
| - | So sieht '' | + | {{gh> |
| - | <code python> | + | ===== cart.py ===== |
| - | """ | + | {{gh> |
| - | import pytest | + | |
| - | from cart import ShoppingCart | + | ===== Zwischenstand nach Teil 2 ===== |
| - | + | Solange | |
| - | + | ||
| - | @pytest.fixture(name=' | + | |
| - | def fixture_cart(): | + | |
| - | """ | + | |
| - | return ShoppingCart() | + | |
| - | + | ||
| - | + | ||
| - | # Teil 1: Die Methoden add_item und total sind fertig programmiert. | + | |
| - | + | ||
| - | def test_add_item(cart): | + | |
| - | """ | + | |
| - | cart.add_item(' | + | |
| - | assert cart.items[' | + | |
| - | + | ||
| - | + | ||
| - | def test_add_item_twice(cart): | + | |
| - | """ | + | |
| - | cart.add_item(' | + | |
| - | cart.add_item(' | + | |
| - | assert len(cart.items) | + | |
| - | | + | |
| - | + | ||
| - | + | ||
| - | def test_total(cart): | + | |
| - | """ | + | |
| - | cart.add_item('Apfel', 0.80, 3) | + | |
| - | cart.add_item('Brot', | + | |
| - | assert cart.total() == pytest.approx(6.75) | + | |
| - | + | ||
| - | + | ||
| - | def test_add_item_negative_price(cart): | + | |
| - | """ | + | |
| - | with pytest.raises(ValueError): | + | |
| - | cart.add_item(' | + | |
| - | + | ||
| - | + | ||
| - | # Teil 2: remove_item und apply_discount sind erst Stubs. | + | |
| + | <code python> | ||
| @pytest.mark.xfail(raises=NotImplementedError, | @pytest.mark.xfail(raises=NotImplementedError, | ||
| def test_remove_item(cart): | def test_remove_item(cart): | ||
| - | | + | ... |
| - | cart.add_item(' | + | |
| - | cart.add_item(' | + | |
| - | cart.remove_item(' | + | |
| - | assert list(cart.items) == [' | + | |
| - | + | ||
| - | + | ||
| - | @pytest.mark.xfail(raises=NotImplementedError, | + | |
| - | def test_remove_unknown_item(cart): | + | |
| - | """ | + | |
| - | with pytest.raises(KeyError): | + | |
| - | cart.remove_item(' | + | |
| - | + | ||
| - | + | ||
| - | @pytest.mark.xfail(raises=NotImplementedError, | + | |
| - | def test_apply_discount(cart): | + | |
| - | """ | + | |
| - | cart.add_item(' | + | |
| - | cart.apply_discount(25) | + | |
| - | assert cart.total() == pytest.approx(60.00) | + | |
| - | + | ||
| - | + | ||
| - | @pytest.mark.xfail(raises=NotImplementedError, | + | |
| - | @pytest.mark.parametrize(' | + | |
| - | def test_apply_discount_invalid(cart, | + | |
| - | """ | + | |
| - | with pytest.raises(ValueError): | + | |
| - | cart.apply_discount(percent) | + | |
| </ | </ | ||
| Zeile 93: | Zeile 29: | ||
| </ | </ | ||
| - | ===== Teil 3: Stubs durch Code ersetzen | + | ===== Nach dem Programmieren der Stubs ===== |
| - | <code python> | + | |
| - | def remove_item(self, | + | |
| - | """ | + | |
| - | Entfernt einen Artikel vollständig aus dem Warenkorb. | + | |
| - | :param name: Bezeichnung des Artikels | + | |
| - | :raises KeyError: wenn der Artikel nicht im Warenkorb ist | + | |
| - | """ | + | |
| - | del self.items[name] | + | |
| - | + | ||
| - | def apply_discount(self, | + | |
| - | """ | + | |
| - | Gewährt einen Rabatt in Prozent auf das Total. | + | |
| - | :param percent: Rabatt in Prozent, erlaubt sind 0 bis 50 | + | |
| - | :raises ValueError: wenn der Rabatt kleiner als 0 oder grösser als 50 ist | + | |
| - | """ | + | |
| - | if percent < 0 or percent > 50: | + | |
| - | raise ValueError(' | + | |
| - | self.discount = percent | + | |
| - | </ | + | |
| Sobald die Methoden funktionieren, | Sobald die Methoden funktionieren, | ||
| Zeile 123: | Zeile 39: | ||
| </ | </ | ||
| - | Das ist gewollt: PyTest meldet, dass der Code jetzt fertig ist und die Markierung entfernt werden muss. Ohne '' | + | Das ist gewollt: PyTest meldet, dass der Code jetzt fertig ist und die Markierung entfernt werden muss. Ohne '' |
| - | + | ||
| - | Nach dem Entfernen aller '' | + | |
| - | + | ||
| - | < | + | |
| - | $ pytest | + | |
| - | test_cart.py ......... | + | |
| - | 9 passed | + | |
| - | </ | + | |
| ===== Erläuterungen ===== | ===== Erläuterungen ===== | ||
| Zeile 139: | Zeile 47: | ||
| ---- | ---- | ||
| - | {{tag> | + | {{tag> |
| [[https:// | [[https:// | ||