Question: Write a test for your Bitcoin program (from the API topic) that converts a number of Bitcoin to their value in US Dollars. Structure your
Write a test for your Bitcoin program (from the API topic) that converts a number of Bitcoin to their value in US Dollars.
Structure your program so you don't need to mock user input.
Structure your program so it's possible to isolate and then mock the API call.
Mock the API call by providing a mock JSON response. Assert that your program calculates the correct value in dollars.
import requests url = https://api.coindesk.com/v1/bpi/currentprice.json" def main(): bitcoin get_bitcoin_amount() dollars convert_bitcoin_to_dollars (bitcoin) display_results (bitcoin, dollars) def get_bitcoin_amount(): while True: try: if bitcoin - float(input('Enter the number of bitcoin: ')) if bitcoin > 0: return bitcoin else: print(" Please enter a number greater than e') except ValueError: def convert_bitcoin_to_dollars (bitcoin): rate_json = get_bitcoin_data() exchange_rate= extract_rate(rate_json) print('Enter a positive number.') bitcoin = exchange rate * bitcoin return bitcoin # this function will be mocked i def get bitcoin_data(): name main() return requests.get(url).json() def extract_rate(rate_json): return rate_json["bpi']['USD']['rate_float'] def display_results (bitcoin, dollars): print (f {bitcoin) bitcoin is equal to ${dollars}') main
Step by Step Solution
3.46 Rating (153 Votes )
There are 3 Steps involved in it
To write a test for your Bitcoin program you can use the unittest library in Python to create a test ... View full answer
Get step-by-step solutions from verified subject matter experts
