Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py.test to test flask register, AssertionError: Popped wrong request context

Tags:

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

like image 569
Spirit Avatar asked Oct 30 '14 07:10

Spirit


People also ask

Can I pass arguments to pytest when testing flask applications?

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.

What type of testing should I use in 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.

What is pytest-flask and how to use it?

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.

How do I create a test environment for a flask application?

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:


2 Answers

It's a known flask problem. You receive two exceptions instead one. Simply add PRESERVE_CONTEXT_ON_EXCEPTION = False to your test config.

like image 177
Jimilian Avatar answered Oct 11 '22 18:10

Jimilian


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))     ... 
like image 31
mrquintopolous Avatar answered Oct 11 '22 18:10

mrquintopolous