lundi 21 janvier 2019

Test a Flask Form

given this code fragment

@blueprint.route('/search', methods=['POST','GET'])
def search():
    form = SearchForm(search="CARMPAT FP Equity")
    if request.method == 'POST' and form.validate():
        return render_template("results.html", query=form.search.data)

    return render_template("search.html", form=form)

how would I (unittest) the post functionality? I was trying to mock the SearchForm but no success yet...

I am using pytest. I have the client as a fixture available:

@pytest.fixture(scope="module")
def client():
    app = create_app()
    app.config['WTF_CSRF_METHODS'] = []  # This is the magic
    app.config['TESTING'] = True
    with app.app_context():
        db.session = __session()
        __init_session(session=db.session)

    yield app.test_client()
    db.session.close()

Any help is very appreciated

Aucun commentaire:

Enregistrer un commentaire