Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9104965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:59:07+00:00 2026-06-17T01:59:07+00:00

I recently had to rewrite our rest api, and made the switch from Flask

  • 0

I recently had to rewrite our rest api, and made the switch from Flask to Cherrypy (mostly due to Python 3 compatibility). But now I’m stuck trying to write my unit tests, Flask has a really nifty built-in test client, that you can use to sent fake requests to your application (without starting a server.) I can’t find any similar functionality for Cherrypy, is there such functionality, or am I stuck starting a server and doing actual requests against it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:59:08+00:00Added an answer on June 17, 2026 at 1:59 am

    As far as I know, CherryPy doesn’t indeed provide a facility for this type of testing (no running server). But it’s fairly easy to do it nonetheless (though it relies on some of the internals of CherryPy).

    Here’s a simple showcase:

    from StringIO import StringIO
    import unittest
    import urllib
    
    import cherrypy
    
    local = cherrypy.lib.httputil.Host('127.0.0.1', 50000, "")
    remote = cherrypy.lib.httputil.Host('127.0.0.1', 50001, "")
    
    class Root(object):
        @cherrypy.expose
        def index(self):
            return "hello world"
    
        @cherrypy.expose
        def echo(self, msg):
            return msg
    
    def setUpModule():
        cherrypy.config.update({'environment': "test_suite"})
    
        # prevent the HTTP server from ever starting
        cherrypy.server.unsubscribe()
    
        cherrypy.tree.mount(Root(), '/')
        cherrypy.engine.start()
    setup_module = setUpModule
    
    def tearDownModule():
        cherrypy.engine.exit()
    teardown_module = tearDownModule
    
    class BaseCherryPyTestCase(unittest.TestCase):
        def webapp_request(self, path='/', method='GET', **kwargs):
            headers = [('Host', '127.0.0.1')]
            qs = fd = None
    
            if method in ['POST', 'PUT']:
                qs = urllib.urlencode(kwargs)
                headers.append(('content-type', 'application/x-www-form-urlencoded'))
                headers.append(('content-length', '%d' % len(qs)))
                fd = StringIO(qs)
                qs = None
            elif kwargs:
                qs = urllib.urlencode(kwargs)
    
            # Get our application and run the request against it
            app = cherrypy.tree.apps['']
            # Let's fake the local and remote addresses
            # Let's also use a non-secure scheme: 'http'
            request, response = app.get_serving(local, remote, 'http', 'HTTP/1.1')
            try:
                response = request.run(method, path, qs, 'HTTP/1.1', headers, fd)
            finally:
                if fd:
                    fd.close()
                    fd = None
    
            if response.output_status.startswith('500'):
                print response.body
                raise AssertionError("Unexpected error")
    
            # collapse the response into a bytestring
            response.collapse_body()
            return response
    
    class TestCherryPyApp(BaseCherryPyTestCase):
        def test_index(self):
            response = self.webapp_request('/')
            self.assertEqual(response.output_status, '200 OK')
            # response body is wrapped into a list internally by CherryPy
            self.assertEqual(response.body, ['hello world'])
    
        def test_echo(self):
            response = self.webapp_request('/echo', msg="hey there")
            self.assertEqual(response.output_status, '200 OK')
            self.assertEqual(response.body, ["hey there"])
    
            response = self.webapp_request('/echo', method='POST', msg="hey there")
            self.assertEqual(response.output_status, '200 OK')
            self.assertEqual(response.body, ["hey there"])
    
    if __name__ == '__main__':
        unittest.main()
    

    Edit, I’ve extended this answer as a CherryPy recipe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a C# developer, but recently I had to rewrite a version of my
Recently had to downgrade a project from .NET 3.5 to .NET 2.0 because it
I recently had a problem creating a stringstream due to the fact that I
I recently had to re-factor some code from a previous maintainer and initially I
I recently had to update one of my model's properties from type StringProperty to
I recently had to move my database from SQL Studio over to Oracle and
We recently had a developer leave our organization. We're not sure if the version
I recently had a bug in Firefox due to me choosing the wrong script
I recently had an issue after upgrading my .net framework to 4.0 from 3.5:
I recently had a similar thread about this, but now I need to animate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.