pytest template

I keep forgetting this, so here it is, two exerpts from Conventions for Python test discovery and unittest support:

pytest implements the following standard test discovery:

  • If no arguments are specified then collection starts from testpaths (if configured) or the current directory. Alternatively, command line arguments can be used in any combination of directories, file names or node ids.
  • Recurse into directories, unless they match norecursedirs.
  • In those directories, search for test_*.py or * files, imported by their test package name.
  • From those files, collect test items:
    • test_ prefixed test functions or methods outside of class
    • test_ prefixed test functions or methods inside Test prefixed test classes (without an __init__ method)

Install py.test

Install via pip install pytest without the dot.



Example Content

Example content of

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unitest
from thing import Thing

class TestThing(unittest.TestCase):

  def setUp(self):
    self.testee = Thing()

  def test_first_thing(self):
    assert self.testee is not None

Now just run py.test in the base directory.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Thing(object):

  def __unicode__(self):
    fixme: write for readability

  def __str__(self):
    return unicode(self).encode("utf-8")

  def __repr__(self):
    fixme: write for unambiguous representation (uniquely identify objects)
This work by Fredrik Wendt is licensed under CC by-sa.