- 我通常通過打開另一個終端(客戶端)來測驗我的應用程式,然后從那里發送發布請求。
- 在那個視窗中,我將收到的結果與我預期的結果進行比較。
- 我怎樣才能自動化這個程序?(例如作為
Makefile目標check_app)。
這是一個最小的示例(server):
from flask import Flask, request, jsonify
from typing import Any, Dict, List, Final
RAISE: Final[int] = 33
app = Flask(__name__)
@app.route("/analyze", methods=["POST"])
def update_salary() -> List[Dict[str, Any]]:
return jsonify([
{
"name": entry["name"],
"salary": entry["salary"] RAISE
}
for entry in request.get_json()
if request.is_json
])
這是客戶
import json
import requests
from typing import Final
URL: Final[str] = "http://127.0.0.1:5000/analyze"
with open("input.json") as fl:
data = json.load(fl)
response = requests.post(URL, json=data)
for entry in response.json():
name = entry["name"]
new_salary = entry["salary"]
print(f"Hey {name} your new salary is {new_salary}")
我做的步驟是:
$ flask run # automagically runs wsgi.py in terminal 1
$ python main.py # sends post request in terminal 2
編輯 這是我基于評論的不成功嘗試:
.ONESHELL:
test:
@kill -9 $$(lsof -t -i tcp:5000)
@flask run &
@cd ../sender
@python main.py > received.txt
@diff received.txt expected.txt
@cd -
我得到錯誤:
Traceback (most recent call last):
File "/home/oren/.local/lib/python3.10/site-packages/urllib3/connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "/home/oren/.local/lib/python3.10/site-packages/urllib3/util/connection.py", line 95, in create_connection
raise err
File "/home/oren/.local/lib/python3.10/site-packages/urllib3/util/connection.py", line 85, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
uj5u.com熱心網友回復:
這是基于@Mark Setchell 評論的完整生成檔案:
.ONESHELL:
test:
@echo "[ - ] testing app"
@kill -9 $$(lsof -t -i tcp:5000)
@flask run 1>/dev/null 2>/dev/null &
@sleep 2
@cd ../sender
@python main.py > received.txt
@if cmp -s -- received.txt expected.txt; \
then echo "[ > ] PASS ?? "; else echo "[ - ] ERROR ??"; fi
@cd - 1>/dev/null 2>/dev/null
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/496735.html
上一篇:帶有錯誤/成功訊息的燒瓶重定向
