Sou*_*Dey 2 python sqlite flask flask-sqlalchemy flask-testing
I'm trying to do unit testing of my Flask web app. I'm use a pattern I saw in a Udemy class on Flask and a pattern similar to the Flask Mega-Tutorial online (http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vii-unit-testing). The problem I'm having is that the test does not actual create it's own database -- rather it uses the production database and messes it up.
Here's what tests.py script looks like:
import os,sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
basedir = os.path.abspath(os.path.dirname(__file__))
import unittest
from myapp import app, db
from user.models import User
class UserTest(unittest.TestCase):
def setUp(self):
self.db_uri = 'sqlite:///' + os.path.join(basedir, 'test.db')
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
app.config['SQL_ALCHEMY_DATABASE_URI'] = self.db_uri
self.app = app.test_client()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
def test_models(self):
#Create a customer user
user = User("John Doe", "jdoe@jdoe.com", "jdoe", "password", is_consultant=False)
db.session.add(user)
db.session.commit()
#Create 2 consultant users
user1 = User("Jane Doe", "janedoe@gg.com", "janedoe", "password", is_consultant=True)
db.session.add(user1)
user2 = User("Nikola Tesla", "nikola@tesla.com", "nikola", "password", is_consultant=True)
db.session.add(user2)
db.session.commit()
#Check that all users exist
assert len(User.query.all()) is 3
Run Code Online (Sandbox Code Playgroud)
My app init is in the same folder and looks like so:
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.migrate import Migrate
from flask.ext.login import LoginManager
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
# flask-login extension setup
login_manager = LoginManager()
login_manager.init_app(app)
# migration setup
migrate = Migrate(app, db)
from user import views
Run Code Online (Sandbox Code Playgroud)
I don't understand what is going on. It never creates the test.db SQLite database. It always creates the app.db production database. And if it's there, it totally messes up the database is there. Afterwards if I do python manage.py runserver -- it doesn't work anymore. It says table not found. Because the teardown dropped all the tables. What is going on? And how do I fix it?
天哪,我想通了。我为数据库 URI 设置了错误的键。它应该是:app.config['SQLALCHEMY_DATABASE_URI'] = self.db_uri.
所以一切都很好。做就是了:
class UserTest(unittest.TestCase):
def setUp(self):
self.db_uri = 'sqlite:///' + os.path.join(basedir, 'test.db')
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = self.db_uri
self.app = app.test_client()
db.create_all()
Run Code Online (Sandbox Code Playgroud)
一切都按预期工作。
我通过在测试中放置一个断点并查看 app.config 是什么来检查发生了什么 - 我看到有一个SQL_ALCHEMY_DATABASE_URI键(它没有做任何事情,我正在设置)和一个SQLALCHEMY_DATABASE_URI键(这个是最重要的)。
| 归档时间: |
|
| 查看次数: |
3681 次 |
| 最近记录: |