大数跨境

测试用例越写越多管不过来,用Flask+Pytest造一个测试管理平台(2)

测试用例越写越多管不过来,用Flask+Pytest造一个测试管理平台(2) 51Testing软件测试网
2026-08-17
5
导读:Flask+Pytest测试管理平台的前端模板完整代码,含基础布局、用例列表、计划管理、报告查看页面,可直接复用。

点击蓝字

关注我们

上一期我们学习了构建动化测试用例管理平台的准备工作,今天我们将继续深入学习创建前端模板。


五、创建前端模板


5.1 基础模板(templates/base.html)

<!DOCTYPE html><html lang="zh-CN"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>{% block title %}自动化测试平台{% endblock %}</title>    <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">    <style>        * {            margin0;            padding0;            box-sizing: border-box;        }        body {            font-family'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;            background#f5f7fa;            min-height100vh;            padding20px;        }        .container {            max-width1200px;            margin0auto;            background: white;            border-radius10px;            box-shadow010px 40px rgba(0,0,0,0.1);            overflow: hidden;        }        .header {            background#2c3e50;            color: white;            padding30px;            text-align: center;        }        .header h1 {            font-size2.5em;            margin-bottom10px;        }        .nav {            background#f8f9fa;            padding15px 30px;            border-bottom2px solid #e9ecef;        }        .nav a {            color#2c3e50;            ;            margin-right25px;            font-weight600;            transition: color 0.3s;        }        .nav a:hover {            color#3498db;        }        .content {            padding30px;        }        .btn {            background#3498db;            color: white;            border: none;            padding12px 30px;            border-radius5px;            cursor: pointer;            font-size16px;            transition: all 0.3s;        }        .btn:hover {            background#2980b9;             transformtranslateY(-2px);            box-shadow05px 15px rgba(521522190.3);        }        table {            width100%;            border-collapse: collapse;            margin-top20px;        }        thtd {            padding15px;            text-align: left;            border-bottom1px solid #e9ecef;        }        th {            background#f8f9fa;            font-weight600;            color#495057;        }        tr:hover {            background#f8f9fa;        }    </style></head><body>    <div class="container">        <div class="header">            <h1>🚀 自动化测试管理平台</h1>            <p>基于 Flask + Pytest 构建</p>        </div>        <div class="nav">            <a href="{{ url_for('index') }}">📋 测试用例</a>            <a href="{{ url_for('plan_list') }}">📝 测试计划</a>            <a href="{{ url_for('reports_list') }}">📊 测试报告</a>        </div>        <div class="content">            {% block content %}{% endblock %}        </div>    </div></body></html>



5.2 测试用例列表页(templates/index.html)

{% extends "base.html" %}
{% block title %}测试用例列表{% endblock %}
{% block content %}<h2>📋 测试用例管理</h2><p style="color: #6c757d; margin: 10px 0 20px 0;">共 {{ test_cases|length }} 个测试用例</p>
<form method="POST" action="{{ url_for('create_plan') }}" style="margin-bottom: 30px;">    <div style="margin-bottom: 20px;">        <label style="display: block; margin-bottom: 10px; font-weight: 600;">创建测试计划:</label>        <input type="text" name="plan_name" placeholder="输入计划名称"               style="padding: 10px; width: 300px; border: 2px solid #e9ecef; border-radius: 5px;" required>        <button type="submit" class="btn">创建计划</button>    </div>        {% if test_cases %}    <table>        <thead>            <tr>                <th style="width: 50px;">选择</th>                <th>测试文件</th>                <th>测试用例名称</th>                <th>用例路径</th>            </tr>        </thead>        <tbody>            {% forcase in test_cases %}            <tr>                <td><input type="checkbox" name="test_cases" value="{{ case.path }}" style="width: 20px; height: 20px;"></td>                <td>{{ case.file }}</td>                <td><code>{{ case.name }}</code></td>                <td style="color: #6c757d; font-size: 0.9em;">{{ case.path }}</td>            </tr>            {% endfor %}        </tbody>    </table>    {% else %}    <div style="text-align: center; padding: 50px; color: #6c757d;">        <p style="font-size: 1.2em;">暂无测试用例</p>        <p style="margin-top: 10px;">请在 test_cases 目录下创建测试文件</p>    </div>    {% endif %}</form>{% endblock %}



5.3 测试计划列表页(templates/plan_list.html)

{% extends "base.html" %}
{% block title %}测试计划列表{% endblock %}
{% block content %}<h2>📝 测试计划管理</h2><p style="color: #6c757d; margin: 10px 0 20px 0;">共 {{ plans|length }} 个测试计划</p>
{% if plans %}<table>    <thead>        <tr>            <th>计划名称</th>            <th>创建时间</th>            <th>测试用例数量</th>            <th>操作</th>        </tr>    </thead>    <tbody>        {% for plan in plans %}        <tr>            <td><strong>{{ plan.name }}</strong></td>            <td>{{ plan.created_at }}</td>            <td>{{ plan.test_cases|length }} 个用例</td>            <td>                <a href="{{ url_for('run_plan', plan_name=plan.filename.replace('.json', '')) }}"                   class="btn" style="padding: 8px 20px; font-size: 14px; ;">                    ▶️ 执行测试                </a>            </td>        </tr>        {% endfor %}    </tbody></table>{% else %}<div style="text-align: center; padding: 50px; color: #6c757d;">    <p style="font-size: 1.2em;">暂无测试计划</p>    <p style="margin-top: 10px;">请先在测试用例页面创建测试计划</p>    <a href="{{ url_for('index') }}" class="btn" style="margin-top: 20px; display: inline-block; ;">        前往创建    </a></div>{% endif %}{% endblock %}



5.4 测试报告列表页(templates/report.html)

{% extends "base.html" %}
{% block title %}测试报告列表{% endblock %}
{% block content %}<h2>📊 测试报告管理</h2><p style="color: #6c757d; margin: 10px 0 20px 0;">共 {{ reports|length }} 份测试报告</p>
{% if reports %}<table>    <thead>        <tr>            <th>报告名称</th>            <th>生成时间</th>            <th>文件大小</th>            <th>操作</th>        </tr>    </thead>    <tbody>        {% for report in reports %}        <tr>            <td><code>{{ report.name }}</code></td>            <td>{{ report.created_at }}</td>            <td>{{ report.size }}</td>            <td>                <a href="{{ url_for('view_report', report_name=report.name) }}"                   target="_blank" class="btn" style="padding: 8px 20px; font-size: 14px; ;">                    👁️ 查看报告                </a>            </td>        </tr>        {% endfor %}    </tbody></table>{% else %}<div style="text-align: center; padding: 50px; color: #6c757d;">    <p style="font-size: 1.2em;">暂无测试报告</p>    <p style="margin-top: 10px;">执行测试计划后将自动生成报告</p>    <a href="{{ url_for('plan_list') }}" class="btn" style="margin-top: 20px; display: inline-block; ;">        前往执行测试    </a></div>{% endif %}{% endblock %}



5.5 创建样式文件(static/styles.css)

/* 自定义样式 - 可根据需要扩展 */code {    background#f8f9fa;    padding2px 6px;    border-radius3px;    font-family'Courier New', monospace;    color#e83e8c;}
.btn-secondary {    background#6c757d;}
.btn-success {    background#28a745;}
.btn-danger {    background#dc3545;}
.alert {    padding15px;    margin-bottom20px;    border-radius5px;}
.alert-success {    background#d4edda;    color#155724;    border1px solid #c3e6cb;}
.alert-error {    background#f8d7da;    color#721c24;    border1px solid #f5c6cb;}

未完待续

下期我们将继续深入学习创建示例测试用例与计划、启动和使用平台、功能扩展建议。

声明:本文为51Testing软件测试网 blues_C 用户投稿内容,该用户投稿时已经承诺独立承担涉及知识产权的相关法律责任,并且已经向51Testing承诺此文并无抄袭内容。发布本文的用途仅仅为学习交流,不做任何商用,未经授权请勿转载,否则作者和51Testing有权追究责任。如果您发现本公众号中有涉嫌抄袭的内容,欢迎发送邮件至:editor@51testing.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。


图片

【声明】内容源于网络
0
0
51Testing软件测试网
博为峰51Testing软件测试网提供各种线上招聘、线上课程等网络服务,出版软件测试系列丛书及电子杂志,组织线上技术交流活动;同时还举办多种线下公益活动,如软件测试沙龙、软件测试专场招聘会等。
内容 3939
粉丝 0
51Testing软件测试网 博为峰51Testing软件测试网提供各种线上招聘、线上课程等网络服务,出版软件测试系列丛书及电子杂志,组织线上技术交流活动;同时还举办多种线下公益活动,如软件测试沙龙、软件测试专场招聘会等。
总阅读2.8k
粉丝0
内容3.9k