如何测试从API读取,更新Excel表并将文件上传到Dropbox的Python脚本
我有一个非常简单的Python脚本,如下所示。它所做的就是从API读取数据,将数据添加到Excel文件中,然后将Excel文件上传到Dropbox。我知道单位测试应该在结构上非常简单,并且仅测试一件事,而对其他任何东西没有依赖性(Excel,Dropbox)。我可以测试此脚本进行完整集成测试,写入Excel文件并检查它的唯一方法,而对于Dropbox文件夹来说是相同的?
def get_info(url):
response = session.get(url)
sold_units = json.loads(response.text)
return info
def populate_excel_file(info):
for unit in info:
last_update_date = parser.parse(unit['lastupdatedate'])
if now-timedelta(hours=24) <= last_update_date <= now:
new_used = unit['NewUsed']
make = unit['Make']
model = unit['Model']
model_year = unit['ModelYear']
dsrp = unit['DSRP']
row = sheet.max_row + 1
sheet.cell(column=1, row=row, value=unit['lastupdatedate'])
sheet.cell(column=2, row=row, value=make)
sheet.cell(column=3, row=row, value=model)
sheet.cell(column=4, row=row, value=model_year)
sheet.cell(column=5, row=row, value=new_used)
sheet.cell(column=6, row=row, value=dsrp)
workbook.save(filename='BookNEW.xlsx')
def upload_file(file_from, file_to):
dbx = dropbox.Dropbox(access_token)
f = open(file_from, 'rb')
dbx.files_upload(f.read(), file_to)
info = get_info("https://api.call")
populate_excel_file(info)
file_from = 'BookNEW.xlsx'
file_to = 'Book.xlsx'
upload_file(file_from, file_to)
I have a very simple python script as shown below. All it does is read data from an API, add data to an excel file, and then uploads that excel file to dropbox. I'm aware that unit tests are supposed to be very simple in structure and only test one thing, with no dependencies on anything else (excel, dropbox). Is the only way I can test this script to do full integration tests, writing to an excel file and checking it, and the same for a dropbox folder?
def get_info(url):
response = session.get(url)
sold_units = json.loads(response.text)
return info
def populate_excel_file(info):
for unit in info:
last_update_date = parser.parse(unit['lastupdatedate'])
if now-timedelta(hours=24) <= last_update_date <= now:
new_used = unit['NewUsed']
make = unit['Make']
model = unit['Model']
model_year = unit['ModelYear']
dsrp = unit['DSRP']
row = sheet.max_row + 1
sheet.cell(column=1, row=row, value=unit['lastupdatedate'])
sheet.cell(column=2, row=row, value=make)
sheet.cell(column=3, row=row, value=model)
sheet.cell(column=4, row=row, value=model_year)
sheet.cell(column=5, row=row, value=new_used)
sheet.cell(column=6, row=row, value=dsrp)
workbook.save(filename='BookNEW.xlsx')
def upload_file(file_from, file_to):
dbx = dropbox.Dropbox(access_token)
f = open(file_from, 'rb')
dbx.files_upload(f.read(), file_to)
info = get_info("https://api.call")
populate_excel_file(info)
file_from = 'BookNEW.xlsx'
file_to = 'Book.xlsx'
upload_file(file_from, file_to)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论