import subprocess import sys import time import requests import os import pytest def start_server(port): env = os.environ.copy() env["PORT"] = str(port) # Start server as a subprocess using the same Python interpreter proc = subprocess.Popen( [sys.executable, "app.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, ) return proc def wait_until_up(url, timeout=10.0): start = time.time() while True: try: requests.post(url, json={"features": [200, 1, 0, 500]}, timeout=1.0) return True except Exception: if time.time() - start > timeout: return False time.sleep(0.2) @pytest.mark.integration def test_predict_integration(): port = 5001 url = f"http://127.0.0.1:{port}/predict" proc = start_server(port) try: assert wait_until_up(url), "Server did not start in time" resp = requests.post(url, json={"features": [200, 1, 0, 500]}) assert resp.status_code == 200 j = resp.json() assert "fraud" in j assert "probability" in j finally: proc.terminate() proc.wait(timeout=5)