3

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!

Cardstdani
  • 4,999
  • 3
  • 12
  • 31
joeg3
  • 185
  • 1
  • 2
  • 7
  • 1
    How are you running the test? maybe it's pytest not finding any test because the name does not comply to its standard. [pytest best practices](https://docs.pytest.org/en/7.1.x/explanation/goodpractices.html#conventions-for-python-test-discovery) – EDG956 Jul 03 '22 at 13:27
  • Can you should the command you're running and its output? also, you shouldn't use built-in function names as variables (`str`, `list`, `dict`, etc, are not good variable names in Python) – EDG956 Jul 03 '22 at 13:29
  • This question is a duplicate of https://stackoverflow.com/questions/5142418/what-is-the-use-of-assert-in-python – Cardstdani Jul 03 '22 at 13:50
  • Does this answer your question? [What is the use of "assert" in Python?](https://stackoverflow.com/questions/5142418/what-is-the-use-of-assert-in-python) – Cardstdani Jul 03 '22 at 13:51

1 Answers1

6

Try making method name and test file starts with test_

user10869670
  • 106
  • 6