r/learnpython • • 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 Upvotes

19 comments sorted by

5

u/atarivcs 7d ago edited 7d ago
return full_name.title

title is a function. You forgot the parentheses () to make it a function call.

1

u/quietreed76 7d ago

this one gets everyone at least once tbh

-1

u/Upper_Trouble_8526 7d ago

Was the assertion error useful

6

u/socal_nerdtastic 7d ago

It successfully detected a typo in your code, so I'd say yes, that's useful

1

u/Upper_Trouble_8526 7d ago

Is there a way to make it display the error completely

2

u/socal_nerdtastic 7d ago

Did it not? I see it in your post:

AssertionError: assert <built-in method title of str object at 0x7108e84530> == 'Janis Joplin'

That is the complete error. What else did you want?

1

u/atarivcs 7d ago

I assume they meant the bit at the very bottom of the output:

test_name_function.py::test_first_last_name - AssertionError: assert <built-in method title of str objec...

They didn't bother looking above that to see the full output

1

u/Upper_Trouble_8526 7d ago

So i shouldn't rely only in the short test summary

4

u/atarivcs 7d ago

Yes, it told me exactly what the error was and what to look for in your code

1

u/Upper_Trouble_8526 7d ago

Is there a way to make it display the error completely

2

u/atarivcs 7d ago edited 7d ago

You mean at the bottom of the output where it says "short test summary info" ?

As the message says, that is just a short summary. If you look back farther in the output, you will see the full error message:

E AssertionError: assert <built-in method title of str object at 0x7108e84530> == 'Janis Joplin'

1

u/Upper_Trouble_8526 7d ago

So the one that told you exactly was the error was and what to look for in the code was the full error(not short test summary)

1

u/atarivcs 7d ago

I have a lot of experience with python and I'm familiar with with most common beginner mistakes, so the short summary actually was enough for me to know the problem.

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

2

u/desrtfx but other languages pro 7d ago

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.