0

This is CS50XPython - Working 9 to 5 (pset7) - Running the test in the IDE works correctly. Looking up exit code 2, it states the program was interrupted by the user (check50?)

I have verified all my tests work, the code works, the import statements are correct, but I cannot get check50 to get past this error.

The results of running "pytest test_working.py" are below. As shown by the 5 dots, all tests passed.

test_working.py .....

Check50's output is...

Results for cs50/problems/2022/python/working generated by check50 v3.3.7
:) working.py and test_working.py exist
:) working.py does not import libraries other than sys and re
:) working.py converts "9 AM to 5 PM" to "09:00 to 17:00"
:) working.py converts "9:00 AM to 5:00 PM" to "09:00 to 17:00"
:) working.py converts "8 PM to 8 AM" to "20:00 to 08:00"
:) working.py converts "8:00 PM to 8:00 AM" to "20:00 to 08:00"
:) working.py converts "12 AM to 12 PM" to "00:00 to 12:00"
:) working.py converts "12:00 AM to 12:00 PM" to "00:00 to 12:00"
:) working.py raises ValueError when given "8:60 AM to 4:60 PM"
:) working.py raises ValueError when given "9AM to 5PM"
:) working.py raises ValueError when given "09:00 to 17:00"
:) working.py raises ValueError when given "9 AM - 5 PM"
:) working.py raises ValueError when given "10:7 AM - 5:1 PM"
:( correct working.py passes all test_working checks
    expected exit code 0, not 2

My test_working.py code is...

from working import convert, convert_minutes, convert_to_twenty_four_hour
import pytest

def test_invalid_format():
    with pytest.raises(ValueError):
        convert("9AM5PM")
    with pytest.raises(ValueError):
        convert("9AM 5PM")
    with pytest.raises(ValueError):
        convert("9 AM 5 PM")
    with pytest.raises(ValueError):
        convert("9 AM - 5 PM")

def test_invalid_hours():
    with pytest.raises(ValueError):
        convert("15 AM to 3 PM")
    with pytest.raises(ValueError):
        convert("9 AM to 5 pm")
    with pytest.raises(ValueError):
        convert("9 am to 5 pm")
    with pytest.raises(ValueError):
        convert("9 to 5")
    with pytest.raises(ValueError):
        convert("9 AM 5 PM")


def test_minutes():
    assert convert_minutes(0) == 0
    assert convert_minutes(3) == 3
    assert convert_minutes(59) == 59

def test_invalid_minutes():
    assert convert_minutes(None) == 0
    with pytest.raises(ValueError):
        convert_minutes(60)
    with pytest.raises(ValueError):
        convert_minutes(85)
    with pytest.raises(ValueError):
        convert_minutes(-1)

def test_twenty_four_hour():
    assert convert_to_twenty_four_hour(5, "PM") == 17
    assert convert_to_twenty_four_hour(12, "AM") == 0
    assert convert_to_twenty_four_hour(12, "PM") != 15

def test_valid_times():
    assert convert("10:30 PM to 8:50 AM") == "22:30 to 08:50"

1 Answer 1

0

While this is NOT the answer I would recommend, it works.

I created a new test_working.py Created a single test_convert() function Created tests for only the listed test in check50's output (all which passed with the prior/above test_working.py) Ran check50

Everything passed.

I kept my current test_working.py since it tests all the functions I used and many more edge cases, however, even though all it's tests pass, it fails check50.

4
  • I have the same problem, tried this, but still getting: :( correct working.py passes all test_working checks expected exit code 0, not 2 it could be a problem to import pytest within test_working?
    – Anarres
    Oct 12, 2022 at 11:50
  • the 'import pytest' shouldn't be the issue or running your tests locally (non-check50) would not work. If you want to post your test_working.py file's code, I will see if I see anything. Since it is just the test side of the code, it should not be an issue with posting a 'solution'.
    – tiredoftry
    Oct 13, 2022 at 12:41
  • The problem was that I imported two functions in test_working.py. With import working everything run. But it seemed to work for you (importing the diferent functions)... so I have no idea whats going but I am glad I got it through
    – Anarres
    Oct 14, 2022 at 13:05
  • Glad it is working. With import, I had to get use to doing 'from working import ..., ..., ...' rather than 'import ..., ..., ... from working'. I kept getting the syntax backwards when doing named imports.
    – tiredoftry
    Oct 14, 2022 at 15:16

You must log in to answer this question.

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