skip to navigation
skip to content

unittestplus 1.1.5

Test utilities built on unittest, including test discovery

Latest Version: 1.1.9

Description

Unit test utilities built on unittest.

Includes an augmented TestCase class with improved (stricter and more informative) assert methods; test autodiscovery; utility functions for running tests and combining TestCase classes into suites and prettier verbose test output.

As a bonus, running verbose tests with TestCasePlus also looks more readable (see end of this file).

Published under a BSD license.

Dependencies

Requires Michael Foord's excellent Mock module, http://www.voidspace.org.uk/python/mock.

Status

Complete & working, but only used on a couple of my personal projects.

Installing

Windows users may download and double-click a graphical installer from http://pypi.python.org/pypi/unittestplus.

Command-line jockeys may use any of:

easy_install unittestplus

or:

pip install unittestplus

or download a zip of the source from http://pypi.python.org/pypi/unittestplus and use:

python setup.py install

Alternatively, to check out the latest unstable source from subversion, including tests, see:

http://code.google.com/p/unittestplus/source/checkout.

Using

class unittestplus.testcaseplus.TestCasePlus

Use as you would unittest.TestCase, with the following changes:

New methods

  • assertNone(value, message='')
  • assertNotNone(value, message='')
  • assertTrue(value, message='') # fails unless value is boolean and True
  • assertFalse(value, message='') # fails unless value is boolean and False
  • assertIs(obj1, obj2, message='') # fails unless obj1 is obj2
  • assertIsNot(value, message='') # fails if obj1 is obj2

assertEquals()

assertEquals(actual, expected, message='', epsilon=0)

assertEquals now actually shows you the two compared values if it fails. Also, I've added an epsilon keyword parameter, which supplants the use of assertAlmostEquals. If it is absent, all comparisons are exact. If it is specified, then assertEquals will only fail if the different between two floats is greater or equal to epsilon. If iterables are compared, the given epsilon is used to compare each of their elements. This is not recursively applied to nested iterables.

As in unittest, assertEqual is a synonym for assertEquals. None of the failXXX style aliases are supported by TestCasePlus.

assertRaises()

assertRaises(callable, expectedException, expected_message='')

Note that this is a reversal of the argument order in unittest. I wanted assertRaises to follow a (to me) more consistent and memorable 'actual, expected' ordering of its arguments. Uses of the traditional ordering are detected and warned about.

This also allows you to specify the message that the expected exception should contain, which can't be done in unittest.

Module unittestplus.run

run()

Use as you would unittest.main(). It can be passed a TestCase (or TestCasePlus) class, or an instance of TestSuite:

from unittestplus.run import run
from unittestplus.testcaseplus import TestCasePlus

class MyTest(TestCasePlus):
    pass # etc...

if __name__ == "__main__":
    run(MyTest)

The 'run()' function also accepts 'verbosity' (int) and 'descriptions' (bool) keyword args, which it passes on to unittest.TextTestRunner. Pass verbosity=2 and descriptions=1 to see the prettier output described below.

combine()

Combine can be given any number of TestCase classes or TestSuites, and will combine them all into a single TestSuite:

from unittestplus.run import combine, run
from unittestplus.testcaseplus import TestCasePlus

class FirstTest(TestCasePlus):
    pass

class SecondTest(TestCasePlus):
    pass

AllTests = combine(FirstTest, SecondTest)

if __name__ == '__main__':
    run(AllTests)

get_all_tests()

Your top-level 'run_all_tests.py' script can easily auto-discover all tests in a package and all its sub-packages, using:

from unittestplus.run import get_all_tests, run
run(get_all_tests('unittestplus'))

run_all_tests(test_dir)

A convenience function that calls run(get_all_tests(test_dir)), while also setting verbosity of the test runner to 2 if either '-v' or '--verbose' can be found on sys.argv.

Prettier Output

Running verbose tests with TestCasePlus outputs:

testcaseplus_test.ModuleFunctionsTest.test_is_int_indexable ... ok
run_test.CombineTest.test_combine_raises_on_nontests ... ok
run_test.CombineTest.test_combine_suites ... ok
run_test.RunFuncTest.test_run_creates_testrunner ... ok

as opposed to the unittest.TestCase output:

test_is_int_indexable (unittestplus.tests.testcaseplus_test.ModuleFunctionsTest) ... ok
test_combine_raises_on_nontests (unittestplus.tests.run_test.CombineTest) ... ok
test_combine_suites (unittestplus.tests.run_test.CombineTest) ... ok
test_run_creates_testrunner (unittestplus.tests.run_test.RunFuncTest) ... ok
File Type Py Version Uploaded on Size # downloads
unittestplus-1.1.5.zip (md5) Source 2009-11-08 17:15:38.958162 13KB 21
unittestplus-1.1.5.win32.exe (md5) MS Windows installer any 2009-11-08 17:15:44.222273 207KB 3

Log in to rate this package.