Flask Documentation (1.1.x)
the Database Blueprints and Views Templates Static Files Blog Blueprint Make the Project Installable Test Coverage Deploy to Production Keep Developing! Templates Jinja Setup Standard Context Standard Filters Processors Testing Flask Applications The Application The Testing Skeleton The First Test Logging In and Out Test Adding Messages Other Testing Tricks Faking Resources and Context Keeping the Context Object Blueprint Objects Incoming Request Data Response Objects Sessions Session Interface Test Client Test CLI Runner Application Globals Useful Functions and Classes Message Flashing JSON Support Template0 码力 | 428 页 | 895.98 KB | 1 年前3Flask Documentation (1.1.x)
url_for() properly handles that for you. For example, here we use the test_request_context() method to try out url_for(). test_request_context() tells Flask to behave as though it’s handling a request route('/user/') def profile(username): return '{}\'s profile'.format(escape(username)) with app.test_request_context(): print(url_for('index')) print(url_for('login')) print(url_for('login', next='/')) The easiest solution for unit testing is to use the test_request_context() context manager. In combination with the with statement it will bind a test request so that you can interact with it. Here is an 0 码力 | 291 页 | 1.25 MB | 1 年前3Flask入门教程
@app.route('/user/') def user_page(name): return 'User: %s' % name @app.route('/test') def test_url_for(): # 下面是一些调用示例: print(url_for('hello')) # 输出:/ # 注意下面两个调用是如何生成包含 URL 输出:/user/peter print(url_for('test_url_for')) # 输出:/test # 下面这个调用传入了多余的关键字参数,它们会被作为查询字符串附加到 URL 后面。 print(url_for('test_url_for', num=2)) # 输出:/test?num=2 return 'Test page' 实验过程中编写的代码可以删掉 tearDown(self): # 测试固件 pass def test_sayhello(self): # 第 1 个测试 rv = sayhello() self.assertEqual(rv, 'Hello!') def test_sayhello_to_somebody(self) # 第 2 个测试 0 码力 | 127 页 | 7.62 MB | 1 年前3Flask-RESTful Documentation Release 0.3.10
Warning Debug mode should never be used in a production environment! Now open up a new prompt to test out your API using curl $ curl http://127.0.0.1:5000/ {"hello": "world"} Resourceful Routing The included to take care of setting up a virtualenv for running tests. All you need to do is run: $ make test To change the Python version used to run the tests (default is Python 2.7), change the PYTHON_MAJOR with: $ make test-all Individual tests can be run using a command with the format: nosetests:ClassName.func_name Example: $ source env/bin/activate $ nosetests tests/test_reqparse.py:ReqParseTestCase 0 码力 | 42 页 | 84.60 KB | 1 年前3Flask-RESTful Documentation Release 0.3.10
Warning: Debug mode should never be used in a production environment! Now open up a new prompt to test out your API using curl $ curl http://127.0.0.1:5000/ {"hello": "world"} 1.2.2 Resourceful Routing included to take care of setting up a virtualenv for running tests. All you need to do is run: $ make test To change the Python version used to run the tests (default is Python 2.7), change the PYTHON_MAJOR with: $ make test-all Individual tests can be run using a command with the format: nosetests:ClassName.func_name Example: $ source env/bin/activate $ nosetests tests/test_reqparse.py:ReqParseTestCase 0 码力 | 39 页 | 212.29 KB | 1 年前3Flask-RESTful Documentation Release 0.3.6
Warning: Debug mode should never be used in a production environment! Now open up a new prompt to test out your API using curl $ curl http://127.0.0.1:5000/ {"hello": "world"} 1.2.2 Resourceful Routing included to take care of setting up a virtualenv for running tests. All you need to do is run: $ make test To change the Python version used to run the tests (default is Python 2.7), change the PYTHON_MAJOR with: $ make test-all Individual tests can be run using using a command with the format: nosetests:ClassName.func_name Example: $ source env/bin/activate $ nosetests tests/test_reqparse.py:ReqParseTestCase 0 码力 | 46 页 | 245.60 KB | 1 年前3Flask-RESTful Documentation Release 0.3.6
Warning Debug mode should never be used in a production environment! Now open up a new prompt to test out your API using curl $ curl http://127.0.0.1:5000/ {"hello": "world"} Resourceful Routing The included to take care of setting up a virtualenv for running tests. All you need to do is run: $ make test To change the Python version used to run the tests (default is Python 2.7), change the PYTHON_MAJOR with: $ make test-all Individual tests can be run using using a command with the format: nosetests:ClassName.func_name Example: $ source env/bin/activate $ nosetests tests/test_reqparse.py:ReqParseTestCase 0 码力 | 49 页 | 91.90 KB | 1 年前3Flask-RESTful Documentation Release 0.3.7
Warning: Debug mode should never be used in a production environment! Now open up a new prompt to test out your API using curl $ curl http://127.0.0.1:5000/ {"hello": "world"} 1.2.2 Resourceful Routing included to take care of setting up a virtualenv for running tests. All you need to do is run: $ make test To change the Python version used to run the tests (default is Python 2.7), change the PYTHON_MAJOR with: $ make test-all Individual tests can be run using using a command with the format: nosetests:ClassName.func_name Example: $ source env/bin/activate $ nosetests tests/test_reqparse.py:ReqParseTestCase 0 码力 | 50 页 | 253.09 KB | 1 年前3Flask-RESTful Documentation Release 0.3.8
Warning: Debug mode should never be used in a production environment! Now open up a new prompt to test out your API using curl $ curl http://127.0.0.1:5000/ {"hello": "world"} 1.2.2 Resourceful Routing included to take care of setting up a virtualenv for running tests. All you need to do is run: $ make test To change the Python version used to run the tests (default is Python 2.7), change the PYTHON_MAJOR with: $ make test-all Individual tests can be run using a command with the format: nosetests:ClassName.func_name Example: $ source env/bin/activate $ nosetests tests/test_reqparse.py:ReqParseTestCase 0 码力 | 50 页 | 253.64 KB | 1 年前3Flask-RESTful Documentation Release 0.3.8
Warning Debug mode should never be used in a production environment! Now open up a new prompt to test out your API using curl $ curl http://127.0.0.1:5000/ {"hello": "world"} Resourceful Routing The included to take care of setting up a virtualenv for running tests. All you need to do is run: $ make test To change the Python version used to run the tests (default is Python 2.7), change the PYTHON_MAJOR with: $ make test-all Individual tests can be run using a command with the format: nosetests:ClassName.func_name Example: $ source env/bin/activate $ nosetests tests/test_reqparse.py:ReqParseTestCase 0 码力 | 55 页 | 93.30 KB | 1 年前3
共 11 条
- 1
- 2