forked from automationExamples/pytest-api-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner_app.py
More file actions
109 lines (83 loc) · 2.92 KB
/
test_runner_app.py
File metadata and controls
109 lines (83 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from flask import Flask, render_template, jsonify, request, Response, send_from_directory
import json
import queue
import threading
import pytest
import sys
import os
import subprocess
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
app = Flask(__name__)
result_queue = queue.Queue()
is_running = False
class LiveTestPlugin:
def __init__(self, q):
self.q = q
def pytest_runtest_logreport(self, report):
if report.when == 'call' or (report.when == 'setup' and report.failed):
status = 'passed' if report.passed else 'failed' if report.failed else 'skipped'
self.q.put({'type': 'result', 'test': report.nodeid, 'status': status})
def pytest_sessionfinish(self, session, exitstatus):
self.q.put({'type': 'done'})
def run_pytest_thread(selected_tests, q):
global is_running
is_running = True
os.chdir(BASE_DIR)
plugin = LiveTestPlugin(q)
pytest.main(selected_tests + ['--html=report.html', '-v'], plugins=[plugin])
is_running = False
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/tests')
def get_tests():
result = subprocess.run(
[sys.executable, '-m', 'pytest', '--collect-only', '-q', '--no-header'],
capture_output=True, text=True, cwd=BASE_DIR
)
tests = [line.strip() for line in result.stdout.split('\n') if '::' in line.strip()]
return jsonify(tests)
@app.route('/api/run', methods=['POST'])
def run_tests():
global result_queue, is_running
if is_running:
return jsonify({'status': 'already running'}), 400
selected_tests = request.json.get('tests', [])
result_queue = queue.Queue()
threading.Thread(
target=run_pytest_thread,
args=(selected_tests, result_queue),
daemon=True
).start()
return jsonify({'status': 'started'})
@app.route('/api/stream')
def stream():
def generate():
while True:
try:
data = result_queue.get(timeout=1)
yield f"data: {json.dumps(data)}\n\n"
if data.get('type') == 'done':
break
except queue.Empty:
yield f"data: {json.dumps({'type': 'heartbeat'})}\n\n"
return Response(
generate(),
mimetype='text/event-stream',
headers={'Cache-Control': 'no-cache', 'X-Accel-Buffering': 'no'}
)
@app.route('/report')
def report():
return render_template('report_page.html')
@app.route('/report-raw')
def report_raw():
if os.path.exists(os.path.join(BASE_DIR, 'report.html')):
return send_from_directory(BASE_DIR, 'report.html')
return "No report found. Run tests first.", 404
@app.route('/assets/<path:filename>')
def assets(filename):
return send_from_directory(os.path.join(BASE_DIR, 'assets'), filename)
if __name__ == '__main__':
import webbrowser
webbrowser.open('http://localhost:5001')
app.run(port=5001, debug=False, threaded=True)