[Python] Unit Tests for FLASK API

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    I have a basic flask API running :

    @app.route('/helloworld', methods = ['GET'])
    def first_api():
    hname = "hello"
    lhname = "world"
    print(hname+lhanme)


    Now I need to add some unit tests to it, and here is my unit test file:

    import json
    def test_index(app, client):
    res = client.get('/helloworld')
    assert res.status_code == 200
    assert "hello" in res.data


    How can I pass value for variables hname and lhname from this unit test?

    Here is my conf file for pytest:

    import pytest
    from app import app as flask_app

    @pytest.fixture
    def app():
    return flask_app

    @pytest.fixture
    def client(app):
    return app.test_client()

    Continue reading...

Compartilhe esta Página