Home > Enterprise >  Unable to assert length of list with Pytest
Unable to assert length of list with Pytest

Time:07-04

I am learning Python and I'm using Pytest to check my code as I learn. Here is some sample code I have running:

str = "I love pizza"
str_list = list(str)
print(str_list)
print(len(str_list))

With expected result printed to stdout:

['I', ' ', 'l', 'o', 'v', 'e', ' ', 'p', 'i', 'z', 'z', 'a']
12

But if I run this test:

def create_list_from_string():
    str = "I love pizza"
    str_list = list(str)
    assert 123 == len(str_list)

I cannot get the assert to fail. I have other tests in the file that pass when expected and fail if I purposely edit them to make them fail. So I think I have Pytest set up correctly. I know Python uses indentation for code blocks, and I verified all the indentations are 4 spaces and there's no trailing tabs or spaces. I also know that assert is not broken and I'm making some kind of newbie mistake. Thanks!

CodePudding user response:

Try making method name and test file starts with test_

  • Related