Pytest, kod z ucebnice

Místo pro dotazy a rady ohledně programovacích jazyků (C++, C#, PHP, ASP, Javascript, VBS..) a tvorby webových stránek

Moderátor: Mods_senior

shockwave
Level 3
Level 3
Příspěvky: 459
Registrován: únor 11
Pohlaví: Muž
Stav:
Offline

Pytest, kod z ucebnice

Příspěvekod shockwave » 04 kvě 2020 11:40

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")

Reklama
Uživatelský avatar
CZechBoY
Master Level 9.5
Master Level 9.5
Příspěvky: 8813
Registrován: srpen 08
Bydliště: Brno
Pohlaví: Muž
Stav:
Offline
Kontakt:

Re: Pytest, kod z ucebnice

Příspěvekod CZechBoY » 04 kvě 2020 17:00

  1. import pytest
  2. from aplikace import InvalidCharacterNameError, InvalidClassNameError, Character, create_character
  3.  
  4.  
  5. def test_empty_name():
  6.     with pytest.raises(InvalidCharacterNameError):
  7.         create_character(name="", class_name="warrior")
  8.  
  9.  
  10. def test_invalid_class_name():
  11.     with pytest.raises(InvalidClassNameError):
  12.         create_character(name="Solaire", class_name="mage")
  13.  
  14.  
  15. def test_empty_name():
  16.     with pytest.raises(
  17.         InvalidCharacterNameError, match="character name empty"
  18.     ):
  19.         create_character(name="", class_name="warrior")
  20.  
  21.  
  22. def test_invalid_class_name():
  23.     with pytest.raises(
  24.         InvalidClassNameError, match='invalid class name: "mage"'
  25.     ):
  26.         create_character(name="Solaire", class_name="mage")



  1. class InvalidCharacterNameError(Exception):
  2.     pass
  3.  
  4.  
  5. class InvalidClassNameError(Exception):
  6.     pass
  7.  
  8.  
  9. class Character:
  10.     pass
  11.  
  12.  
  13. VALID_CLASSES = ["sorcerer", "warrior"]
  14.  
  15.  
  16. def create_character(name: str, class_name: str) -> Character: # -> Character, znamená, že se vrací typ Character
  17. """
  18. Creates a new character and inserts it into the database.
  19.  
  20. :param name: the character name.
  21. :param class_name: the character class name.
  22. :raise InvalidCharacterNameError: if the character name is empty.
  23. :raise InvalidClassNameError: if the class name is invalid.
  24. :return: the newly created Character.
  25. """
  26. if not name:
  27.     raise InvalidCharacterNameError("character name empty")
  28.  
  29. if class_name not in VALID_CLASSES:
  30.     msg = f'invalid class name: "{class_name}"'
  31.     raise InvalidCharacterNameError(msg)
  32. ...
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


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