2010-10-11 05:45:45 +08:00
==============================================================
2011-03-04 06:40:38 +08:00
Injecting objects into test functions (funcargs)
2010-10-11 05:45:45 +08:00
==============================================================
2010-11-13 18:10:45 +08:00
.. currentmodule:: _pytest.python
2010-10-11 05:45:45 +08:00
2010-11-06 06:37:25 +08:00
.. _`funcargs`:
2010-10-11 05:45:45 +08:00
.. _`funcarg mechanism`:
2010-11-25 19:11:10 +08:00
Dependency injection through function arguments
2010-10-11 05:45:45 +08:00
=================================================
2012-06-25 23:35:33 +08:00
py.test lets you inject objects into test invocations and precisely
control their life cycle in relation to the overall test execution. Moreover,
you can run a test function multiple times injecting different objects.
2011-03-04 06:40:38 +08:00
The basic mechanism for injecting objects is also called the
2011-12-05 18:10:48 +08:00
*funcarg mechanism* because objects are ultimately injected
2011-03-04 06:40:38 +08:00
by calling a test function with it as an argument. Unlike the
classical xUnit approach *funcargs* relate more to `Dependency Injection`_
because they help to de-couple test code from objects required for
2012-06-25 23:35:33 +08:00
them to execute. At test writing time you do not need to care for the
details of how your required resources are constructed or if they
live through a function, class, module or session scope.
2011-03-04 06:40:38 +08:00
.. _`Dependency injection`: http://en.wikipedia.org/wiki/Dependency_injection
To create a value with which to call a test function a factory function
is called which gets full access to the test function context and can
register finalizers or invoke lifecycle-caching helpers. The factory
2012-06-25 23:35:33 +08:00
can be implemented in same test class or test module, in a
per-directory ``conftest.py`` file or in an external plugin. This
allows total de-coupling of test and setup code.
2010-11-25 19:11:10 +08:00
A test function may be invoked multiple times in which case we
speak of :ref:`parametrized testing <parametrizing-tests>`. This can be
very useful if you want to test e.g. against different database backends
or with multiple numerical arguments sets and want to reuse the same set
of test functions.
2010-10-11 05:45:45 +08:00
2012-06-25 23:35:33 +08:00
py.test comes with some :ref:`builtinfuncargs` and there are some refined usages in the examples section.
2011-03-04 06:40:38 +08:00
2011-02-09 21:55:21 +08:00
.. _funcarg:
2011-03-04 06:40:38 +08:00
Basic injection example
--------------------------------
2010-10-11 05:45:45 +08:00
2011-03-04 06:40:38 +08:00
Let's look at a simple self-contained test module::
2010-10-11 05:45:45 +08:00
2010-10-11 05:54:00 +08:00
# content of ./test_simplefactory.py
2010-10-11 05:45:45 +08:00
def pytest_funcarg__myfuncarg(request):
return 42
def test_function(myfuncarg):
assert myfuncarg == 17
2011-03-04 06:40:38 +08:00
This test function needs an injected object named ``myfuncarg``.
2012-06-25 23:35:33 +08:00
py.test will automatically discover and call the ``pytest_funcarg__myfuncarg``
factory. Running the test looks like this::
2010-10-11 05:54:00 +08:00
$ py.test test_simplefactory.py
2011-11-19 02:32:11 +08:00
=========================== test session starts ============================
2012-05-23 00:30:34 +08:00
platform linux2 -- Python 2.7.1 -- pytest-2.2.4
2010-11-26 20:26:56 +08:00
collecting ... collected 1 items
2010-11-06 18:38:53 +08:00
2010-10-11 05:45:45 +08:00
test_simplefactory.py F
2010-11-06 18:38:53 +08:00
2011-11-19 02:32:11 +08:00
================================= FAILURES =================================
______________________________ test_function _______________________________
2010-11-06 18:38:53 +08:00
2010-10-11 05:45:45 +08:00
myfuncarg = 42
2010-11-06 18:38:53 +08:00
2010-10-11 05:45:45 +08:00
def test_function(myfuncarg):
> assert myfuncarg == 17
E assert 42 == 17
2010-11-06 18:38:53 +08:00
2010-10-11 16:07:14 +08:00
test_simplefactory.py:5: AssertionError
2012-05-23 00:30:34 +08:00
========================= 1 failed in 0.01 seconds =========================
2010-10-11 05:45:45 +08:00
2012-06-25 23:35:33 +08:00
This shows that the test function was called with a ``myfuncarg``
argument value of ``42`` and the assert fails as expected. Here is
how py.test comes to call the test function this way:
2010-10-11 05:45:45 +08:00
2010-11-25 19:11:10 +08:00
1. py.test :ref:`finds <test discovery>` the ``test_function`` because
of the ``test_`` prefix. The test function needs a function argument
named ``myfuncarg``. A matching factory function is discovered by
looking for the name ``pytest_funcarg__myfuncarg``.
2010-10-11 05:45:45 +08:00
2. ``pytest_funcarg__myfuncarg(request)`` is called and
returns the value for ``myfuncarg``.
2011-03-04 06:40:38 +08:00
3. the test function can now be called: ``test_function(42)``.
This results in the above exception because of the assertion
2010-11-25 19:11:10 +08:00
mismatch.
2010-10-11 05:45:45 +08:00
Note that if you misspell a function argument or want
to use one that isn't available, you'll see an error
2011-03-04 06:40:38 +08:00
with a list of available function arguments.
2012-06-25 23:35:33 +08:00
.. Note::
You can always issue::
py.test --funcargs test_simplefactory.py
2010-10-11 05:45:45 +08:00
2012-06-25 23:35:33 +08:00
to see available function arguments.
2010-10-11 05:45:45 +08:00
2012-06-25 23:35:33 +08:00
The request object passed to factories
-----------------------------------------
2010-10-11 05:45:45 +08:00
2012-06-25 23:35:33 +08:00
Each funcarg factory receives a :py:class:`~_pytest.main.Request` object which
provides methods to manage caching and finalization in the context of the
test invocation as well as several attributes of the the underlying test item. In fact, as of version pytest-2.3, the request API is implemented on all Item
objects and therefore the request object has general :py:class:`Node attributes and methods <_pytest.main.Node>` attributes. This is a backward compatible
change so no changes are neccessary for pre-2.3 funcarg factories.
2010-10-11 05:45:45 +08:00
.. _`parametrizing tests, generalized`: http://tetamap.wordpress.com/2009/05/13/parametrizing-python-tests-generalized/
.. _`blog post about the monkeypatch funcarg`: http://tetamap.wordpress.com/2009/03/03/monkeypatching-in-unit-tests-done-right/
.. _`xUnit style`: xunit_setup.html
.. _`funcarg factory`:
.. _factory:
.. _`test generators`:
.. _`parametrizing-tests`:
2010-11-25 19:11:10 +08:00
.. _`parametrized test functions`:
2010-10-11 05:45:45 +08:00
Parametrizing multiple calls to a test function
===========================================================
You can parametrize multiple runs of the same test
function by adding new test function calls with different
function argument values. Let's look at a simple self-contained
example:
Basic generated test example
----------------------------
2010-11-06 06:37:25 +08:00
Let's consider a test module which uses the ``pytest_generate_tests``
hook to generate several calls to the same test function::
2010-10-11 05:45:45 +08:00
2010-10-12 16:59:04 +08:00
# content of test_example.py
2010-10-11 05:45:45 +08:00
def pytest_generate_tests(metafunc):
if "numiter" in metafunc.funcargnames:
2011-11-17 19:09:21 +08:00
metafunc.parametrize("numiter", range(10))
2010-10-11 05:45:45 +08:00
def test_func(numiter):
assert numiter < 9
2011-11-17 19:09:21 +08:00
Running this will generate ten invocations of ``test_func`` passing in each of the items in the list of ``range(10)``::
2010-10-11 05:45:45 +08:00
2010-10-11 05:54:00 +08:00
$ py.test test_example.py
2011-11-19 02:32:11 +08:00
=========================== test session starts ============================
2012-05-23 00:30:34 +08:00
platform linux2 -- Python 2.7.1 -- pytest-2.2.4
2010-11-26 20:26:56 +08:00
collecting ... collected 10 items
2010-11-06 18:38:53 +08:00
2010-10-11 05:45:45 +08:00
test_example.py .........F
2010-11-06 18:38:53 +08:00
2011-11-19 02:32:11 +08:00
================================= FAILURES =================================
_______________________________ test_func[9] _______________________________
2010-11-06 18:38:53 +08:00
2010-10-11 05:45:45 +08:00
numiter = 9
2010-11-06 18:38:53 +08:00
2010-10-11 05:45:45 +08:00
def test_func(numiter):
> assert numiter < 9
E assert 9 < 9
2010-11-06 18:38:53 +08:00
2011-11-17 19:09:21 +08:00
test_example.py:6: AssertionError
2012-05-23 00:30:34 +08:00
==================== 1 failed, 9 passed in 0.02 seconds ====================
2010-10-13 18:26:14 +08:00
2011-11-17 19:09:21 +08:00
Obviously, only when ``numiter`` has the value of ``9`` does the test fail. Note that the ``pytest_generate_tests(metafunc)`` hook is called during
2010-11-06 06:37:25 +08:00
the test collection phase which is separate from the actual test running.
Let's just look at what is collected::
2010-10-13 18:26:14 +08:00
$ py.test --collectonly test_example.py
2011-11-19 02:32:11 +08:00
=========================== test session starts ============================
2012-05-23 00:30:34 +08:00
platform linux2 -- Python 2.7.1 -- pytest-2.2.4
2011-03-09 17:58:36 +08:00
collecting ... collected 10 items
2010-11-06 18:38:53 +08:00
<Module 'test_example.py'>
<Function 'test_func[0]'>
<Function 'test_func[1]'>
<Function 'test_func[2]'>
<Function 'test_func[3]'>
<Function 'test_func[4]'>
<Function 'test_func[5]'>
<Function 'test_func[6]'>
<Function 'test_func[7]'>
<Function 'test_func[8]'>
<Function 'test_func[9]'>
2011-03-09 17:58:36 +08:00
2012-05-23 00:30:34 +08:00
============================= in 0.00 seconds =============================
2010-10-13 18:26:14 +08:00
If you want to select only the run with the value ``7`` you could do::
$ py.test -v -k 7 test_example.py # or -k test_func[7]
2011-11-19 02:32:11 +08:00
=========================== test session starts ============================
2012-05-23 00:30:34 +08:00
platform linux2 -- Python 2.7.1 -- pytest-2.2.4 -- /home/hpk/venv/0/bin/python
2010-11-26 20:26:56 +08:00
collecting ... collected 10 items
2010-11-06 18:38:53 +08:00
2011-11-17 19:09:21 +08:00
test_example.py:5: test_func[7] PASSED
2010-11-06 18:38:53 +08:00
2011-11-19 02:32:11 +08:00
======================= 9 tests deselected by '-k7' ========================
2012-02-06 07:33:04 +08:00
================== 1 passed, 9 deselected in 0.01 seconds ==================
2010-10-11 05:45:45 +08:00
2011-02-09 21:55:21 +08:00
You might want to look at :ref:`more parametrization examples <paramexamples>`.
2010-10-11 05:45:45 +08:00
.. _`metafunc object`:
The **metafunc** object
-------------------------------------------
metafunc objects are passed to the ``pytest_generate_tests`` hook.
They help to inspect a testfunction and to generate tests
according to test configuration or values specified
in the class or module where a test function is defined:
``metafunc.funcargnames``: set of required function arguments for given function
``metafunc.function``: underlying python test function
``metafunc.cls``: class object where the test function is defined in or None.
``metafunc.module``: the module object where the test function is defined in.
``metafunc.config``: access to command line opts and general config
2011-11-20 07:45:05 +08:00
.. automethod:: Metafunc.parametrize
.. automethod:: Metafunc.addcall(funcargs=None,id=_notexists,param=_notexists)