How to test REST API

Use Test Utilities from werkzeug

http://werkzeug.pocoo.org/docs/0.12/test/

In [1]: from werkzeug.test import Client

In [2]: from frappe.app import application

In [3]: from werkzeug.wrappers import BaseResponse

In [4]: c = Client(application, BaseResponse)

In [5]: resp = c.get('/api/method/version')

In [6]: resp.status_code
Out[6]: 200

In [7]: resp.data
Out[7]: '{"message":"8.4.1"}'

3 Likes

Following also works,

c = frappe.utils.get_test_client() 
ci, status, headers = c.get('/api/method/version')

But there are errors while testing, all tests after executing this test fails

error:

RuntimeError: no object bound to session

for now werkzeug client cannot be used as is while testing.

any help?

@revant_one

Any update on this?

I didn’t check after my last message. Not planning to check either.

@revant_one
Even this one gives the same error

werkzeug_console

Okay. I don’t know. I don’t use it.

For testing REST API there are tests here :

@revant_one thanks for your time

  • this is my last resort
  • It sure might work in bench console, but it gives the same error when it is run using
    bench --site <<site_name>> run-tests --app <<app_name>>
  • Any any consecutive test written gives following error
Site localhost does not exist
E
======================================================================
ERROR: test_hit_check (<<app_name>>.tests.test_logged_in_user.LoggedInUser)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<<app_dir>>tests/test_logged_in_user.py", line 18, in test_hit_check
    res = c.get('/api/method/frappe.auth.get_logged_user')
  File "<<frappe-bench-dir>>/env/lib/python3.7/site-packages/werkzeug/test.py", line 1029, in get
    return self.open(*args, **kw)
  File "<<frappe-bench-dir>>/env/lib/python3.7/site-packages/werkzeug/test.py", line 993, in open
    response = self.run_wsgi_app(environ.copy(), buffered=buffered)
  File "<<frappe-bench-dir>>/env/lib/python3.7/site-packages/werkzeug/test.py", line 884, in run_wsgi_app
    rv = run_wsgi_app(self.application, environ, buffered=buffered)
  File "<<frappe-bench-dir>>/env/lib/python3.7/site-packages/werkzeug/test.py", line 1119, in run_wsgi_app
    app_rv = app(environ, start_response)
  File "<<frappe-bench-dir>>/env/lib/python3.7/site-packages/werkzeug/local.py", line 231, in application
    return ClosingIterator(app(environ, start_response), self.cleanup)
  File "<<frappe-bench-dir>>/env/lib/python3.7/site-packages/werkzeug/wrappers/base_request.py", line 237, in application
    resp = f(*args[:-2] + (request,))
  File "<<frappe-bench-dir>>/apps/frappe/frappe/app.py", line 54, in application
    init_request(request)
  File "<<frappe-bench-dir>>/apps/frappe/frappe/app.py", line 114, in init_request
    frappe.init(site=site, sites_path=_sites_path)
  File "<<frappe-bench-dir>>/apps/frappe/frappe/__init__.py", line 150, in init
    local.conf = _dict(get_site_config())
  File "<<frappe-bench-dir>>/apps/frappe/frappe/__init__.py", line 224, in get_site_config
    sys.exit(1)
SystemExit: 1

Can you send this to someone who can help me? Thanks again!