2016-08-23 10:35:41 +08:00
|
|
|
:orphan:
|
|
|
|
|
2017-11-04 05:13:47 +08:00
|
|
|
===================================
|
|
|
|
PROPOSAL: Parametrize with fixtures
|
|
|
|
===================================
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
This document outlines a proposal around using fixtures as input
|
|
|
|
of parametrized tests or fixtures.
|
2016-06-24 18:08:57 +08:00
|
|
|
|
|
|
|
Problem
|
|
|
|
-------
|
|
|
|
|
|
|
|
As a user I have functional tests that I would like to run against various
|
|
|
|
scenarios.
|
|
|
|
|
|
|
|
In this particular example we want to generate a new project based on a
|
|
|
|
cookiecutter template. We want to test default values but also data that
|
|
|
|
emulates user input.
|
|
|
|
|
|
|
|
- use default values
|
2016-06-25 17:06:17 +08:00
|
|
|
|
2016-06-24 18:08:57 +08:00
|
|
|
- emulate user input
|
2016-06-25 17:06:17 +08:00
|
|
|
|
2016-06-24 18:08:57 +08:00
|
|
|
- specify 'author'
|
2016-06-25 17:06:17 +08:00
|
|
|
|
2016-06-24 18:08:57 +08:00
|
|
|
- specify 'project_slug'
|
2016-06-25 17:06:17 +08:00
|
|
|
|
2016-06-24 18:08:57 +08:00
|
|
|
- specify 'author' and 'project_slug'
|
|
|
|
|
|
|
|
This is how a functional test could look like:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def default_context():
|
|
|
|
return {'extra_context': {}}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(params=[
|
|
|
|
{'author': 'alice'},
|
|
|
|
{'project_slug': 'helloworld'},
|
|
|
|
{'author': 'bob', 'project_slug': 'foobar'},
|
|
|
|
])
|
|
|
|
def extra_context(request):
|
|
|
|
return {'extra_context': request.param}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(params=['default', 'extra'])
|
|
|
|
def context(request):
|
|
|
|
if request.param == 'default':
|
|
|
|
return request.getfuncargvalue('default_context')
|
|
|
|
else:
|
|
|
|
return request.getfuncargvalue('extra_context')
|
|
|
|
|
|
|
|
|
|
|
|
def test_generate_project(cookies, context):
|
|
|
|
"""Call the cookiecutter API to generate a new project from a
|
|
|
|
template.
|
|
|
|
"""
|
|
|
|
result = cookies.bake(extra_context=context)
|
|
|
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert result.exception is None
|
|
|
|
assert result.project.isdir()
|
|
|
|
|
2016-06-25 16:10:57 +08:00
|
|
|
|
|
|
|
Issues
|
|
|
|
------
|
|
|
|
|
|
|
|
* By using ``request.getfuncargvalue()`` we rely on actual fixture function
|
|
|
|
execution to know what fixtures are involved, due to it's dynamic nature
|
|
|
|
* More importantly, ``request.getfuncargvalue()`` cannot be combined with
|
|
|
|
parametrized fixtures, such as ``extra_context``
|
|
|
|
* This is very inconvenient if you wish to extend an existing test suite by
|
|
|
|
certain parameters for fixtures that are already used by tests
|
|
|
|
|
2016-06-25 20:52:57 +08:00
|
|
|
pytest version 3.0 reports an error if you try to run above code::
|
2016-06-25 16:10:57 +08:00
|
|
|
|
|
|
|
Failed: The requested fixture has no parameter defined for the current
|
|
|
|
test.
|
|
|
|
|
|
|
|
Requested fixture 'extra_context'
|
|
|
|
|
2016-06-25 16:50:14 +08:00
|
|
|
|
|
|
|
Proposed solution
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
A new function that can be used in modules can be used to dynamically define
|
|
|
|
fixtures from existing ones.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
pytest.define_combined_fixture(
|
|
|
|
name='context',
|
|
|
|
fixtures=['default_context', 'extra_context'],
|
|
|
|
)
|
|
|
|
|
2016-06-25 17:00:54 +08:00
|
|
|
The new fixture ``context`` inherits the scope from the used fixtures and yield
|
|
|
|
the following values.
|
|
|
|
|
|
|
|
- ``{}``
|
2016-06-25 17:06:17 +08:00
|
|
|
|
2016-06-25 17:00:54 +08:00
|
|
|
- ``{'author': 'alice'}``
|
2016-06-25 17:06:17 +08:00
|
|
|
|
2016-06-25 17:00:54 +08:00
|
|
|
- ``{'project_slug': 'helloworld'}``
|
2016-06-25 17:06:17 +08:00
|
|
|
|
2016-06-25 17:00:54 +08:00
|
|
|
- ``{'author': 'bob', 'project_slug': 'foobar'}``
|
2016-06-25 16:50:14 +08:00
|
|
|
|
2016-06-25 16:55:08 +08:00
|
|
|
Alternative approach
|
|
|
|
--------------------
|
|
|
|
|
2017-11-04 05:13:47 +08:00
|
|
|
A new helper function named ``fixture_request`` would tell pytest to yield
|
|
|
|
all parameters marked as a fixture.
|
2016-06-25 16:55:08 +08:00
|
|
|
|
2017-11-10 06:14:24 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
The `pytest-lazy-fixture <https://pypi.python.org/pypi/pytest-lazy-fixture>`_ plugin implements a very
|
|
|
|
similar solution to the proposal below, make sure to check it out.
|
|
|
|
|
2016-06-25 16:55:08 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
@pytest.fixture(params=[
|
|
|
|
pytest.fixture_request('default_context'),
|
|
|
|
pytest.fixture_request('extra_context'),
|
|
|
|
])
|
|
|
|
def context(request):
|
|
|
|
"""Returns all values for ``default_context``, one-by-one before it
|
|
|
|
does the same for ``extra_context``.
|
|
|
|
|
|
|
|
request.param:
|
|
|
|
- {}
|
|
|
|
- {'author': 'alice'}
|
|
|
|
- {'project_slug': 'helloworld'}
|
|
|
|
- {'author': 'bob', 'project_slug': 'foobar'}
|
|
|
|
"""
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
The same helper can be used in combination with ``pytest.mark.parametrize``.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'context, expected_response_code',
|
|
|
|
[
|
|
|
|
(pytest.fixture_request('default_context'), 0),
|
|
|
|
(pytest.fixture_request('extra_context'), 0),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_generate_project(cookies, context, exit_code):
|
|
|
|
"""Call the cookiecutter API to generate a new project from a
|
|
|
|
template.
|
|
|
|
"""
|
|
|
|
result = cookies.bake(extra_context=context)
|
|
|
|
|
|
|
|
assert result.exit_code == exit_code
|