Refueling
In a file called fuel.py, reimplement Fuel Gauge from Problem Set 3, restructuring your code per the below, wherein:
convertexpects astrinX/Yformat as input, wherein each ofXandYis a positive integer, and returns that fraction as a percentage rounded to the nearestintbetween0and100, inclusive. IfXand/orYis not an integer, or ifXis greater thanY, thenconvertshould raise aValueError. IfYis0, thenconvertshould raise aZeroDivisionError.gaugeexpects anintand returns astrthat is:"E"if thatintis less than or equal to1,"F"if thatintis greater than or equal to99,- and
"Z%"otherwise, whereinZis that sameint.
def main():
...
def convert(fraction):
...
def gauge(percentage):
...
if __name__ == "__main__":
main()
Then, in a file called test_fuel.py, implement two or more functions that collectively test your implementations of convert and gauge thoroughly, each of whose names should begin with test_ so that you can execute your tests with:
pytest test_fuel.py
Hints
- Be sure to include
import fuelor
from fuel import convert, gaugeatop
test_fuel.pyso that you can callconvertandgaugein your tests. - Take care to
return, notprint, anintinconvertand astringauge. Onlymainshould callprint. - Note that you can raise an exception like
ValueErrorwith code like:raise ValueError - Note that you can check with
pytestwhether a function has raised an exception, per docs.pytest.org/en/latest/how-to/assert.html#assertions-about-expected-exceptions.
Before You Begin
Log into cs50.dev, click on your terminal window, and execute cd by itself. You should find that your terminal window’s prompt resembles the below:
$
Next execute
mkdir test_fuel
to make a folder called test_fuel in your codespace.
Then execute
cd test_fuel
to change directories into that folder. You should now see your terminal prompt as test_fuel/ $. You can now execute
code test_fuel.py
to make a file called test_fuel.py where you’ll write your tests.
How to Test
To test your tests, run pytest test_fuel.py. Be sure you have a copy of a fuel.py file in the same folder. Try to use correct and incorrect versions of fuel.py to determine how well your tests spot errors:
- Ensure you have a correct version of
fuel.py. Run your tests by executingpytest test_fuel.py.pytestshould show that all of your tests have passed. - Modify the correct version of
fuel.py, changing the return values ofconvert. Your program might, for example, mistakenly return astrinstead of anint. Run your tests by executingpytest test_fuel.py.pytestshould show that at least one of your tests has failed. - Similarly, modify the correct version of
fuel.py, changing the return values ofgauge. Your program might, for example, mistakenly omit a%in the resultingstr. Run your tests by executingpytest test_fuel.py.pytestshould show that at least one of your tests has failed.
You can execute the below to check your tests using check50, a program CS50 will use to test your code when you submit. (Now there are tests to test your tests!). Be sure to test your tests yourself and determine which tests are needed to ensure fuel.py is checked thoroughly.
check50 cs50/problems/2022/python/tests/fuel
Green smilies mean your program has passed a test! Red frownies will indicate your program output something unexpected. Visit the URL that check50 outputs to see the input check50 handed to your program, what output it expected, and what output your program actually gave.
How to Submit
In your terminal, execute the below to submit your work.
submit50 cs50/problems/2022/python/tests/fuel