dimanche 29 août 2021

How to set form data in a test file for a flask application

I'm writing a test for a simple Flask MongoDB application. The application runs totally fine. But I'm Not able to get the test working for the signup functionality. I've shared the complete code here in pastbin, not to fill up too much space here.

Before, I dive into the question, I'd like to mention that I've recently started learning flask by referring docs and tutorials. I've searched everywhere I can, for a solution for this, but couldn't find one.

In the signup function the models.py, I get the data using request.form.get(). But I'm not sure how do I get this data in test. I mean, I found out that in order to test the flask app, I can use test_client(). But by making that call in the test, like, let's say -- testclnt_obj.post('/user/signup'), it will eventually make call to the route, which will call User().signup().

And that will again call the request.form.get() inside the signup function and try to get the data from the html form, which as per my understanding is NOT existing (correct me if I'm wrong here). What I mean is, when I run the application, I have an html and I get the data from that html form using flask request. But when testing, there is no html to get the data from.

I'm a bit confused here, about how to send the data to signup and not get the html form code get called. I do not wish to have full code solution, rather an approach on the ways to resolve this issue/the confusion I have. Or any better way to test this is also appreciated.

I tried one thing, (checkout the test code test_signup.py at the end of shared code) where I pass data to the signup directly and start the session, and try to assert the session values, but it threw me the Working outside of request context error:

======================================================================
ERROR: test_signup (test_signup.TestUserSignup)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/cygdrive/d/Learning/Python/Flask/MDBLogin/App/test/test_signup.py", line 35, in test_signup
    User().signup(test_user)
  File "/cygdrive/d/Learning/Python/Flask/MDBLogin/App/src/models.py", line 65, in signup
    return self.start_session(luser)
  File "/cygdrive/d/Learning/Python/Flask/MDBLogin/App/src/models.py", line 25, in start_session
    session['logged_in'] = True
  File "/cygdrive/d/Learning/Python/Flask/MDBLogin/App/penv-mdblogin/lib/python3.8/site-packages/werkzeug/local.py", line 422, in __get__
    obj = instance._get_current_object()
  File "/cygdrive/d/Learning/Python/Flask/MDBLogin/App/penv-mdblogin/lib/python3.8/site-packages/werkzeug/local.py", line 544, in _get_current_object
    return self.__local()  # type: ignore
  File "/cygdrive/d/Learning/Python/Flask/MDBLogin/App/penv-mdblogin/lib/python3.8/site-packages/flask/globals.py", line 33, in _lookup_req_object
    raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

----------------------------------------------------------------------
Ran 1 test in 0.084s

FAILED (errors=1)

Thanks




Aucun commentaire:

Enregistrer un commentaire