I'm using flask to do register and login:
from flask.ext.security.views import register, login class Register(Resource): def post(self): return register() class Login(Resource): def post(self): return login() api.add_resource(Login, '/login') api.add_resource(Register, '/register')
then I use py.test to test the class:
class TestAPI: def test_survey(self, app): client = app.test_client() data = {'email': 'test@test', 'password': 'password'} rv = client.post('/2014-10-17/register', data=json.dumps(data)) ...
when I ran the test, the error occurred as follow:
AssertionError: Popped wrong request context. (<RequestContext 'http://localhost/2014-10-17/register' [POST] of panel.app> instead of <RequestContext 'http://localhost/' [GET] of panel.app>)
Do you know why? And when testing login, there was no such error
Even when checking code coverage, arguments can still be passed to pytest: This article provides a guide for testing Flask applications, focusing on: Patrick is a software engineer from the San Francisco Bay Area with experience in C++, Python, and JavaScript. His favorite areas of teaching are Vue and Flask.
For example, in a Flask app, you may use unit tests to test: Functional tests, meanwhile, should focus on how the view functions operate. Focus on testing scenarios that the end user will interact with. The experience that the users of your product have is paramount! pytest is a test framework for Python used to write, organize, and run test cases.
pytest-flask facilitates testing Flask apps by providing a set of common fixtures used for testing Flask apps. This library is not used in this tutorial, as I want to show how to create the fixtures that help support testing Flask apps.
flask_app = create_app('flask_test.cfg') In order to create the proper environment for testing, Flask provides a test_client helper. This creates a test version of our Flask application, which we used to make a GET call to the '/' URL. We then check that the status code returned is OK (200) and that the response contained the following strings:
It's a known flask problem. You receive two exceptions instead one. Simply add PRESERVE_CONTEXT_ON_EXCEPTION = False
to your test config.
It seems that you have to wrap you testing calls with something like this:
with self.app.test_client() as client: data = {'email': 'test@test', 'password': 'password'} rv = client.post('/2014-10-17/register', data=json.dumps(data)) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With