1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
import pytest
from flask import Flask, render_template_string
from flask_wtf import FlaskForm
from wtforms import BooleanField, PasswordField, StringField, SubmitField, HiddenField
from wtforms.validators import DataRequired, Length
class HelloForm(FlaskForm):
name = StringField('Name')
username = StringField('Username', validators=[DataRequired(), Length(1, 20)])
password = PasswordField('Password', validators=[DataRequired(), Length(8, 150)])
remember = BooleanField('Remember me')
hidden = HiddenField()
submit = SubmitField()
@pytest.fixture
def hello_form():
return HelloForm
@pytest.fixture(autouse=True)
def app():
app = Flask(__name__)
app.testing = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.secret_key = 'for test'
@app.route('/')
def index():
return render_template_string('{{ bootstrap.load_css() }}{{ bootstrap.load_js() }}')
yield app
@pytest.fixture
def client(app):
context = app.test_request_context()
context.push()
with app.test_client() as client:
yield client
context.pop()
|