Comments (14)
The default app is used for a couple of things: (1) to enable the test
command to be launched from manage.py
; and (2) to enable the calls to url_for()
within tests to work.
You can address (1) by running the tests from a separate script:
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
Unfortunately (2) is a bit harder to address, because url_for()
normally wants a request context. I haven't tried this, but probably setting SERVER_NAME
to some hostname in the testing configuration will allow url_for()
to run. I'm not sure if the resulting URLs will work with the test client though, maybe a fix is needed there as well, since the test client expects relative URLs without hostname.
from flasky.
Actually, regarding (2) above, I think you can easily emulate what Flask-Script does, which is to run commands under a test request context. A test context can be created in the setUp()
methods of the test cases.
from flasky.
Thank you for the pointers, I will investigate these options. Before closing, I feel I should say that your book is amazing, great piece of work!
from flasky.
Related to this issue, I was wondering if there was a way to use a different database URI for the unit test? Because an app is created (which initialises the db) using the 'default' config in the manage.py script, I am experiencing some difficulties changing the database URI when another app is created in the unit test self.app = create_app('testing')
. I've set the SQLALCHEMY_DATABASE_URI
to a different value in TestingConfig
but database operations in the unit tests seems to still be performed on the default db URI.
note: Not sure if it makes a difference but i'm using mysql
from flasky.
@v-ken my solution to the default config problem was providing the system variable when I wanted a different config. So when I run tests, I set FLASK_CONFIG = testing on my system.
from flasky.
Thanks. That's definitely a solution. Was wondering if there was a way to reset the sqlalchemy DB to use the new URI? Seems like a much cleaner implementation as we can fully rely on self.app = create_app('testing')
in the unit test.
from flasky.
@v-ken The app that is created as part of setting up Flask-Script is not related to the app that is created for the tests, so I'm not sure why the interfere with each other. Have you debugged your issue to understand what is exactly the problem?
from flasky.
@miguelgrinberg Thanks for your response. That's what I thought as well. But I think both apps might be referencing a single db = SQLAlchemy()
instance. And I'm not sure if this is the reason but according to the Flask-SQLAlchemy docs:
Note that some of those cannot be modified after the engine was created so make sure to configure as early as possible and to not modify them at runtime.
I tested by cloning this repo and setting the following in the .env
file:
FLASK_CONFIG=development
TEST_DATABASE_URL=mysql://username:password@localhost/db_test
DEV_DATABASE_URL=mysql://username:password@localhost/db
from flasky.
What happened when you tested the .env
file above? Did you get any error?
from flasky.
I did get some errors when I ran the unit test but the point was that I noticed all the DB operations were performed on the DEV_DATABASE_URL
instead of TEST_DATABASE_URL
.
Correct me if I'm wrong but even though FLASK_CONFIG
is set to development, I would've thought that the app created in the unit test should use the 'testing' config?
In test setUp:
self.app = create_app('testing')
from flasky.
@miguelgrinberg Can you confirm that this issue isn't happening on your end? Just want to make sure that it isn't a mistake on my side. Thanks!
from flasky.
Hi @miguelgrinberg , did you get the time to confirm if this is an issue on your end as well? Can I open this as a new issue?
from flasky.
Sorry I did not answer, @v-ken. This does not happen here, when I run the tests a data-test.sqlite
file is created, and my debug database is unaffected. Are you using a pristine copy of the project or do you have changes?
from flasky.
I got the same problem as @v-ken met, after add the test case of "test_roles_and_permissions" and "test_anonymous_user", I got the error:
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: users.email [SQL: u'INSERT INTO users (email, username, role_id, password_hash) VALUES (?, ?, ?, ?)'] [parameters: ('[email protected]', None, 3, 'pbkdf2:sha1:1000$R377YAwK$7ac350e7f748f8591774be665bd1dd1ce5d5bf79')]
and the data-test.sqlite is created but empty. I ran migrate and upgrade command but didn't work.
from flasky.
Related Issues (20)
- The app has no db file to initialize the database HOT 4
- Breaks in python 3.10.6 HOT 6
- Update Read me. HOT 1
- Flask mail returning mail as exception HOT 1
- Selenium testing failing HOT 3
- Question on before_request and unconfirmed routes. HOT 4
- While testing client requests, how get/post by endpoint instead of url? HOT 2
- Used flask_ldap3_login? HOT 2
- no .env and .env-mysql file when I run docker-compose HOT 7
- Hello world
- html inheritance on tag 3b
- Flask-WTF Question HOT 2
- Redirect issue in Example 8-23 HOT 4
- HTML Syntax Issue in 2nd Edition of Book HOT 1
- Example 14-4 Question in 2nd Edition HOT 4
- A 'SQLAlchemy' instance has already been registered on this Flask app. Import and use that instance instead. HOT 1
- CSRF session token is missing
- Flask run command doesn't work HOT 6
- SMTP Handler - No Emails HOT 2
- Issue Deploying TensorFlow on Heroku Server Using Flask Technology HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flasky.