Thomas Fritz
Thomas Fritz

Reputation: 340

404 Not Found when testing Flask application with pytest

This simple web service works if I run it by hand but in my unit tests I get a 404 not found page as my response, preventing me to properly test the application.

normal behavior:

enter image description here

Folder structure:

/SRC
--web_application.py
/UNIT_TESTS
--test_wab_application.py

web_application.py


from flask import Flask, request, jsonify, send_from_directory

from python.Greeting import Greeting

application = Flask(__name__)


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='mega_developer',
        DATABASE=os.path.join(app.instance_path, 'web_application.sqlite'),
    )
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass
    return app


@application.route('/greetings', methods=['GET', 'POST'])
def hello():

    # GET: for url manipulation #
    if request.method == 'GET':
        return jsonify(hello = request.args.get('name', 'world', str))

test_web_application.py

import tempfile
import pytest    
import web_application


class TestWebApplication:

    app = web_application.create_app()  # container object for test applications #

    @pytest.fixture
    def initialize_app(self):
        app = web_application.create_app()
        app.config['TESTING'] = True
        app.config['DEBUG'] = False
        app.config['WTF_CSRF_ENABLED'] = False
        app.config['DATABASE'] = tempfile.mkstemp()
        app.testing = True
        self.app = app

    def test_hello_get(self, initialize_app):
        with self.app.test_client() as client:
            response = client.get('/greetings?name=Rick Sanchez')

        assert response.status_code == 200

test results (most relevant part only):

Launching pytest with arguments test_web_application.py::TestWebApplication::test_hello_get in C:\Users\Xrenyn\Documents\Projekte\Studium_Anhalt\QA&Chatbots Exercises\Exercise 2 - Web Service Basics\UNIT_TESTS

============================= test session starts =============================
platform win32 -- Python 3.8.0, pytest-5.2.2, py-1.8.0, pluggy-0.13.0 -- C:\Users\Xrenyn\Documents\Projekte\Studium_Anhalt\QA&Chatbots Exercises\Exercise 2 - Web Service Basics\VENV\Scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\Xrenyn\Documents\Projekte\Studium_Anhalt\QA&Chatbots Exercises\Exercise 2 - Web Service Basics\UNIT_TESTS
collecting ... collected 1 item

test_web_application.py::TestWebApplication::test_hello_get FAILED       [100%]
test_web_application.py:21 (TestWebApplication.test_hello_get)
404 != 200

Expected :200
Actual   :404

So far I have tested various alternative routing paths for the client.get() method in test-web_application.py , including combinations like '/SRC/greetings?name=Rick Sanchez' or '../SRC/greetings?name=Rick Sanchez', but all to no different effect.

Do you have any idea on what I might be doing wrong or how I could get access to my web services' functions from within unit tests?

Upvotes: 6

Views: 10156

Answers (1)

mpawlak
mpawlak

Reputation: 319

I think the problem is that you are creating two Flask instances. One with the name application that you add hello route to, and the second one using the create_app function. You need to create a test client using the application instance (the one you added the hello route to).

Can you import application and then obtain the client using application.test_client()?

Sample solution:

import pytest

from web_application import application


@pytest.fixture
def client():
    with application.test_client() as client:
        yield client


class TestSomething:
    def test_this(self, client):
        res = client.get('/greetings?name=Rick Sanchez')
        assert res.status_code == 200

Checkout the official docs on testing.

Upvotes: 11

Related Questions