Pry - Test Trees

pry test trees consist of two types of nodes:

  • TestContainer objects are internal nodes that can contain Test nodes or other TestContainers. The TestContainer objects set up and tear down fixtures for the nodes they contain.

  • Test objects are leaf nodes corresponding to actual unit tests. Each Test object corresponds to a single test. Test objects must be direct children of a TestContainer object.

Example

This example constructs a simple tree with one TestContainer containing two Test nodes:

import libpry

class MyContainer(libpry.TestContainer):
    def setUpAll(self):
        print "setUpAll"

    def tearDownAll(self):
        print "tearDownAll"

    def setUp(self):
        print "\tsetUp"

    def tearDown(self):
        print "\ttearDown"

class MyTest(libpry.Test):
    def __call__(self):
        print "\t\t%s..."%self.name

tests = [
    MyContainer(), [
        MyTest("test_one"),
        MyTest("test_two"),
    ]
]
(examples/test_trees.py)

As this example shows, trees can be constructed using nested lists. An equivalent idiom would be to pass the children of each container node as instantiation arguments:

tests = [
    MyContainer(
        [MyTest("test_one"), MyTest("test_two")]
    )
]

Since all nodes are full TinyTree.Tree objects, there is also a rich object interface for constructing and manipulating trees (see the TinyTree documentation for more information).

It is now possible to interrogate and run this test suite using the pry command-line tool. The following command gives a graphical representation of the test tree structure:

> pry -l test_trees.py
./test_trees
     MyContainer
         test_one
         test_two
Total: 2

We can illustrate the order in which setUp and tearDown methods are run by running the suite. We silence pry's output using the -q flag, so we can focus on just the example output:

> pry -q test_trees.py
setUpAll
	setUp
		test_one...
	tearDown
	setUp
		test_two...
	tearDown
tearDownAll

Test Paths

Each test is uniquely identified by its path from the root of the test tree. For example, we can run just test_one in the example above by specifying its path to pry:

> pry -q test_trees.MyContainer.test_one
setUpAll
	setUp
		test_one...
	tearDown
tearDownAll

Note that the module in which the test occurs can be specified as the initial part of the path.

Copyright Nullcube 2008