Testing CherryPy application

Testing CherryPy application

Python: CherryPy


Testing any application we make is one of the most important parts of development. There are many reasons why we should test our application:

  • Testing helps us to find out the defects as well as errors that we can't recognize during the development.

  • We can also say that testing is necessary to check whether the application produces the correct/expected output.

  • It is also necessary to check about the quality of the application. Testing also helps us to check the quality of the product.

As we now understand the importance of testing we will now test our CherryPy application. First, we will write an application where '<application-name>/' opens the main page '<application-name>/second_page' opens another page. To do this in CherryPy we will define two methods in our class, one index(self) to handle the first page and another second_page(self) to handle the second page.

Save the below code in a file named 'app.py'.

import cherrypy

class application:
def index(self):
return "This is the main page of the application."

def second_page(self):
return "This is the second page of the application."

if __name__ == '__main__':

Now, we will make the test file. Save the below code in a file named 'test.py'.

import cherrypy
from cherrypy.test import helper

from app import application

class SimpleCPTest(helper.CPWebCase):
def setup_server():
'/', {})

def test_index(self):
'200 OK')

def test_second_page(self):
'200 OK')

To run the test we will need Pytest. Install it by writing the following command on a Terminal/Command Line.

    pip3 install pytest

To check if the installation is correct or to check for any additional info. Run the following on a Terminal/Command Line.

    pip3 show pytest

Now run the test with the following command.

    pytest -v test.py

The output of the test will be something like this

=============================== test session starts ================================
platform linux -- Python
3.8.2, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: <test-directory-path>
3 items

test.py::SimpleCPTest::test_index PASSED [
test.py::SimpleCPTest::test_second_page PASSED [
test.py::SimpleCPTest::test_gc PASSED [

3 passed in 0.50s =================================

The output says that all the tests have passed which means that our application is running correctly.

More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 991 2
Number Guessing Game using Python 683 2
Pyperclip: Not Implemented Error 1033 2
Hangman Game using Python 16821 2
Using Databases with CherryPy application 1676 2
nose: Working 509 2
pytest: Working 512 2
Open Source and Hacktoberfest 868 2
Managing Logs of CherryPy applications 1005 2
Top 20 Data Science Tools 684 2
Ajax application using CherryPy 799 2
REST application using CherryPy 664 2
On Screen Keyboard using Python 5532 2
Elastic Net Regression 816 2
US Presidential Election 2020 Prediction using Python 795 2
Sound Source Separation 1165 2
URLs with Parameters in CherryPy 1635 2
Testing CherryPy application 638 2
Handling HTML Forms with CherryPy 1450 2
Applications of Natural Language Processing in Businesses 511 2
NetworkX: Multigraphs 649 2
Tracking User Activity with CherryPy 1404 2
CherryPy: Handling Cookies 822 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1306 2
Fake News Detection Model using Python 735 2
Keeping Home Routers secure while working remotely 484 2
Email Slicer using Python 2998 2
NetworkX: Creating a Graph 1111 2
Best Mathematics Courses for Machine Learning 551 2
Hello World in CherryPy 681 2
Building dependencies as Meson subprojects 979 2
Vehicle Detection System 1081 2
NetworkX: Examining and Removing Graph Elements 608 2
Handling URLs with CherryPy 537 2
PEP 8 - Guide to Beautiful Python Code 759 2
NetworkX: Drawing Graphs 624 2
Mad Libs Game using Python 645 2
Hosting Cherry applications 613 2
Top 5 Free Online IDEs of 2020 867 2
pytest: Introduction 535 2
Preventing Pwned and Reused Passwords 582 2
Contact Book using Python 2095 2
Introduction to CherryPy 547 2
nose: Introduction 505 2
Text-based Adventure Game using Python 3002 2
NetworkX: Adding Attributes 2290 2
NetworkX: Directed Graphs 1021 2
Dice Simulator using Python 562 2
Decorating CherryPy applications using CSS 834 2