Table Of Contents

Previous topic

mysetup pattern: application specific test fixtures

Next topic

Working with non-python tests

This Page

parametrizing tests

py.test allows to easily implement your own custom parametrization scheme for tests. Here we provide some examples for inspiration and re-use.

Parametrizing test methods through per-class configuration

Here is an example pytest_generate_function function implementing a parametrization scheme similar to Michael Foords unittest parameterizer in a lot less code:

# content of ./test_parametrize.py
import pytest

def pytest_generate_tests(metafunc):
    # called once per each test function
    for funcargs in metafunc.cls.params[metafunc.function.__name__]:
        # schedule a new test function run with applied **funcargs
        metafunc.addcall(funcargs=funcargs)

class TestClass:
    # a map specifying multiple argument sets for a test method
    params = {
        'test_equals': [dict(a=1, b=2), dict(a=3, b=3), ],
        'test_zerodivision': [dict(a=1, b=0), dict(a=3, b=2)],
    }

    def test_equals(self, a, b):
        assert a == b

    def test_zerodivision(self, a, b):
        pytest.raises(ZeroDivisionError, "a/b")

Running it means we are two tests for each test functions, using the respective settings:

$ py.test -q
F..F
================================= FAILURES =================================
_________________________ TestClass.test_equals[0] _________________________

self = <test_parametrize.TestClass instance at 0x128a638>, a = 1, b = 2

    def test_equals(self, a, b):
>       assert a == b
E       assert 1 == 2

test_parametrize.py:17: AssertionError
______________________ TestClass.test_zerodivision[1] ______________________

self = <test_parametrize.TestClass instance at 0x1296440>, a = 3, b = 2

    def test_zerodivision(self, a, b):
>       pytest.raises(ZeroDivisionError, "a/b")
E       Failed: DID NOT RAISE

test_parametrize.py:20: Failed
2 failed, 2 passed in 0.03 seconds

Parametrizing test methods through a decorator

Modifying the previous example we can also allow decorators for parametrizing test methods:

# content of test_parametrize2.py

import pytest

# test support code
def params(funcarglist):
    def wrapper(function):
        function.funcarglist = funcarglist
        return function
    return wrapper

def pytest_generate_tests(metafunc):
    for funcargs in getattr(metafunc.function, 'funcarglist', ()):
        metafunc.addcall(funcargs=funcargs)

# actual test code
class TestClass:
    @params([dict(a=1, b=2), dict(a=3, b=3), ])
    def test_equals(self, a, b):
        assert a == b

    @params([dict(a=1, b=0), dict(a=3, b=2)])
    def test_zerodivision(self, a, b):
        pytest.raises(ZeroDivisionError, "a/b")

Running it gives similar results as before:

$ py.test -q test_parametrize2.py
F..F
================================= FAILURES =================================
_________________________ TestClass.test_equals[0] _________________________

self = <test_parametrize2.TestClass instance at 0x1dbcc68>, a = 1, b = 2

    @params([dict(a=1, b=2), dict(a=3, b=3), ])
    def test_equals(self, a, b):
>       assert a == b
E       assert 1 == 2

test_parametrize2.py:19: AssertionError
______________________ TestClass.test_zerodivision[1] ______________________

self = <test_parametrize2.TestClass instance at 0x1dd0488>, a = 3, b = 2

    @params([dict(a=1, b=0), dict(a=3, b=2)])
    def test_zerodivision(self, a, b):
>       pytest.raises(ZeroDivisionError, "a/b")
E       Failed: DID NOT RAISE

test_parametrize2.py:23: Failed
2 failed, 2 passed in 0.03 seconds

checking serialization between Python interpreters

Here is a stripped down real-life example of using parametrized testing for testing serialization betwee different interpreters. We define a test_basic_objects function which is to be run with different sets of arguments for its three arguments:

* ``python1``: first python interpreter
* ``python2``: second python interpreter
* ``obj``: object to be dumped from first interpreter and loaded into second interpreter
"""
module containing a parametrized tests testing cross-python
serialization via the pickle module.
"""
import py

pythonlist = ['python2.4', 'python2.5', 'python2.6', 'python2.7', 'python2.8']

def pytest_generate_tests(metafunc):
    if 'python1' in metafunc.funcargnames:
        assert 'python2' in metafunc.funcargnames
        for obj in metafunc.function.multiarg.kwargs['obj']:
            for py1 in pythonlist:
                for py2 in pythonlist:
                    metafunc.addcall(id="%s-%s-%s" % (py1, py2, obj),
                        param=(py1, py2, obj))

@py.test.mark.multiarg(obj=[42, {}, {1:3},])
def test_basic_objects(python1, python2, obj):
    python1.dumps(obj)
    python2.load_and_is_true("obj == %s" % obj)

def pytest_funcarg__python1(request):
    tmpdir = request.getfuncargvalue("tmpdir")
    picklefile = tmpdir.join("data.pickle")
    return Python(request.param[0], picklefile)

def pytest_funcarg__python2(request):
    python1 = request.getfuncargvalue("python1")
    return Python(request.param[1], python1.picklefile)

def pytest_funcarg__obj(request):
    return request.param[2]

class Python:
    def __init__(self, version, picklefile):
        self.pythonpath = py.path.local.sysfind(version)
        if not self.pythonpath:
            py.test.skip("%r not found" %(version,))
        self.picklefile = picklefile
    def dumps(self, obj):
        dumpfile = self.picklefile.dirpath("dump.py")
        dumpfile.write(py.code.Source("""
            import pickle
            f = open(%r, 'wb')
            s = pickle.dump(%r, f)
            f.close()
        """ % (str(self.picklefile), obj)))
        py.process.cmdexec("%s %s" %(self.pythonpath, dumpfile))

    def load_and_is_true(self, expression):
        loadfile = self.picklefile.dirpath("load.py")
        loadfile.write(py.code.Source("""
            import pickle
            f = open(%r, 'rb')
            obj = pickle.load(f)
            f.close()
            res = eval(%r)
            if not res:
                raise SystemExit(1)
        """ % (str(self.picklefile), expression)))
        print (loadfile)
        py.process.cmdexec("%s %s" %(self.pythonpath, loadfile))

Running it (with Python-2.4 through to Python2.7 installed):

. $ py.test -q multipython.py
....s....s....s....ssssss....s....s....s....ssssss....s....s....s....ssssss
48 passed, 27 skipped in 2.55 seconds