Merge pull request #4156 from yuxiaoy1/patch-4
Improve the code example in testing docs
This commit is contained in:
commit
5261aeb7c7
1 changed files with 7 additions and 6 deletions
|
|
@ -48,20 +48,21 @@ the application for testing and initializes a new database::
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from flaskr import create_app
|
from flaskr import create_app
|
||||||
|
from flaskr.db import init_db
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def client():
|
def client():
|
||||||
db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
|
db_fd, db_path = tempfile.mkstemp()
|
||||||
flaskr.app.config['TESTING'] = True
|
app = create_app({'TESTING': True, 'DATABASE': db_path})
|
||||||
|
|
||||||
with flaskr.app.test_client() as client:
|
with app.test_client() as client:
|
||||||
with flaskr.app.app_context():
|
with app.app_context():
|
||||||
flaskr.init_db()
|
init_db()
|
||||||
yield client
|
yield client
|
||||||
|
|
||||||
os.close(db_fd)
|
os.close(db_fd)
|
||||||
os.unlink(flaskr.app.config['DATABASE'])
|
os.unlink(db_path)
|
||||||
|
|
||||||
This client fixture will be called by each individual test. It gives us a
|
This client fixture will be called by each individual test. It gives us a
|
||||||
simple interface to the application, where we can trigger test requests to the
|
simple interface to the application, where we can trigger test requests to the
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue