How to do pytest/unittest in Google Colab (Colaboratory) Notebook for data scientists

Oct 15, 2020

Author: Tara Su

A notebook is convenient for data scientists as a quick prototyping tool. When it comes to writing repeatable code beyond a POC (Proof of Concept) attempt, a notebook is rarely the first choice of an AI/ML developer.

Well constructed python-files (.py scripts) and carefully written test cases are addictive. Once you get into the rhythm, you rarely would want to go back to notebooks…… Except when you want a nice interface for quick visual and documentation. But even then, you would prefer to only write documentation and driver code in the notebook which calls the underlying python file run.

Google Colab allows you to write and execute Python in your browser, with zero configuration required, many useful packages preinstalled, and free access to GPUs. Only if it can run python files and test suits. Of course, it can! It’s from Google! The collective of great developers!

You just need to be able to do 2 things:

  1. Call command line from a Colab notebook.
  2. Mount Google drive to your notebook for convenience.

It’s that simple!

Here is how you can do it