How to test REST API

Use Test Utilities from werkzeug

In [1]: from werkzeug.test import Client

In [2]: from 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]:
Out[7]: '{"message":"8.4.1"}'


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


RuntimeError: no object bound to session

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

any help?


Any update on this?

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

Even this one gives the same error


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
ERROR: test_hit_check (<<app_name>>.tests.test_logged_in_user.LoggedInUser)
Traceback (most recent call last):
  File "<<app_dir>>tests/", 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/", line 1029, in get
    return*args, **kw)
  File "<<frappe-bench-dir>>/env/lib/python3.7/site-packages/werkzeug/", line 993, in open
    response = self.run_wsgi_app(environ.copy(), buffered=buffered)
  File "<<frappe-bench-dir>>/env/lib/python3.7/site-packages/werkzeug/", 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/", line 1119, in run_wsgi_app
    app_rv = app(environ, start_response)
  File "<<frappe-bench-dir>>/env/lib/python3.7/site-packages/werkzeug/", line 231, in application
    return ClosingIterator(app(environ, start_response), self.cleanup)
  File "<<frappe-bench-dir>>/env/lib/python3.7/site-packages/werkzeug/wrappers/", line 237, in application
    resp = f(*args[:-2] + (request,))
  File "<<frappe-bench-dir>>/apps/frappe/frappe/", line 54, in application
  File "<<frappe-bench-dir>>/apps/frappe/frappe/", line 114, in init_request
    frappe.init(site=site, sites_path=_sites_path)
  File "<<frappe-bench-dir>>/apps/frappe/frappe/", line 150, in init
    local.conf = _dict(get_site_config())
  File "<<frappe-bench-dir>>/apps/frappe/frappe/", line 224, in get_site_config
SystemExit: 1

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