Python Training by Dan Bader

OS X notifications for your pytest runs

This article shows you how to use the pytest-osxnotify, a plugin for pytest that adds native Mac OS X notifications to the pytest terminal runner.

pytest-osxnotify demo gif

pytest + OS X notifications = happy developers

pytest-osxnotify is a plugin for the pytest testing tool. It adds OS X notifications to your test runs so you know when a test run completes and whether it failed or succeeded without looking at your terminal window.

This is especially useful when you re-run your tests automatically every time a source file was modified.

A quick example

Installing pytest-osxnotify is easy. Let’s set up a simple example that shows you how to use pytest so that it watches your source files for modifications and re-runs the tests as necessary.

We start by installing pytest, pytest-xdist and pytest-osxnotify1.

$ pip install pytest pytest-xdist pytest-osxnotify

Let’s also create a simple test file for us to run. Save the following as example_test.py in the current folder.

def test_example1():
    assert True

def test_example2():
    assert True

def test_example3():
    assert True

Now we start the pytest watcher that monitors our source file for modifications and re-runs the tests when necessary.

$ py.test -f example_test.py

That’s it. We can now move our terminal to the background and hack away in our favourite editor knowing that we’ll stay informed about the results of our test runs.

pytest-osxnotify example

  1. You’ll typically want to install your dependencies into a Python virtualenv so that they don’t pollute your system install. Look here for a good tutorial on using virtualenv. 

<strong><em>Improve Your Python</em></strong> with a fresh 🐍 <strong>Python Trick</strong> 💌 every couple of days

Improve Your Python with a fresh 🐍 Python Trick 💌 every couple of days

🔒 No spam ever. Unsubscribe any time.

This article was filed under: programming, python, tdd, and testing.

Related Articles:
  • Catching bogus Python asserts on CI – It’s easy to accidentally write Python assert statements that always evaluate to true. Here’s how to avoid this mistake and catch bad assertions as part of your continuous integration build.
  • Click & jump to any file or folder from the terminal – iTerm2 for macOS has a little known feature that lets you open files and folders simply by Cmd+Clicking on them in the terminal. Among other things, this is super handy for debugging tests.
  • A countdown timer extension for Alfred – I wrote a countdown timer extension for the Alfred application launcher for OS X. The extension is open-source, written in Python and uses Mountain Lion’s user notifications.
  • Monochrome font rendering with FreeType and Python – For my Raspberry Pi internet radio project I needed a way to render text suitable for a low resolution monochrome LCD. This article describes how to render 1-bit text using FreeType and Python.
  • Setting up Sublime Text for Python development – I recently started using Sublime Text 2 more and more as my main editor for Python development. This article explains my setup and some tweaks that make Python programmers happy.
Effective Python Development with Sublime Text

Effective Python Development with Sublime Text
How to get an efficient Sublime Text setup for writing Python that makes you more productive and will be an absolute joy to use:
» Click here to get the free 5 day course

Latest Articles:
Improve Your Python with 🐍 Python Tricks 💌

Improve Your Python with 🐍 Python Tricks 💌
Get a short & sweet Python code snippet delivered to your inbox every couple of days:
» Click here to see examples

← Browse All Articles