不确定如何在单元测试中发布表单数据

我有一个HTML模板,它提交一个包含用户选择的结果的表单。此表单被发布到"routes.py“中的python函数,该函数将根据此选择的值添加到数据库中。我正在尝试对此函数运行单元测试,但是不知道如何在单元测试文件中发布表单。

以下是HTML模板中表单的相关部分:

<form method="POST" action="{{ url_for('select') }}">
    <div class="info">

        <div >
            <div >
                <span>Select a car</span>
                <br/>
                <br>
                    <select name="car_select">
                        {% for i in cars %}
                            <option>{{i}}</option>
                        {% endfor %}    
                    </select>

            </div>
            <br><br>

            <button type="submit" onclick="time()">Vote</button>

        </div>
</form>

下面是我在"routes.py“中运行单元测试的函数的相关部分:

@app.route("/select" , methods=['GET', 'POST'])
def select():
    select = request.form.get('car_select')

下面是我在单元测试中用来启动"select“函数的Helper函数(不正确):

def select(self, car):
        return self.app.post("/select", data=dict(car_select=car), follow_redirects = True)

我希望这个助手函数能模拟由HTML form方法执行的POST,这样"request.form.get('car_select')“就等于助手函数的"car”参数。

转载请注明出处:http://www.kldfzc.com/article/20230526/1037596.html