Ahoj, bohuzel v angl ucebnici daji test kod a funkci do jednoho souboru a uz nerozeberou , co kam dat , kdyby test kod byl zvlast od testovane funkce. Mohl by mi nekdo u kodu napsat, co dat do test_souboru a co do souboru s funkci. Dekuji
Kod:
import pytest
class InvalidCharacterNameError(Exception): # tady ten kod prijde do test_souboru ?
pass
class InvalidClassNameError(Exception): # tady ten kod prijde do test_souboru ?
pass
class Character: # tady ten kod prijde do test_souboru ?
pass
VALID_CLASSES = ["sorcerer", "warrior"] # tady ten kod prijde do test_souboru nebo zustane s kodem funkce ?
def create_character(name: str, class_name: str) -> Character: # -> Character, co znamena ?
"""
Creates a new character and inserts it into the database.
:param name: the character name.
:param class_name: the character class name.
:raise InvalidCharacterNameError:
if the character name is empty.
:raise InvalidClassNameError:
if the class name is invalid.
:return: the newly created Character.
"""
if not name:
raise InvalidCharacterNameError("character name empty")
if class_name not in VALID_CLASSES:
msg = f'invalid class name: "{class_name}"'
raise InvalidCharacterNameError(msg)
...
def test_empty_name():
with pytest.raises(InvalidCharacterNameError):
create_character(name="", class_name="warrior")
def test_invalid_class_name():
with pytest.raises(InvalidClassNameError):
create_character(name="Solaire", class_name="mage")
def test_empty_name():
with pytest.raises(
InvalidCharacterNameError, match="character name empty"
):
create_character(name="", class_name="warrior")
def test_invalid_class_name():
with pytest.raises(
InvalidClassNameError, match='invalid class name: "mage"'
):
create_character(name="Solaire", class_name="mage")
Pytest, kod z ucebnice
- CZechBoY
- Master Level 9.5
- Příspěvky: 8813
- Registrován: srpen 08
- Bydliště: Brno
- Pohlaví:
- Stav:
Offline
- Kontakt:
Re: Pytest, kod z ucebnice
PHP, Nette, MySQL, C#, TypeScript, Python
IntelliJ Idea, Docker, Opera browser, Linux Mint
iPhone XS
Raspberry PI 3 (KODI, Raspbian)
XBox One S, PS 4, nVidia GeForce NOW
IntelliJ Idea, Docker, Opera browser, Linux Mint
iPhone XS
Raspberry PI 3 (KODI, Raspbian)
XBox One S, PS 4, nVidia GeForce NOW
Zpět na “Programování a tvorba webu”
Kdo je online
Uživatelé prohlížející si toto fórum: Žádní registrovaní uživatelé a 2 hosti