Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest - getting the value of fixture parameter

Tags:

python

pytest

Is there a way to save the value of parameter, provided by pytest fixture:

Here is an example of conftest.py

# content of conftest.py

import pytest

def pytest_addoption(parser):
    parser.addoption("--parameter", action="store", default="default",
                     help="configuration file path")

@pytest.fixture
def param(request):
    parameter = request.config.getoption("--parameter")
    return parameter

Here is an example of pytest module:

# content of my_test.py

def test_parameters(param):
    assert param == "yes"

OK - everything works fine, but is there a way to get the value of param outside the test - for example with some build-in pytest function pytest.get_fixture_value["parameter"]

EDITED - DETAILED EXPLANATION WHAT I WANT TO ACHIEV

I am writing an module, that deploys and after that provides parameters to tests, writen in pytest. My idea is if someones test looks like that:

class TestApproachI:
    @load_params_as_kwargs(parameters_A)
    def setup_class(cls, param_1, param_2, ... , param_n):
        # code of setup_class

    def teardown_class(cls):
        # some code

    def test_01(self):
        # test code

And this someone gives me a configuration file, that explains with what parameters to run his code, I will analyze those parameters (in some other script) and I will run his tests with the command pytest --parameters=path_to_serialized_python_tuple test_to_run where this tuple will contain the provided values for this someone parameters in the right order. And I will tell that guy (with the tests) to add this decorator to all the tests he wants me to provide parameters. This decorator would look like this:

class TestApproachI:
    # this path_to_serialized_tuple should be provided by 'pytest --parameters=path_to_serialized_python_tuple test_to_run'
    @load_params(path_to_serialized_tuple) 
    def setup_class(cls, param_1, param_2, ... , param_n):
        # code of setup_class

    def teardown_class(cls):
        # some code

    def test_01(self):
        # test code

The decorator function should look like that:

def load_params(parameters):
    def decorator(func_to_decorate):
        @wraps(func_to_decorate)
        def wrapper(self):
            # deserialize the tuple and decorates replaces the values of test parameters
            return func_to_decorate(self, *parameters)
        return wrapper
    return decorator
like image 229
stefan.stt Avatar asked Oct 23 '25 03:10

stefan.stt


2 Answers

Set that parameter as os environment variable, and than use it anywhere in your test through os.getenv('parameter')

So, you can use like,

@pytest.fixture
def param(request):
    parameter = request.config.getoption("--parameter")
    os.environ["parameter"]=parameter 
    return parameter

@pytest.mark.usefixtures('param')
def test_parameters(param):
    assert os.getenv('parameter') == "yes"
like image 180
Chanda Korat Avatar answered Oct 26 '25 14:10

Chanda Korat


I am using pytest-lazy-fixture to get the value any fixture:

  • first install it using pip install pytest-lazy-fixture or pipenv install pytest-lazy-fixture
  • then, simply assign the fixture to a variable like this if you want:
    fixture_value = pytest.lazy_fixture('fixture')

the fixture has to wrapped with quotations

like image 20
Abdulateef Al-radaee Avatar answered Oct 26 '25 15:10

Abdulateef Al-radaee