我有这个目录结构:
FlaskBox
FlaskSample
static
style.css
templates
1.html and others
__init__.py
loginModel.py
test
login_test.py
__ 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
我想为登录和测试目录编写测试用例,我有一个文件login_test.py包含以下内容:
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
但是当我使用pytest运行它时,我给了我类似的错误: ImportError:没有名为“ FlaskSample”的模块
有人可以建议我出什么事吗? 以及如何使用两个不同目录中的函数
这是我的错误堆栈: