-1

Before you read, let me tell you that I've searched about it but couldn't find the resolution of it.

I am writing test cases using unittest for a Flask API that has GET and POST requests. Earlier, I was calling the function using the endpoint and the test cases were running smoothly but the test coverage didn't cover the server codenode.py. So, I am trying to run it by calling the defined methods directly in the test file say Test1.py .

node.py

@app.route('/address/<addressId>', methods = ["GET"])
def address(addressId):
    response = searchAddress(sheet_t,addressId)
    if len(response) == 0:
        return jsonify({"note":"No transactions not found for user"})
    else:
        return jsonify(response)

Test1.py

import unittest
import n as node
from flask import Flask
import json

app = Flask(__name__)

class TestSearch(unittest.TestCase):

    def test_address(self):
        data1 = {
            "transactions": [
                {
                    "amount": 10,
                    "recipient": "ABC",
                    "sender": "DEF",
                }
            ]
        }
        data2 = json.dumps(data1)
        res = node.address("ABC")
        self.assertEquals(res,data2)

if __name__ == '__main__':
    unittest.main()

Error

======================================================================
ERROR: test_address (__main__.TestSearch)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:/POC/test1.py", line 21, in test_address
    res = node.address("ABC")
  File "C:\POC\node.py", line 444, in address
    return jsonify(response)
  File "C:\ProgramData\Anaconda3\lib\site-packages\flask\json\__init__.py", line 309, in jsonify
    if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] or current_app.debug:
  File "C:\ProgramData\Anaconda3\lib\site-packages\werkzeug\local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
  File "C:\ProgramData\Anaconda3\lib\site-packages\werkzeug\local.py", line 306, in _get_current_object
    return self.__local()
  File "C:\ProgramData\Anaconda3\lib\site-packages\flask\globals.py", line 51, in _find_app
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context().  See the
documentation for more information.

----------------------------------------------------------------------
Ran 1 test in 0.006s

FAILED (errors=1)

Here, I can't load the jsonify response on the test method as a string. Any ideas on how to do that?

I can't change the server code by doing json.dumps() instead of jsonify().

Ayush Kumar
  • 833
  • 2
  • 13
  • 30

1 Answers1

2

This doesn't have anything to do with loading anything as a string.

As the error says, you're working outside the application context - you're trying to call the handler function directly rather than using the test framework. You should read the Flask docs on how to use the test client.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I have tried changing that to a sample string response too and it was working fine. It's not working with `jsonify()` – Ayush Kumar Dec 03 '18 at 18:27
  • instead of `return jsonify(response)` , I tried doing following in server code and it was returning a response and the response was being loaded in the test file. `res = json.dumps(response)` `return res` But I can't do this way – Ayush Kumar Dec 03 '18 at 18:31