Table of contents
1.
Introduction 
2.
Testing your Application
3.
Testing your Application in CherryPy
3.1.
Example
4.
Frequently Asked Questions
4.1.
What is CherryPy?
4.2.
What should you do with your application after its development is complete?
4.3.
How can you test your CherryPy application?
4.4.
What is unit testing?
4.5.
Why do we need testing?
5.
Conclusion
Last Updated: Mar 27, 2024
Hard

Testing your Application in CherryPy

Author Teesha Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

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. 

This article will discuss the methods for testing your application in CherryPy. 

CherryPy

Testing your Application

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. Your application should pass all these tests to be made market ready. 

Testing your application

There are various types of testing approaches, each suitable for different purposes. You have to understand the different approaches and then choose a set of different types of tests to run against your application. 

Following are some of the standard testing approaches

🚀 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. The developers themselves usually perform this.

Unit testing

🚀 Usability Testing: Developers frequently forget that they are writing an application for end users unfamiliar with the system. Usability testing validates the product's advantages and disadvantages.

Usability testing

🚀Load and Performance Testing: All testing related to verifying the system's performance and monitoring how it behaves under stress comes under this category. 

Load and performance testing

🚀Integration Testing: It is a software testing technique in which the various units, modules, or components of application software are tested as a whole. These modules, however, may have been coded by different programmers.

Integration testing

🚀Regression Testing: It ensures that subsequent product releases do not break any of the previous features and functionalities.

Regression testing

🚀System Testing: It is performed on an entire integrated system to assess the system's compliance with the corresponding requirements.

System testing

There are many more testing approaches. To learn more about the different software testing approaches, you can visit Software Testing and Quality Assurance | Learn & Practice from Coding Ninjas Studio.  

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 AmazonGoogleMicrosoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Thank You

Happy Coding!

Live masterclass