Search code examples
pythonflaskpytest

ModuleNotFoundError: No module named 'application' - Pytest


Im tring to start with Pytest and I need to import my app into the test file

My error is ModuleNotFoundError: No module named 'application'

My file tree is as follows: filetree

and i have a wsgi.py file that looks like so

from application import init_app

app = init_app()

if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)

I have a test file that looks like so:

import pytest
from application import app


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


def test_get_assets(client):
    response = client.get('/assets/get')
    assert response.status_code == 200

and my __init__ file in my application folder is as follows:

from flask import Flask
from config import Config

from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_bcrypt import Bcrypt
from flask_jwt_extended import JWTManager
from flask_migrate import Migrate
from flask_mail import Mail
from flask_cors import CORS

# Global accessible libraries
db = SQLAlchemy()
ma = Marshmallow()
bcrypt = Bcrypt()
JWTManager = JWTManager()
migration = Migrate()
mail = Mail()
cors = CORS()


def init_app():
    app = Flask(__name__, instance_relative_config=False)
    app.config.from_object(Config)

Solution

  • Add an __init__.py file in your tests package/folder. I think pytest still requires it to navigate through python packages.