2024-06-25 12:18:11 +02:00
|
|
|
import pytest
|
2024-06-27 00:05:42 +00:00
|
|
|
from werkzeug.exceptions import InternalServerError
|
|
|
|
|
from werkzeug.exceptions import NotFound
|
|
|
|
|
|
|
|
|
|
from flask import branch_coverage
|
|
|
|
|
from flask import Flask
|
|
|
|
|
from flask import json
|
|
|
|
|
from flask import track_coverage
|
|
|
|
|
|
2024-06-25 12:18:11 +02:00
|
|
|
|
|
|
|
|
def create_app():
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
2024-06-27 00:05:42 +00:00
|
|
|
@app.route("/success")
|
2024-06-25 12:18:11 +02:00
|
|
|
def success():
|
2024-06-27 00:05:42 +00:00
|
|
|
track_coverage("dispatch_request_success")
|
|
|
|
|
return "Success", 200
|
2024-06-25 12:18:11 +02:00
|
|
|
|
2024-06-27 00:05:42 +00:00
|
|
|
@app.route("/error")
|
2024-06-25 12:18:11 +02:00
|
|
|
def error():
|
2024-06-27 00:05:42 +00:00
|
|
|
track_coverage("dispatch_request_error")
|
|
|
|
|
raise InternalServerError(description="Error occurred")
|
2024-06-25 12:18:11 +02:00
|
|
|
|
2024-06-27 00:05:42 +00:00
|
|
|
@app.route("/not_found")
|
2024-06-25 12:18:11 +02:00
|
|
|
def not_found():
|
2024-06-27 00:05:42 +00:00
|
|
|
track_coverage("dispatch_request_not_found")
|
|
|
|
|
raise NotFound(description="This is a 404")
|
2024-06-25 12:18:11 +02:00
|
|
|
|
|
|
|
|
return app
|
|
|
|
|
|
2024-06-27 00:05:42 +00:00
|
|
|
|
2024-06-25 12:18:11 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def client():
|
|
|
|
|
app = create_app()
|
2024-06-27 00:05:42 +00:00
|
|
|
app.config["TESTING"] = True
|
2024-06-25 12:18:11 +02:00
|
|
|
with app.test_client() as client:
|
|
|
|
|
yield client
|
|
|
|
|
|
2024-06-27 00:05:42 +00:00
|
|
|
|
2024-06-25 12:18:11 +02:00
|
|
|
def test_success_path(client):
|
2024-06-27 00:05:42 +00:00
|
|
|
response = client.get("/success")
|
2024-06-25 12:18:11 +02:00
|
|
|
assert response.status_code == 200
|
2024-06-27 00:05:42 +00:00
|
|
|
assert response.data == b"Success"
|
|
|
|
|
assert branch_coverage["dispatch_request_success"]
|
|
|
|
|
|
2024-06-25 12:18:11 +02:00
|
|
|
|
|
|
|
|
def test_error_handling(client):
|
2024-06-27 00:05:42 +00:00
|
|
|
response = client.get("/error")
|
2024-06-25 12:18:11 +02:00
|
|
|
assert response.status_code == 500
|
2024-06-27 00:05:42 +00:00
|
|
|
assert "Error occurred" in response.get_data(as_text=True)
|
|
|
|
|
assert branch_coverage["dispatch_request_error"]
|
|
|
|
|
|
2024-06-25 12:18:11 +02:00
|
|
|
|
|
|
|
|
def test_not_found_handling(client):
|
2024-06-27 00:05:42 +00:00
|
|
|
response = client.get("/not_found")
|
2024-06-25 12:18:11 +02:00
|
|
|
assert response.status_code == 404
|
2024-06-27 00:05:42 +00:00
|
|
|
assert "This is a 404" in response.get_data(as_text=True)
|
|
|
|
|
assert branch_coverage["dispatch_request_not_found"]
|
|
|
|
|
|
2024-06-25 12:18:11 +02:00
|
|
|
|
|
|
|
|
def test_no_route(client):
|
2024-06-27 00:05:42 +00:00
|
|
|
response = client.get("/no_route")
|
2024-06-25 12:18:11 +02:00
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
2024-06-26 14:44:52 +02:00
|
|
|
|
2024-06-27 00:05:42 +00:00
|
|
|
def save_coverage_to_json(file_path="coverage_result.json"):
|
|
|
|
|
with open(file_path, "w") as json_file:
|
|
|
|
|
json.dump(branch_coverage, json_file, indent=4)
|