Testing your Application in CherryPy
Worry not, because CherryPy has you covered with its helper class to ease the process of testing. Helper class in CherryPy helps you write functional tests easily and quickly.
You have to first develop and mount your application as usual. After doing this, you can write any number of tests you want to run against your application. You can use the getPage() method of the helper class while defining the tests. Set the page's status, header, and body using the various assert methods. This will help you validate your workflow and data.
Following is a simple example of an echo application that displays a “Hello Ninja” message.
Example
import cherrypy
from cherrypy.test import helper
class HelperSubclass(helper.CPWebCase):
def setup_s():
class Root(object):
@cherrypy.expose
def echo(self, msg):
return msg
cherrypy.tree.mount(Root())
setup_s = staticmethod(setup_s)
def test_message(self):
''' This is a test method'''
self.getPage("/echo?message=Hello%20Ninja")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/html;charset=utf-8')
self.assertBody('Hello Ninja')
def test_non_utf8_message_will_fail(self):
self.getPage("/echo?msg=A+bient%F4t",
headers=[
('Accept-Charset', 'ISO-8859-1, utf-8'),
('Content-Type', 'text/html; charset=ISO-8859-1')
]
)
self.assertStatus('404 Not Found')

You can also try this code with Online Python Compiler
Run Code
We have defined the tests using the helper class’s getPage() method and specialized assert* (assertStatus, assertHeader, assertBody) methods.
You can run tests on your application using the following command:
$ py.test -s test_echo_app.py

You can also try this code with Online Python Compiler
Run Code
The -s option mentioned in the above command is needed since the CherryPy class folds stdin and stdout. Without the flag, tests may stall while waiting for input on failed assertions.
This can also be done by disabling the interactive mode that’s enabled by default. This mainly helps when you are running your tests inside an IDE. You must disable the environment variable WEBTEST_INTERACTIVE by setting it to FALSE or 0.
If you don't want to change the environment variable, then the same thing can be done by creating a subclass of the helper class. In the subclass, you can set helper.CPWebCase.interactive to False and then use this subclass to derive all the test classes. Following is an example of how you can do so:
import cherrypy
from cherrypy.test import helper
class TestsBase(helper.CPWebCase):
helper.CPWebCase.interactive = False

You can also try this code with Online Python Compiler
Run Code
They are not bare unit tests, despite being written in the typical pattern supported by the unittest module. As a matter of fact, a whole CherryPy stack is started for your application to run.
Frequently Asked Questions
What is CherryPy?
CherryPy is a python-based, object-oriented web application framework developed in 2002. CherryPy helps in speeding up the process of web application development by wrapping the HTTP protocols.
What should you do with your application after its development is complete?
It must be tested whether it be a web application or any other program code. The program has to be tested against custom tests that cover all the possible scenarios with all the edge cases and possible inputs.
How can you test your CherryPy application?
CherryPy has you covered with its helper class to ease the process of testing. Helper class in CherryPy helps you write functional tests easily and quickly.
What is unit testing?
Unit testing is a testing approach where the smallest testable units of an application or program are tested individually and independently. It is the best way to find where the bugs are in an application.
Why do we need testing?
Testing is the method of ensuring that your application runs smoothly and error-free in all possible scenarios. It is essential to test your application because it helps you build more robust and bug-free applications.
Conclusion
This article discussed testing web applications in CherryPy. We have shown the concept of testing in CherryPy with an example.
Recommended Readings:
I hope you would have gained a better understanding of these topics now!
Are you planning to ace the interviews of reputed product-based companies like Amazon, Google, Microsoft, and more?
Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!