Skip to content

Instantly share code, notes, and snippets.

@howardrotterdam
Created May 4, 2025 08:58
Show Gist options
  • Select an option

  • Save howardrotterdam/e6573295b749eb9e0037298c496025ac to your computer and use it in GitHub Desktop.

Select an option

Save howardrotterdam/e6573295b749eb9e0037298c496025ac to your computer and use it in GitHub Desktop.
How to use pytest to write tests in Jupyter Notebook or Google Colab?

Gemini 2.0flash:

Ah, bringing the power of pytest into the interactive environments of Jupyter notebooks and Google Colab! While the typical pytest workflow involves running tests from the command line, you can definitely integrate it into these environments for more immediate feedback and experimentation. Here are a few approaches you can take:

Method 1: Using ! (Shell Commands)

This is the most straightforward way to run pytest from within a notebook cell. You essentially use the ! prefix to execute shell commands.

  1. Install pytest (if you haven't already):

    !pip install pytest
  2. Write your test functions in .py files: Pytest is designed to discover test functions within Python files (typically named test_*.py or *_test.py). Create a new file (e.g., test_my_code.py) in the same directory as your notebook or in a location pytest can find. Inside this file, define your test functions:

    # test_my_code.py
    def add(x, y):
        return x + y
    
    def test_add_positive_numbers():
        assert add(2, 3) == 5
    
    def test_add_negative_numbers():
        assert add(-1, -2) == -3
  3. Run pytest from the notebook cell: Navigate to the directory containing your test file (you can use !cd your_directory) and then run pytest:

    !pytest

    Pytest will discover and run the tests in test_my_code.py and display the results directly in the notebook output.

  4. Running specific tests: You can also target specific tests or test files:

    !pytest test_my_code.py::test_add_positive_numbers

Method 2: Using ipytest (Recommended for Notebook Integration)

ipytest is a pytest plugin specifically designed for running pytest within Jupyter notebooks and IPython environments. It provides better integration and output formatting within the notebook.

  1. Install ipytest:

    !pip install ipytest
  2. Enable ipytest within your notebook: In a code cell, run:

    import ipytest
    ipytest.autoconfig()

    ipytest.autoconfig() configures pytest to run within the notebook environment.

  3. Write your tests directly in notebook cells: Now, you can define your test functions directly within notebook cells. You need to mark these cells as "code" cells.

    %%ipytest
    
    def multiply(x, y):
        return x * y
    
    def test_multiply_positive():
        assert multiply(2, 4) == 8
    
    def test_multiply_by_zero():
        assert multiply(5, 0) == 0

    The %%ipytest cell magic tells ipytest to collect and run the tests within that cell.

  4. Run all tests in the notebook: To run all cells marked with %%ipytest, you can execute:

    ipytest.run()

Method 3: Hybrid Approach (Mixing .py files and notebook cells)

You can also combine these approaches. You might have some core logic in separate .py files with corresponding tests, and then use ipytest in the notebook to test specific notebook-related functionality or to quickly experiment with tests.

Considerations for Jupyter Notebook/Colab:

  • File Management: Be mindful of where your test files are located relative to your notebook. Use !ls or !pwd to check the current directory.
  • Dependencies: Ensure all necessary libraries and modules are installed in your notebook environment.
  • Output: ipytest generally provides cleaner and more integrated output within the notebook compared to the raw !pytest output.
  • Restarting the Kernel: If you make significant changes to your code or test setup, you might need to restart the Jupyter kernel for the changes to be reflected correctly.
  • Google Colab Specifics: The file system in Colab is temporary. If you're working with separate .py files, you might need to re-upload them if your Colab session disconnects.

Which method should you choose?

  • For quick experimentation and testing small pieces of code directly within the notebook, ipytest offers the most seamless integration.
  • If you have more structured projects with separate modules and dedicated test files, using !pytest to run tests from .py files is a more conventional and organized approach.
  • A hybrid approach can be useful when you have a mix of both scenarios.

By using either of these methods, you can effectively leverage the power of pytest for writing and running tests within your Jupyter Notebook or Google Colab environment, leading to more robust and reliable code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment