|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
根据图片中的问题,我的答案如下,求大佬指导,
也麻烦各位路过的大佬能写两个不同的TDD,我想多学学,谢谢!
测试一:
import json
import numpy as np
def summarise_and_save (filename, arr):
output = {}
output['min'] = int(np.min(arr))
output['max'] = int(np.max(arr))
output['mean'] = int(np.mean(arr))
output['series'] = list(np.round(arr))
with open (filename,'w') as file:
json.dump(output,file,default=int)
def test1():
'''
Test that the function can save a NumPy array to a JSON file.
'''
arr = np.array([1,2,3,4,5])
filename = 'test1.json'
summarise_and_save(filename, arr)
with open (filename,'r') as file:
data = json.load(file)
assert data['min']==1,"the function can't save a NumPy array to a JSON file"
print(True)
assert data['max']==5,"the function can't save a NumPy array to a JSON file"
print(True)
assert data['mean']==3,"the function can't save a NumPy array to a JSON file"
print(True)
assert data['series']==[1,2,3,4,5],"the function can't save a NumPy array to a JSON file"
print(True)
test1()
测试二:
import json
import numpy as np
def summarise_and_save (filename, arr):
output = {}
output['min'] = round(np.min(arr),2)
output['max'] = round(np.max(arr),2)
output['mean'] = round(np.mean(arr),2)
output['series'] = list(np.round(arr,2))
with open (filename,'w') as file:
json.dump(output,file)
def test2():
'''
Test that the function get a non-Numpy array.
'''
arr_invalid = [1,2,3]
try:
summarise_and_save ('invalid_output.json', arr_invalid)
except:
print("It's a invalid array")
test2()
|
-
-
|