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 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

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.
  • 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.
  • Functional linked lists in Python – Linked lists are fundamental data structures that every programmer should know. This article explains how to implement a simple linked list data type in Python using a functional programming style.
  • Software engineer reading list: My favourite books about programming – Reading books is one of the best ways to improve your craftsmanship and to become a better software developer. This is a continuously updated list with my favourite programming books, sorted by topic. I link to the ebook version where possible but most books should be available made from dead trees as well.
  • Assert Statements in Python – How to use assertions to help automatically detect errors in your Python programs in order to make them more reliable and easier to debug.
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:
Unique Mousepads for Pythonistas

Unique Mousepads for Pythonistas
Rest Your Hand Comfortably & Write Beautiful Python
» Browse Python Mousepads at

← Browse All Articles