4. Example #2
Notes:
If you enable debug support the server will reload itself
on code changes, and it will also provide you with a
helpful debugger if things go wrong
The route() decorator
Default Port 5000
fromflaskimportFlask
app=Flask(__name__)
#---------------------------------------------
@app.route('/')
defindex():
return'IndexPage'
@app.route('/hello')
defhello():
return'HelloWorld'
#---------------------------------------------
if__name__=='__main__':
#app.run()
app.run(host='0.0.0.0')
#app.debug=True
#app.run(debug=True)
hello.py
4 / 20
17. test_flaskr.py
py.testtest_flaskr.py
py.testwill run all files in the current directory and its
subdirectories of the form test_*.pyor *_test.py
importpytest
importos
importflaskr
importtempfile
#---------------------------------------------
@pytest.fixture
defclient(request):
db_fd,flaskr.app.config['DATABASE']=tempfile.mkstemp()
flaskr.app.config['TESTING']=True
client=flaskr.app.test_client()
withflaskr.app.app_context():
flaskr.init_db()
defteardown():
os.close(db_fd)
os.unlink(flaskr.app.config['DATABASE'])
request.addfinalizer(teardown)
returnclient
#---------------------------------------------
deflogin(client,username,password):
returnclient.post('/login',data=dict(
username=username,
password=password
),follow_redirects=True)
#---------------------------------------------
deflogout(client):
returnclient.get('/logout',follow_redirects=True)
17 / 20