document fixture override techniques
--HG-- branch : parametrized-fixture-override
This commit is contained in:
parent
c4623939af
commit
d1005ebb8f
|
@ -780,4 +780,182 @@ to a :ref:`conftest.py <conftest.py>` file or even separately installable
|
|||
fixtures functions starts at test classes, then test modules, then
|
||||
``conftest.py`` files and finally builtin and third party plugins.
|
||||
|
||||
Overriding fixtures on various levels
|
||||
-------------------------------------
|
||||
|
||||
In relatively large test suite, you most likely need to ``override`` a ``global`` or ``root`` fixture with a ``locally``
|
||||
defined one, keeping the test code readable and maintainable.
|
||||
|
||||
Override a fixture on a folder (conftest) level
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Given the tests file structure is:
|
||||
|
||||
::
|
||||
|
||||
tests/
|
||||
__init__.py
|
||||
|
||||
conftest.py
|
||||
# content of tests/conftest.py
|
||||
import pytest
|
||||
|
||||
@pytest.fixture
|
||||
def username():
|
||||
return 'username'
|
||||
|
||||
test_something.py
|
||||
# content of tests/test_something.py
|
||||
def test_username(username):
|
||||
assert username == 'username'
|
||||
|
||||
subfolder/
|
||||
__init__.py
|
||||
|
||||
conftest.py
|
||||
# content of tests/subfolder/conftest.py
|
||||
import pytest
|
||||
|
||||
@pytest.fixture
|
||||
def username(username):
|
||||
return 'overridden-' + username
|
||||
|
||||
test_something.py
|
||||
# content of tests/subfolder/test_something.py
|
||||
def test_username(username):
|
||||
assert username == 'overridden-username'
|
||||
|
||||
As you can see, a fixture with the same name can be overridden for certain test folder level.
|
||||
Note that the ``base`` or ``super`` fixture can be accessed from the ``overriding``
|
||||
fixture easily - used in the example above.
|
||||
|
||||
Override a fixture on a test module level
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Given the tests file structure is:
|
||||
|
||||
::
|
||||
|
||||
tests/
|
||||
__init__.py
|
||||
|
||||
conftest.py
|
||||
# content of tests/conftest.py
|
||||
@pytest.fixture
|
||||
def username():
|
||||
return 'username'
|
||||
|
||||
test_something.py
|
||||
# content of tests/test_something.py
|
||||
import pytest
|
||||
|
||||
@pytest.fixture
|
||||
def username(username):
|
||||
return 'overridden-' + username
|
||||
|
||||
def test_username(username):
|
||||
assert username == 'overridden-username'
|
||||
|
||||
test_something_else.py
|
||||
# content of tests/test_something_else.py
|
||||
import pytest
|
||||
|
||||
@pytest.fixture
|
||||
def username(username):
|
||||
return 'overridden-else-' + username
|
||||
|
||||
def test_username(username):
|
||||
assert username == 'overridden-else-username'
|
||||
|
||||
In the example above, a fixture with the same name can be overridden for certain test module.
|
||||
|
||||
|
||||
Override a fixture with direct test parametrization
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Given the tests file structure is:
|
||||
|
||||
::
|
||||
|
||||
tests/
|
||||
__init__.py
|
||||
|
||||
conftest.py
|
||||
# content of tests/conftest.py
|
||||
import pytest
|
||||
|
||||
@pytest.fixture
|
||||
def username():
|
||||
return 'username'
|
||||
|
||||
@pytest.fixture
|
||||
def other_username(username):
|
||||
return 'other-' + username
|
||||
|
||||
test_something.py
|
||||
# content of tests/test_something.py
|
||||
import pytest
|
||||
|
||||
@pytest.mark.parametrize('username', ['directly-overridden-username'])
|
||||
def test_username(username):
|
||||
assert username == 'directly-overridden-username'
|
||||
|
||||
@pytest.mark.parametrize('username', ['directly-overridden-username-other'])
|
||||
def test_username_other(other_username):
|
||||
assert username == 'other-directly-overridden-username-other'
|
||||
|
||||
In the example above, a fixture value is overridden by the test parameter value. Note that the value of the fixture
|
||||
can be overridden this way even if the test doesn't use it directly (doesn't mention it in the function prototype).
|
||||
|
||||
|
||||
Override a parametrized fixture with non-parametrized one and vice versa
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Given the tests file structure is:
|
||||
|
||||
::
|
||||
|
||||
tests/
|
||||
__init__.py
|
||||
|
||||
conftest.py
|
||||
# content of tests/conftest.py
|
||||
import pytest
|
||||
|
||||
@pytest.fixture(params=['one', 'two', 'three'])
|
||||
def parametrized_username(request):
|
||||
return request.param
|
||||
|
||||
@pytest.fixture
|
||||
def non_parametrized_username(request):
|
||||
return 'username'
|
||||
|
||||
test_something.py
|
||||
# content of tests/test_something.py
|
||||
import pytest
|
||||
|
||||
@pytest.fixture
|
||||
def parametrized_username():
|
||||
return 'overridden-username'
|
||||
|
||||
@pytest.fixture(params=['one', 'two', 'three'])
|
||||
def non_parametrized_username(request):
|
||||
return request.param
|
||||
|
||||
def test_username(parametrized_username):
|
||||
assert parametrized_username == 'overridden-username'
|
||||
|
||||
def test_parametrized_username(non_parametrized_username):
|
||||
assert non_parametrized_username in ['one', 'two', 'three']
|
||||
|
||||
test_something_else.py
|
||||
# content of tests/test_something_else.py
|
||||
def test_username(parametrized_username):
|
||||
assert parametrized_username in ['one', 'two', 'three']
|
||||
|
||||
def test_username(non_parametrized_username):
|
||||
assert non_parametrized_username == 'username'
|
||||
|
||||
In the example above, a parametrized fixture is overridden with a non-parametrized version, and
|
||||
a non-parametrized fixture is overridden with a parametrized version for certain test module.
|
||||
The same applies for the test folder level obviously.
|
||||
|
|
Loading…
Reference in New Issue