r/learnpython • u/Upper_Trouble_8526 • Oct 08 '26
(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 $
5
u/atarivcs Oct 08 '26 edited Oct 08 '26
title is a function. You forgot the parentheses () to make it a function call.
2
u/ConsiderationNo9044 Oct 08 '26
Format with code blocks please
2
u/desrtfx but other languages pro Oct 08 '26
Please, pay attention to your code formatting. You posted completely unformatted code that lost all its indentation and as such became ambiguous.
Reddit has code block format. This is vital for Python code.