AGhoul
AGhoul

Reputation: 1

Trouble Understanding Pytest

I'm working through the auditor version of CS50P and am a bit confused on how pytest works, specifically on the test_twttr exercise.

The main program is to remove any vowels from a string and the below code is intended to test it

I believe my code is set up properly for pytest to test my functions; however, when I run it I always get back a "no test ran" responses. Shouldn't this be printing out each function with a "no error" type of message if it is function properly? I also tried the course checker and a error on the first check.

Am i missing something with how this feature works?

from twttr import shorten

def main():
    upperV()
    lowerV()
    number()
    symbol()

def upperV():
    assert shorten("sAo") == "so"
    assert shorten("sEo") == "so"
    assert shorten("sIo") == "so"
    assert shorten("sOo") == "so"
    assert shorten("sUo") == "so"

def lowerV():
    assert shorten("sao") == "so"
    assert shorten("seo") == "so"
    assert shorten("sio") == "so"
    assert shorten("soo") == "so"
    assert shorten("suo") == "so"

def number():
    assert shorten("s1o") == "s1o"
    assert shorten("s0o") == "s0o"
    assert shorten("s2o") == "s2o"
    assert shorten("s4o") == "s4o"
    assert shorten("s5o") == "s5o"
def space():
    assert shorten("s o") == "s o"
    assert shorten("s o") == "s o"
    assert shorten("s o") == "s o"
    assert shorten("s o") == "s o"
    assert shorten("s o") == "s o"
def symbol():
    assert shorten("s@o") == "s@o"
    assert shorten("s#o") == "s#o"

if __name__ == "__main__":
    main()

response message from pytest

Upvotes: 0

Views: 283

Answers (1)

flakes
flakes

Reputation: 23634

Pytest doesn't run you test code like a script. It loads the content as a module and then looks for functions/methods that start with test_ to create a list of items it needs to test. And because it's not run as a script, you don't need to create any main method yourself, that's part of what's offered by the pytest framework itself.

eg.

from twttr import shorten


def test_upperV():
    assert shorten("sAo") == "so"
    assert shorten("sEo") == "so"
    assert shorten("sIo") == "so"
    assert shorten("sOo") == "so"
    assert shorten("sUo") == "so"

P.S pytest also has a number of handy tools like parametrize which can further simplify your test setups!

import pytest
from twttr import shorten

@pytest.mark.parametrize("given, expected", [
    # upperV
    ("sAo", "so"), ("sEo", "so"), ("sIo", "so"), ("sOo", "so"), ("sUo", "so"),
    # lowerV
    ("sao", "so"), ("seo", "so"), ("sio", "so"), ("soo", "so"), ("suo", "so"),
    # number
    ("s1o", "s1o"), ("s0o", "s0o"), ("s2o", "s2o"), ("s4o", "s4o"), ("s5o", "s5o"),
    # space
    ("s o", "s o"), ("s o", "s o"), ("s o", "s o"), ("s o", "s o"), ("s o", "s o"),
    # symbol
    ("s@o", "s@o"), ("s#o", "s#o"),
])
def test_shorten(given, expected):
    assert shorten(given) == expected

Upvotes: 1

Related Questions