404 Not Found when testing Flask application with pytest 404 Not Found when testing Flask application with pytest flask flask

404 Not Found when testing Flask application with pytest


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 pytestfrom web_application import application@pytest.fixturedef client():    with application.test_client() as client:        yield clientclass TestSomething:    def test_this(self, client):        res = client.get('/greetings?name=Rick Sanchez')        assert res.status_code == 200

Checkout the official docs on testing.


class TestConfig(Config):    TESTING = True    SQLALCHEMY_DATABASE_URI = 'sqlite://'    ELASTICSEARCH_URL = None    SERVER_NAME = 'localhost.com:5000'class AuthAPICase(unittest.TestCase):    def setUp(self):        self.app = create_app(TestConfig)        self.app.testing = True        self.app_context = self.app.app_context()        self.app_context.push()        db.create_all()        self.client = self.app.test_client()    def tearDown(self):        db.session.remove()        db.drop_all()        self.app_context.pop()    def test_users(self):        # register a new account        rv = self.client.get('/users')        print('response')        print(rv.get_json())        print(rv.headers)        print(rv.request)        self.assertTrue(rv.status_code == 404)

As the above code, I didn’t create two app instances and still reported 404.