Skip to main content
Bumped by Community user
Bumped by Community user
add spoiler markup
Source Link
$ cat fuel.py
def main():
    fraction_str = input("Enter fraction, formatted as X/Y, of how much fuel is in the tank: ")
    percentage=convert(fraction_str)
    print(gauge(percentage))

def convert(fraction):
    (x,y) = fraction.split("/")
    x_int=int(x)
    y_int=int(y)
    if x_int > y_int:
        raise ValueError
    if y_int == 0:
        raise ZeroDivisionError
    percentage=round(x_int/y_int*100)
    return percentage

def gauge(percentage):
    if percentage <= 1:
      return "E"
    elif percentage >= 99:
      return "F"
    else:
      return f'{percentage}%'

if __name__ == "__main__":
    main()
$

The test program:

$ cat test_fuel.py
import pytest
from fuel import convert
from fuel import gauge

def test_convert():
    assert convert("1/100") == 1
    assert convert("2/10") == 20
    assert convert("0/10") == 0
    with pytest.raises(ValueError):
      convert("banana/apple")


def test_gauge():
    assert gauge(1) == "E"
    assert gauge(100) == "F"
    assert gauge(22) == "22%"

!$ cat fuel.py !def main(): ! fraction_str = input("Enter fraction, formatted as X/Y, of how much fuel is in the tank: ") ! percentage=convert(fraction_str) ! print(gauge(percentage)) ! !def convert(fraction): ! (x,y) = fraction.split("/") ! x_int=int(x) ! y_int=int(y) ! if x_int > y_int: ! raise ValueError ! if y_int == 0: ! raise ZeroDivisionError ! percentage=round(x_int/y_int*100) ! return percentage ! !def gauge(percentage): ! if percentage <= 1: ! return "E" ! elif percentage >= 99: ! return "F" ! else: ! return f'{percentage}%' ! !if __name__ == "__main__": ! main() !$ ! The test program: !$ cat test_fuel.py !import pytest !from fuel import convert !from fuel import gauge ! !def test_convert(): ! assert convert("1/100") == 1 ! assert convert("2/10") == 20 ! assert convert("0/10") == 0 ! with pytest.raises(ValueError): ! convert("banana/apple") ! ! !def test_gauge(): ! assert gauge(1) == "E" ! assert gauge(100) == "F" ! assert gauge(22) == "22%" !

$ cat fuel.py
def main():
    fraction_str = input("Enter fraction, formatted as X/Y, of how much fuel is in the tank: ")
    percentage=convert(fraction_str)
    print(gauge(percentage))

def convert(fraction):
    (x,y) = fraction.split("/")
    x_int=int(x)
    y_int=int(y)
    if x_int > y_int:
        raise ValueError
    if y_int == 0:
        raise ZeroDivisionError
    percentage=round(x_int/y_int*100)
    return percentage

def gauge(percentage):
    if percentage <= 1:
      return "E"
    elif percentage >= 99:
      return "F"
    else:
      return f'{percentage}%'

if __name__ == "__main__":
    main()
$

The test program:

$ cat test_fuel.py
import pytest
from fuel import convert
from fuel import gauge

def test_convert():
    assert convert("1/100") == 1
    assert convert("2/10") == 20
    assert convert("0/10") == 0
    with pytest.raises(ValueError):
      convert("banana/apple")


def test_gauge():
    assert gauge(1) == "E"
    assert gauge(100) == "F"
    assert gauge(22) == "22%"

!$ cat fuel.py !def main(): ! fraction_str = input("Enter fraction, formatted as X/Y, of how much fuel is in the tank: ") ! percentage=convert(fraction_str) ! print(gauge(percentage)) ! !def convert(fraction): ! (x,y) = fraction.split("/") ! x_int=int(x) ! y_int=int(y) ! if x_int > y_int: ! raise ValueError ! if y_int == 0: ! raise ZeroDivisionError ! percentage=round(x_int/y_int*100) ! return percentage ! !def gauge(percentage): ! if percentage <= 1: ! return "E" ! elif percentage >= 99: ! return "F" ! else: ! return f'{percentage}%' ! !if __name__ == "__main__": ! main() !$ ! The test program: !$ cat test_fuel.py !import pytest !from fuel import convert !from fuel import gauge ! !def test_convert(): ! assert convert("1/100") == 1 ! assert convert("2/10") == 20 ! assert convert("0/10") == 0 ! with pytest.raises(ValueError): ! convert("banana/apple") ! ! !def test_gauge(): ! assert gauge(1) == "E" ! assert gauge(100) == "F" ! assert gauge(22) == "22%" !

Source Link

cs50p check50 says my program doesn't pass all checks, but pytest says that it does

check50 says my program doesn't pass all checks:

$ check50 --local cs50/problems/2022/python/tests/fuel
Checking........
Results for cs50/problems/2022/python/tests/fuel generated by check50 vlocally installed, no version information available
:) test_fuel.py exist
:( correct fuel.py passes all test_fuel checks
    expected exit code 0, not 2

But when I run pytest, all checks pass:

$ pytest test_fuel.py
=================================================================== test session starts ====================================================================
platform linux -- Python 3.10.9, pytest-7.2.1, pluggy-1.0.0
rootdir: /home/atsaloli/git/atsaloli/learn-python/cs50p-me50/test_fuel
collected 2 items

test_fuel.py ..                                                                                                                                      [100%]

==================================================================== 2 passed in 0.01s =====================================================================
$

What gives? Why the disconnect? What am I missing? How would I troubleshoot something like this?

The program I am testing:

$ cat fuel.py
def main():
    fraction_str = input("Enter fraction, formatted as X/Y, of how much fuel is in the tank: ")
    percentage=convert(fraction_str)
    print(gauge(percentage))

def convert(fraction):
    (x,y) = fraction.split("/")
    x_int=int(x)
    y_int=int(y)
    if x_int > y_int:
        raise ValueError
    if y_int == 0:
        raise ZeroDivisionError
    percentage=round(x_int/y_int*100)
    return percentage

def gauge(percentage):
    if percentage <= 1:
      return "E"
    elif percentage >= 99:
      return "F"
    else:
      return f'{percentage}%'

if __name__ == "__main__":
    main()
$

The test program:

$ cat test_fuel.py
import pytest
from fuel import convert
from fuel import gauge

def test_convert():
    assert convert("1/100") == 1
    assert convert("2/10") == 20
    assert convert("0/10") == 0
    with pytest.raises(ValueError):
      convert("banana/apple")


def test_gauge():
    assert gauge(1) == "E"
    assert gauge(100) == "F"
    assert gauge(22) == "22%"