1

I have this directory structure:

FlaskBox
    FlaskSample
        static
            style.css
        templates
            1.html and others
        __init__.py
        loginModel.py
    test
        login_test.py

Code inside __init__.py

from flask import Flask, render_template, url_for, redirect, flash, abort, session, request, jsonify
from random import randint
from FlaskSample.loginModel import Login


app = Flask(__name__)
app.config['SECRET_KEY'] = 'you-will-never-guess'

@app.route('/login', methods=['GET', 'POST'])
def login():
    try:
        form = Login()
        if form.validate_on_submit():
            if form.userName.data == 'Admin' and form.password.data == 'Admin':
                print(form.userName.data)
                session.clear()
                session['Username'] = form.userName.data
                return redirect(url_for('getMember', name=form.userName.data))
            else:
                print('error')
                flash("Please Provide proper username and password")

        return render_template('Login.html', title='Sign In', form=form)
    except Exception:
        abort(404)
        raise

And i want to write test case for login and inside test directory i have a file login_test.py consist of following:

import os
import tempfile
import pytest
from FlaskSample import loginModel


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


def login(client, username, password):
    """Helper function to login"""
    return client.post('/login', data={
        'username': username,
        'password': password
    }, follow_redirects=True)


def test_login_logout(client):
    rv = login(client, 'user1', 'wrongpassword')
    assert b'Invalid password' in rv.data

But whene i run this using pytest i gives me error Like: ImportError: No module named 'FlaskSample'

Can anyone suggest me what's the issue? And how can i use functions from two different directory

here is my error stack:

here is my error stack

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Dhruv Patadia
  • 152
  • 2
  • 10
  • instead of `from FlaskSample.loginModel import Login` do `from .loginModel import Login` and the same for `from FlaskSample import loginModel`, should be `from . import loginModel` – Adelin Jul 16 '18 at 12:08
  • Possible duplicate of [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Adelin Jul 16 '18 at 12:10
  • @Adelin Sorry it was a typo. Actually i want to use Login Function from __init__.py into my login_test.py loginModel import was by mistake – Dhruv Patadia Jul 16 '18 at 12:11
  • Add an empty `conftest.py` file to `FlaskBox` directory. – hoefling Jul 16 '18 at 12:19
  • Thanks @hoefling, that solved import problem :), but can you suggest how can i test login function using pytest? – Dhruv Patadia Jul 16 '18 at 12:49

0 Answers0