Question
In in this lab, we will utilize advanced features of the Python unittest framework (mocks) to develop automated integration tests. You will work on developing
In in this lab, we will utilize advanced features of the Python unittest framework (mocks) to develop automated integration tests. You will work on developing integration tests for a weather client and a simple Square class, which uses the Math utility class from Week 2. The WeatherClient class performs RESTful API calls to the open weather platform to obtain current and forecast weather conditions for a given zip code.
In your unit/integration tests, you need to use mocks and patching features of the Python unittest framework to create necessary fake dependencies and stubs. Unit/integration tests MUST provide 100% code coverage of the WeatherClient and Square classes.
Readings:
- Python unittestMocks
- Requests for Humans
- Open Weather API
Steps:
- Study attached python classes (weather_client.py and square.py) to get a sense of what these modules do. Pay careful attention to module dependencies.
- Study the different functions (units) in each class and design a set of test cases that would result in 100% statement coverage.
- Use the unittest framework to develop unit tests for each of the test cases identified in 2. Use mocks to inject necessary dependencies and provide stubs when needed.
- Run your unit tests using the unittest runner and make sure all unit tests pass.
- Use the coverage tool to generate a coverage report.
Deliverables:
- Python unit test code for both weather_client and square modules.
- Screenshot showing successful run of all unit tests.
- Coverage report.
weather_client.py:
import requests
import json
class WeatherClient:
API_BASE_URL = 'http://samples.openweathermap.org/data/2.5/weather'
APP_ID = 'appid=b6907d289e10d714a6e88b30761fae22'
def __init__(self, appid = APP_ID):
self.app_id = appid
def get_weather_by_zip(self, zip_code):
params = {'zip': zip_code,
'appid': self.app_id}
res = requests.get(url=WeatherClient.API_BASE_URL,
params=params)
if res.status_code != 200:
raise requests.HTTPError()
result_json = json.loads(res.content.decode())
return result_json['weather'][0]['description']
def get_15day_forecast_by_zip(self, zip_code):
if __name__ == '__main__':
weather_client = WeatherClient()
weather = weather_client.get_weather_by_zip(zip_code='60564')
print(weather)
square.py:
class Square:
def __init__(self, side, math):
self.side = side
self.math_util = math
def area(self):
return self.math_util.power(self.side, 2)
def circumference(self):
return self.math_util.add(2 * self.side, 2 * self.side)
math.py:
class Math:
def add(self, a: int, b: int):
'''calculates and returns a plus b'''
return a + b
def multiply(self, a: int, b: int):
'''calculates and returns a multiplied by b'''
return a * b
def divide(self, a: int, b: int):
''' calculates a divided by b, raises ValueError on division by Zero'''
if b == 0:
raise ValueError
return a/b
def is_even(self, a: int):
return a % 2 == 0
def power(self, a: int, b: int):
'''calculates a to the power of b'''
prod = 1
for i in range(abs(b)):
prod *= a
return prod
def is_prime(self, num: int):
'''finds out if num is a prime or not'''
if num < 0:
raise(ValueError())
if num == 0:
return False
if num <=3:
return True
for i in range(2, num):
if num % i == 0:
return False
return True
def factorial(self, n: int):
'''calculates n! = 1 * 2 * 3 * ... * n'''
prod = 1
for i in range(1, n+1):
prod *= i
return prod
def factors(self, number: int):
'''finds and returns list of factors for number'''
if number <= 0:
raise ValueError('number must be geater than zero')
if number == 1 :
return list(range(1, number+1))
factors = [1, number]
for factor in range(2, number):
if number % factor == 0:
factors.append(factor)
return factors
Note:
I have given code for all three mentioned programs. Please write unit tests for weather_client.py and square.py. we should use mock, patch and stubs where ever necessary and the code must have 100% coverage.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
To write the unit tests for the weatherclientpy and squarepy modules we will use the Python unittest framework and the mock library to create necessar...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started