These are a few exercises to get used to using mocking.
Take a list of array elements and group them.
[1, 2, 3, 4, 5]
[[1, 2], [3, 4], [5]]
Implement this using TDD and Mocks.
Consider the following example
import time
class ScoreTypeA:
def score(self):
result = 10
# Complex Calculation
time.sleep(100)
return result
class ScoreTypeB:
def score(self):
result = 11
# Complex Calculation
time.sleep(100)
return result
assert adder([ScoreTypeA(), ScoreTypeA(), ScoreTypeB()]) == 31
Write a function to add any object with a score function. Don't use the real ScoreTypeB
objects, as the complex calculation they perform takes several hours.
Requests is a python library to make HTTP requests. This is how you make a HTTP Request using it.
import requests
r = requests.get("https://example.com/")
print(r.json())
Write a script to work out your current external IP address. Mock out the request
Tips