1

I was following python programming course and with the unit test chapter I updated the main file and the test files as described in this problem set page. Now I'm sure that my tests are running fine as when I run pytest test_twttr.py it runs all the tests. I have also made sure that I'm not running any print command within the shorten method. However, when I run check50 command, it fails on the second step where it says it got exit code 1, instead of 0. and I have spent good amount of time to investigate where the problem lies, but couldn't found any so far. So if anyone could help, I would then be able to proceed with rest of the coursework. Thanks.

My test_twttr.py looks like this


from twttr import shorten

def test_replaces_vowels():
    assert shorten('Twitter') == 'Twttr'

def test_replaces_vowels_in_capital():
    assert shorten("WHAT's your name?") == "WHT's yr nm?"

def test_replaces_vowels_in_lowercase():
    assert shorten('able') == 'bl'

def test_omits_uppercase():
    assert shorten('CS50') == 'CS50'

def test_omits_numbers():
    assert shorten(123) == '123'

def test_omits_punctuations():
    assert shorten('a,d!') == ',d!'

and here is the code from twttr.py:

def main():
    userInput = input('Input: ')
    updatedInput = shorten(userInput)
    print('Output: ' + updatedInput)

# go through each letter and remove letter if it is vowel
def shorten(userData):
    userData = str(userData)
    updatedText = ''
    loopCount = 0
    letterCount = len(userData)
    while loopCount 
3
  • Are you referring to this test: correct twttr.py passes all test_twttr checks? If, so this test is running your test_twttr.py against their twttr.py that is known to be correct. When it does, something goes wrong in your test_twttr.py. It's hard to give more help without seeing your test_twttr.py code. It should only be asserting a few test statements.
    – kcw78
    Commented Feb 6, 2023 at 13:50
  • Thanks @kcw78. I have this in my test_twttr.py. it passes all when I run pytest test_twttr.py: from twttr import shorten def test_replaces_vowels(): assert shorten('Twitter') == 'Twttr' def test_replaces_vowels_in_capital(): assert shorten("WHAT's your name?") == "WHT's yr nm?" def test_replaces_vowels_in_lowercase(): assert shorten('able') == 'bl' def test_omits_uppercase(): assert shorten('CS50') == 'CS50' def test_omits_numbers(): assert shorten(123) == '123' def test_omits_punctuations(): assert shorten('a,d!') == ',d!'
    – Tuhin
    Commented Feb 6, 2023 at 14:16
  • I've updated the question and added contents from the test_twttr.py and twttr.py. unfortunately it's not showing all the contents from my twttr.py file.
    – Tuhin
    Commented Feb 6, 2023 at 14:26

1 Answer 1

1

Look at the 5th test. You have:

def test_omits_numbers():
    assert shorten(123) == '123'

When you read the user's input, input() ALWAYS returns a string, so calling shorten(123) triggers an error in the correct version of twttr.py they use for testing. You can't replicate this error testing interactively because input() ALWAYS returns a string. You don't get an error when testing with pytest because your shorten() function converts the input to a string: userData = str(userData).

2
  • That's it, thanks a lot @kcw78, I was scratching my head for so long why it's not passing, as the pytest and interactively. I've updated the test and it's all passing now. Many thanks again.
    – Tuhin
    Commented Feb 6, 2023 at 16:12
  • Glad that helped. If this solved your problem, please check "Accepted Answer" to indicate this is the solution to future readers. Regards.
    – kcw78
    Commented Feb 8, 2023 at 22:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .