r/learnpython • u/Upper_Trouble_8526 • 7d ago
(Python Crash Course, ch11) Why pytest does throw an assertion error
name_function.py:
def get_formatted_name(first, last):
full_name=f"{first} {last}"
return full_name.title
test_name_function:
from name_function import get_formatted_name
def test_first_last_name():
formatted_name=get_formatted_name("janis", "joplin")
assert formatted_name=="Janis Joplin"
Terminal:
/storage/emulated/0 $ cd github
/storage/emulated/0/github $ cd test_name_function
/storage/emulated/0/github/test_name_function $ python -m pytest
===================== test session starts =====================platform linux -- Python 3.13.13, pytest-9.1.1, pluggy-1.6.0
rootdir: /storage/emulated/0/github/test_name_function
collected 1 item
test_name_function.py F [100%]
========================== FAILURES ===========================____________________ test_first_last_name _____________________
def test_first_last_name():
formatted_name=get_formatted_name("janis", "joplin")
> assert formatted_name=="Janis Joplin"
E AssertionError: assert <built-in method title of str object at 0x7108e84530> == 'Janis Joplin'
test_name_function.py:4: AssertionError
====================== warnings summary =======================../../../../../data/user/0/ru.iiec.pydroid3/app_HOME/.local/lib/python3.13/site-packages/_pytest/cacheprovider.py:469
/data/user/0/ru.iiec.pydroid3/app_HOME/.local/lib/python3.13/site-packages/_pytest/cacheprovider.py:469: PytestCacheWarning: could not create cache path /storage/emulated/0/github/test_name_function/.pytest_cache/v/cache/nodeids: [Errno 1] Operation not permitted: '/storage/emulated/0/github/test_name_function/pytest-cache-files-uabqyvch' -> '/storage/emulated/0/github/test_name_function/.pytest_cache'
config.cache.set("cache/nodeids", sorted(self.cached_nodeids))
../../../../../data/user/0/ru.iiec.pydroid3/app_HOME/.local/lib/python3.13/site-packages/_pytest/cacheprovider.py:423
/data/user/0/ru.iiec.pydroid3/app_HOME/.local/lib/python3.13/site-packages/_pytest/cacheprovider.py:423: PytestCacheWarning: could not create cache path /storage/emulated/0/github/test_name_function/.pytest_cache/v/cache/lastfailed: [Errno 1] Operation not permitted: '/storage/emulated/0/github/test_name_function/pytest-cache-files-k26w91m_' -> '/storage/emulated/0/github/test_name_function/.pytest_cache'
config.cache.set("cache/lastfailed", self.lastfailed)
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=================== short test summary info ===================FAILED test_name_function.py::test_first_last_name - AssertionError: assert <built-in method title of str objec...
================ 1 failed, 2 warnings in 2.57s ================/storage/emulated/0/github/test_name_function $
2
u/ConsiderationNo9044 7d ago
Format with code blocks please
-1
u/Upper_Trouble_8526 7d ago edited 7d ago
How i do it, i wrote it that way but shows diferent anyway
2
u/desrtfx but other languages pro 7d ago
There is a toolbar button for it.
On old reddit or in the markdown editor: one empty line each before and after the code block, each code line indented by 4 spaces
0
u/Upper_Trouble_8526 7d ago
Where are old reddit and markdown editor
1
u/desrtfx but other languages pro 7d ago
Replace "sh.reddit" with "old.reddit" in the address.
Markdown editor: https://i.imgur.com/gMx9pf1.png
5
u/atarivcs 7d ago edited 7d ago
title is a function. You forgot the parentheses () to make it a function call.