Introduction to testing with Pytest on Colab

Image source:

Pytest is a powerful testing tool for Python that can be used to test everything from individual functions and classes to entire applications. In this tutorial, you will learn how to use Pytest to write and run tests for your Python code.

Why do we need Pytest?

Pytest makes it easy to identify and isolate problems in your code, and helps you find and fix them faster.

How does it work?

Installing Pytest:

Pytest can be installed using pip:

sudo pip install pytest

Running Tests:

To run tests using pytest , run the pytest command from the command line:


This will run all of the tests in the current directory. To run a Pytest for a specific test file or test module specifying the filename or module name as a command-line argument as shown below will suffice:



Running Pytest in Colab:

Let’s say we have a function load_csv to read from a file in CSV format to create a Pandas dataframe. We have 3 scenarios for the function to behave in:

  1. If the dataframe is successfully created then the function returns a value of 1
  2. If the file does not exist in the file path specified, then it returns 2
  3. If the file is empty/ any other error, then return 3

The code corresponding to the 3 possible situations is shown below:

Function to load 2 CSV files

The logs in this case look something like:

Function call logs

Writing a Pytest on Colab:

We convert the cell containing function load_csv to a python file for it to behave like an individual test file. This can be achieved by adding %%file at the top of the code cell.

Example: %%file

Upon execution, we can see that a python file has been created in the working directory.

Test file creation

And the function call is made from within a Class object. An assertion is made on the expected return value/ result of the function call, as shown in the code snippet below:

Pytest code

A Pytest for this particular python file can be carried out by typing in the next cell:

!python -m pytest

Voila! And the test result is:

Pytest execution & result

Watch it in action:

Code execution

Feel free to access the notebook via the link:


[1] (n.d.). The writing and reporting of assertions in tests — pytest documentation. [online] Available at: [Accessed 30 Jan. 2022].

‌[2] Marketing, J.P.G.A.D.S.B. 2019 G.I. passionate about digital, Data-Driven, H.B.B.M., Analytics, A.I. in L. and Change, D.S. to D.A. (2020). The Complete List Of Python Assert Statements. [online] Just Understanding Data. Available at: [Accessed 30 Jan. 2022].

Connect with me on to discuss more on this topic.‌




Data Scientist/ Analyst, Language Tutor, AI enthusiast, Polyglot, Artist and lifelong learner.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

AWS Certified Solutions Architect — Associate

Keyboard trying to Cover Ocean!

How to create an Xcode project and connect it to Github?

Spring-Boot Native Image

Systemd Tools → systemctl

How to deploy Node.js app to Digital Ocean droplet with free Continuous Integration

How to use YouTube APIs and play youtube videos in Flutter.

5 Pro Tips To Become a Better Programmer

Umbrella Network Weekly Recap: Week of May 24th, 2021

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lubna Khan

Lubna Khan

Data Scientist/ Analyst, Language Tutor, AI enthusiast, Polyglot, Artist and lifelong learner.

More from Medium

Phishing URL Detection With Python

A Simple Recommendation System using Python

Extracting Faces from Images Using Opencv Python

A futuristic image showing the facial keypoints

Interactive Background Removal with OpenCV and GrabCut Algorithm