def before_feature(context, feature): context.db, app.config['DATABASE'] = tempfile.mkstemp() app.testing = True context.client = app.test_client() with app.app_context(): init_db()
def test_faked_post(self): ts = time.time() # mock data with app.app_context(): db = get_db() db.execute("insert into entries (user, title, text) values (?, ?, ?)", [self.user, "Current timestamp", ts]) db.commit() # go to main page self.driver.get("http://localhost:5000") # check if mcoked content is correct post = self.driver.find_element_by_css_selector(".%s" % self.user).text assert "Current timestamp\n%d" % ts in post
def test_faked_post(self): ts = time.time() # mock data with app.app_context(): db = get_db() db.execute( 'insert into entries (user, title, text) values (?, ?, ?)', [self.user, 'Current timestamp', ts]) db.commit() # go to main page self.driver.get('http://localhost:5000') # check if mcoked content is correct post = self.driver.find_element_by_css_selector('.%s' % self.user).text assert 'Current timestamp\n%d' % ts in post
# flaskr 是專案裡的資料夾名稱 from flaskr import app from flaskr import db from flask import render_template, flash, redirect, url_for, request from flaskr.model import UserRegister from flaskr.form import FormRegister, FormLogin from flask_login import login_user, current_user, login_required, logout_user # db一樣要初始化 db.init_app(app) with app.app_context(): db.create_all() @app.route('/') @login_required def index(): return render_template('base.html') @app.route('/register', methods=['GET', 'POST']) def register(): form = FormRegister() if form.validate_on_submit(): user = UserRegister(username=form.username.data, email=form.email.data, password=form.password.data) db.session.add(user) db.session.commit() return '註冊成功!'
def test_db_exists(self): with app.app_context(): client = MongoClient() self.assertNotEqual(client, None)