Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Easy and flexible unittest parameterization.

Project Description

unittest_expander is a Python library that provides flexible and easy-to-use tools to parameterize your unit tests, especially those based on unittest.TestCase.

The library is compatibile with Python 2.6, 2.7, 3.2, 3.3 and 3.4, and does not depend on external packages (uses only the Python standard library).

Author:Jan Kaliszewski (zuo)
License:MIT License
Home Page:https://github.com/zuo/unittest_expander
Documentation:http://unittest-expander.readthedocs.org/

Installing

The easiest way to install the library is to execute (possibly in a virtualenv) the command:

pip install unittest_expander

(note that you need network access to do it this way; if you do not have the pip tool installed – see: https://pip.pypa.io/en/latest/installing.html).

Alternatively, you can download the library source archive, unpack it, cd to the unpacked directory and execute the following command:

python setup.py install

(you may need to have administrator privileges and/or network access, especially if you are executing it not in a virtualenv).

It is also possible to use the library without installing it: as its code is contained in a single file (unittest_expander.py), you can just copy it into your project.

Usage example

Consider the following ugly test:

import unittest

class Test(unittest.TestCase):
    def test_sum(self):
        for iterable, expected in [
            ([], 0),
            ([0], 0),
            ([3], 3),
            ([1, 3, 1], 5),
            (set([1, 3]), 4),
            ({1:'a', 3:'b'}, 4),
        ]:
            self.assertEqual(sum(iterable), expected)

Is it cool? Not at all! So let’s improve it:

import unittest
from unittest_expander import expand, foreach

@expand
class Test(unittest.TestCase):
    @foreach(
        ([], 0),
        ([0], 0),
        ([3], 3),
        ([1, 3, 1], 5),
        (set([1, 3]), 4),
        ({1:'a', 3:'b'}, 4),
    )
    def test_sum(self, iterable, expected):
        self.assertEqual(sum(iterable), expected)

Now you have 6 distinct tests (properly isolated and being always reported as separate tests), although they share the same test method source.

You may want to do the same in a bit more verbose and descriptive way:

import unittest
from unittest_expander import expand, foreach, param

@expand
class Test(unittest.TestCase):

    test_sum_params = [
        param([], expected=0).label('empty gives 0'),
        param([0], expected=0),
        param([3], expected=3),
        param([1, 3, 1], expected=5),
        param(set([1, 3]), expected=4),
        param({1:'a', 3:'b'}, expected=4).label('even dict is ok'),
    ]

    @foreach(test_sum_params)
    def test_sum(self, iterable, expected):
        self.assertEqual(sum(iterable), expected)

This is only a fraction of the possibilities unittest_expander offers to you.

You can learn more from the actual documentation of the module.

Release History

Release History

This version
History Node

0.3.1

History Node

0.3.0

History Node

0.2.1

History Node

0.1.2

History Node

0.1.1

History Node

0.1.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
unittest_expander-0.3.1.tar.gz (31.2 kB) Copy SHA256 Checksum SHA256 Source Aug 19, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting