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
Moderátor: Mods_senior
- CZechBoY
- Master Level 9.5

- Příspěvky: 8813
- Registrován: 20 srp 2008 14:02
- Bydliště: Brno
- Kontaktovat uživatele:
Re: Pytest, kod z ucebnice
Kód: Vybrat vše
import pytest
from aplikace import InvalidCharacterNameError, InvalidClassNameError, Character, create_character
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")Kód: Vybrat vše
class InvalidCharacterNameError(Exception):
pass
class InvalidClassNameError(Exception):
pass
class Character:
pass
VALID_CLASSES = ["sorcerer", "warrior"]
def create_character(name: str, class_name: str) -> Character: # -> Character, znamená, že se vrací typ Character
"""
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)
...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

